Wednesday, August 14, 2019

Regex.IsMatch & Regex.Matches- get the Matched words - Matches

Regex.IsMatch   - it will return true or false.

&  Regex.Matches -  get the Matched words - Matches


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)
                        {
                            var matches = Regex.Matches(htmltotext, profanePattern, RegexOptions.IgnoreCase);
                            string flaggedTermsString1 = string.Empty;
                            if (matches.Count > 0)
                            {
                                foreach (var bWord in matches)
                                {
                                    flaggedTermsString1 += $"{bWord},";
                                }
                            }
}

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

Azure CLI

Azure CLI

Getting the Page URL of a File in CSOM If title & Name was different in Site Pages Library

postPageCtx.Credentials = new SharePointOnlineCredentials(un, securePwd);
var list = postPageCtx.Web.Lists.GetByTitle(pageLibName);
var item = list.GetItemById(pageItemID);
postPageCtx.Load(item);
postPageCtx.ExecuteQuery();
var fieldVals = item.FieldValues;
var postPageUrl = pageSiteUrl + "/SitePages/" + fieldVals["FileLeafRef"].ToString();

LIST OF INTERNAL NAMES FOR SHAREPOINT FIELDS

In order to reference a column or field using the SharePoint object model, you often need to know its internal name. For example, when creating a CAML query, you can specify the field on which to search by providing its internal name. As opposed to the display name, which can be changed in the UI, the internal name is immutable. While the default display name and the internal name are often identical or similar, they can also be very different. The difference isn’t always consistent. For example, spaces in the display name can be removed, such as IsCheckedoutToLocal, or replaced with the hexadecimal equivalent, such as HTML_x0020_File_x0020_Type. Furthermore, the display name can be the same for more than one fields, so the internal name is the only way to distinguish between them.
You can quickly determine the internal name of a field using the UI:
  1. Open the List Settings page
  2. Under the Columns section, select a column to view the Edit Column page
  3. The URL of this page includes the internal name in the query string. For example, the URL for the Created By field includes the following query string List=%7BF641CEF1%2DCDE2%2D49E1%2D9800%2D861A408EF890%7D&Field=Author. The value for the Field parameter, Author, is the internal name for Created By.
However, this approach isn’t always convenient. If the column you want doesn’t already belong to the list, you’d have to first add it.
For reference purpose, below is the list of the display name and corresponding internal name for the default fields of a Document Library.
TITLEINTERNAL NAME
Approval Status_ModerationStatus
Approver Comments_ModerationComments
Check In Comment_CheckinComment
Checked Out ToCheckoutUser
Checked Out ToCheckedOutTitle
Checked Out ToLinkCheckedOutTitle
Content TypeContentType
Content Type IDContentTypeId
Copy Source_CopySource
CreatedCreated
CreatedCreated_x0020_Date
Created ByAuthor
Document Created ByCreated_x0020_By
Document Modified ByModified_x0020_By
EditEdit
Edit Menu Table End_EditMenuTableEnd
Edit Menu Table Start_EditMenuTableStart
Effective Permissions MaskPermMask
Encoded Absolute URLEncodedAbsUrl
File SizeFile_x0020_Size
File SizeFileSizeDisplay
File TypeFile_x0020_Type
GUIDGUID
Has Copy Destinations_HasCopyDestinations
Html File Linkxd_ProgID
HTML File TypeHTML_x0020_File_x0020_Type
IDID
ID of the User who has the item Checked OutCheckedOutUserId
Instance IDInstanceID
Is Checked out to localIsCheckedoutToLocal
Is Current Version_IsCurrentVersion
Is Signedxd_Signature
Item TypeFSObjType
Level_Level
MergeCombine
ModifiedModified
ModifiedLast_x0020_Modified
Modified ByEditor
NameFileLeafRef
NameLinkFilenameNoMenu
NameLinkFilename
NameBaseName
OrderOrder
owshiddenversionowshiddenversion
PathFileDirRef
ProgIdProgId
Property BagMetaInfo
RelinkRepairDocument
ScopeIdScopeId
SelectSelectTitle
SelectSelectFilename
Server Relative URLServerUrl
Shared File Index_SharedFileIndex
Source Name (Converted Document)ParentLeafName
Source Url_SourceUrl
Source Version (Converted Document)ParentVersionString
Template LinkTemplateUrl
TitleTitle
TypeDocIcon
UI Version_UIVersion
Unique IdUniqueId
URL PathFileRef
Version_UIVersionString
Virus StatusVirusStatus
Workflow Instance IDWorkflowInstanceID
Workflow VersionWorkflowVersion
In my case, I’m interested in the Name field. I wouldn’t have guessed that it would be mapped to FileLeafRef.
Referenced Link : 
My code for CSOM as given Below.
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 postPageUrl = pageSiteUrl + "/SitePages/" + fieldVals["FileLeafRef"].ToString();

is Required & Validation message in an adaptive card

The parameter to make the text field mandatory in adaptive card is "isRequired", pls try modify the required parameter with the be...