Tuesday, August 13, 2019

Read site page contents in Sharepoint using CSOM

In Site Pages library (list type of ListTemplateType.WebPageLibrary) the content for list item depending on the underling the content type could be extracted from:

  • WikiField field in case of Wiki Page
  • CanvasContent1 field in case of Site Page (aka "modern" page)

Example
var listTitle = "Site Pages";
var list = ctx.Web.Lists.GetByTitle(listTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items, icol => icol.Include( i => i["WikiField"], i => i["CanvasContent1"], i => i["FileRef"], i => i.ContentType));
ctx.ExecuteQuery();
foreach (var item in items)
{
     Console.WriteLine(">>> {0}", item["FileRef"]);
     switch (item.ContentType.Name)
     {
        case "Site Page":
          Console.WriteLine(item["CanvasContent1"]);
          break;
        case "Wiki Page":
          Console.WriteLine(item["WikiField"]);
          break;

     }    
 }



#region ReadModernPageData
            void ReadingModernPageData()
            {
                bool IsAzureProfanity = false;
                bool IsBlockedProfanity = false;
                using (var postPageCtx = new ClientContext(pageSiteUrl))
                {
                    SecureString secureUsrPwd = new SecureString();
                    foreach (char p in password)
                    {
                        secureUsrPwd.AppendChar(p);
                    }
                    secureUsrPwd.MakeReadOnly();
                    string postBody = string.Empty;
                    TextModerationResults1 demoModeration = new TextModerationResults1();
                    IList<string> returnOutPut = new List<string>();
                    List<string> finalOutPut = new List<string>();
                    byte[] postBodyByteArray;
                    postPageCtx.Credentials = new SharePointOnlineCredentials(userName, secureUsrPwd);
                    var list = postPageCtx.Web.Lists.GetByTitle(pageLibName);
                    var item = list.GetItemById(pageItemID);
                    postPageCtx.Load(item);
                    postPageCtx.ExecuteQuery();
                    var fieldVals = item.FieldValues;

                    var htmltotext = HtmlToNormalizedPlainText(fieldVals["CanvasContent1"].ToString().ToLower());
                    var postPageUrl = pageSiteUrl + "/SitePages/" + fieldVals["FileLeafRef"].ToString();
                    string postTitle = fieldVals["Title"].ToString();
                    log.Info(postPageUrl);
                    //var Author = fieldVals["Author"];
                    FieldUserValue Author = (FieldUserValue)item["Author"];
                    var user = postPageCtx.Web.EnsureUser(Author.LookupValue);
                    // load the data with the context               
                    postPageCtx.Load(user);
                    postPageCtx.ExecuteQuery();
                    var createdBy = user.Email; //+ ";" + hubManagersEmail;

                    var filterWords = GetTermsFromSPlist(log, secureUsrPwd);
                    try
                    {
                        string profanePattern = "";
                        bool isFirst = true;
                        filterWords.ToList().ForEach(i =>
                        {
                            if (isFirst)
                            {
                                profanePattern = "\\b" + i + "\\b";
                                isFirst = false;
                            }
                            else
                                profanePattern += "|\\b" + i + "\\b";
                        });
                        //Write Regex method
                        IsBlockedProfanity = Regex.IsMatch(htmltotext, profanePattern, RegexOptions.IgnoreCase);
                        if (IsBlockedProfanity)
                        {
                            ClientContext invReportCtx = GetSiteContext(inv_SiteUrl, userName, secureUsrPwd);
                            //AddToReportedContent(invReportCtx, inv_ReportedContentLstNm, pageSiteUrl, postPageUrl, "Blocked Words Found", "ARContent", "inProgress");
                            //Send email via exchange server
                            // var isSentEmail = SendMail(userName, password, createdBy, postTitle, "Blocked Words Profanity Found & Terms are :", postPageUrl);
                            var isSentEmail=SendMailViaCtx(postPageCtx, userName, password, createdBy,hubManagersEmail, "Warning! | Blocked Words Found in post | " + postTitle, "Blocked Words Profanity Found & Terms are :", postPageUrl);
                           
                        }
                    }

                    catch (Exception)
                    {
                        log.Info("Error while checking profanity using RegEx");
                    }


                    if (htmltotext.Length <= 1024)
                    {
                        postBody = htmltotext;
                        postBodyByteArray = Encoding.UTF8.GetBytes(postBody);
                        postBodyMemoryStream = new MemoryStream(postBodyByteArray);
                        returnOutPut.Clear();
                        (demoModeration, returnOutPut, IsAzureProfanity) = TextContentModeration1.CheckContentForModeration(postBodyMemoryStream, log);
                        if (IsAzureProfanity)
                        {
                            IsAzureProfanityFinal = true;
                        }
                        finalOutPut.AddRange(returnOutPut.Distinct());
                    }
                    else
                    {
                        char[] delimiters = new char[] { ',', ';', '.' };
                        string[] splittedSentence = htmltotext.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        string normalizedText1024Char = string.Empty;
                        int screenTextLength = 0;
                        int splittedSentenceIndexCounter = 0;
                        foreach (string sentence in splittedSentence)
                        {
                            splittedSentenceIndexCounter++;
                            screenTextLength = normalizedText1024Char.Length + sentence.Length;
                            if (screenTextLength <= 1024)
                            {
                                normalizedText1024Char += sentence;
                                if (splittedSentenceIndexCounter == splittedSentence.Length)
                                {
                                    postBody = normalizedText1024Char;
                                    postBodyByteArray = Encoding.UTF8.GetBytes(postBody);
                                    postBodyMemoryStream = new MemoryStream(postBodyByteArray);
                                    returnOutPut.Clear();
                                    (demoModeration, returnOutPut, IsAzureProfanity) = TextContentModeration1.CheckContentForModeration(postBodyMemoryStream, log);
                                    if (IsAzureProfanity)
                                    {
                                        IsAzureProfanityFinal = true;
                                    }
                                    finalOutPut.AddRange(returnOutPut.Distinct());
                                }
                            }
                            else
                            {
                                postBody = normalizedText1024Char;
                                postBodyByteArray = Encoding.UTF8.GetBytes(postBody);
                                postBodyMemoryStream = new MemoryStream(postBodyByteArray);
                                returnOutPut.Clear();
                                (demoModeration, returnOutPut, IsAzureProfanity) = TextContentModeration1.CheckContentForModeration(postBodyMemoryStream, log);
                                if (IsAzureProfanity)
                                {
                                    IsAzureProfanityFinal = true;
                                }
                                finalOutPut.AddRange(returnOutPut.Distinct());
                                screenTextLength = 0;
                                normalizedText1024Char = string.Empty;
                                screenTextLength = sentence.Length;
                                normalizedText1024Char = sentence;
                                // very few scenarios - last sentence will validate in below code
                                if (splittedSentenceIndexCounter == splittedSentence.Length)
                                {
                                    postBody = normalizedText1024Char;
                                    postBodyByteArray = Encoding.UTF8.GetBytes(postBody);
                                    postBodyMemoryStream = new MemoryStream(postBodyByteArray);
                                    returnOutPut.Clear();
                                    (demoModeration, returnOutPut, IsAzureProfanity) = TextContentModeration1.CheckContentForModeration(postBodyMemoryStream, log);
                                    if (IsAzureProfanity)
                                    {
                                        IsAzureProfanityFinal = true;
                                    }
                                    finalOutPut.AddRange(returnOutPut.Distinct());
                                }
                            }
                        }
                    }
                    // add to Inv_RequestContent list
                    string flaggedTermsString = string.Empty;

                    foreach (var distinctITem in finalOutPut.Distinct())
                    {
                        flaggedTermsString += $"{distinctITem},";
                    }

                    log.Info(finalOutPut.Distinct().Count().ToString());

                    if (finalOutPut.Distinct().Count() > 0)
                    {
                        //Update List Item:
                        ClientContext invReportCtx = GetSiteContext(inv_SiteUrl, userName, secureUsrPwd);
                        AddToReportedContent(invReportCtx, inv_ReportedContentLstNm, pageSiteUrl, postPageUrl, "Azure Profanity & Terms are :" + flaggedTermsString, "ARContent", "inProgress");

                        //Send email via exchange server
                        var isSentEmail = SendMailViaCtx(postPageCtx, userName, password, createdBy,hubManagersEmail, "Profanity Alert! Azure Profanity found in Post ! "+postTitle, "Azure Profanity & Terms are :" + flaggedTermsString, postPageUrl);

                    }
                    else
                    {
                        log.Info("Page doesn't have data");
                    }

                }
                #endregion

No comments:

Update or edit status filed in JIRA issue

the status field is a special case and can't be updated directly, which is the second problem here. Changing a status in Jira is called ...