How To Disable Comments On Modern Pages Using CSOM

Today, we will discuss how to disable the comments on Modern pages of Modern sites using CSOM. This article is basically targeted for beginners who are working on Modern Sites in Office 365.

Preparation

I am using the Visual Studio 2017 Community Edition which is freely available. Following are the common steps required to connect to a modern site and to access the page. Once preparation is done, we will do the actual code.

  1. Create the console application as shown below.

    Office 365-SharePoint Online - Creating Console Application
    Figure 1: Office 365-SharePoint Online - Creating Console Application
  1. Add following keys to app.config files.
    1. <appSettings>  
    2.    <add key="CredentialsFilePath" value="C:\spo\knowledgejunction\knuser.txt" />  
    3. </appSettings>  
  1. Install the respective NuGet packages.

    1. Installing “SharePointOnline.CSOM” package for CSOM APIs to connect with our Office 365 site.

      In Solution Explorer, right-click on references and click “Manage NuGet Packages” as shown below.

      Office 365 - SharePoint Online - Manage NuGet Packages... 

      Figure 2: Office 365 - SharePoint Online - Manage NuGet Packages...

      NuGet Package Manager will be opened. Under “Browse” tab, search for the key “Microsoft.SharePointOnline.CSOM”, and install the searched package as shown in the below figure.

      Office 365 - SharePoint Online - Installing "Microsoft.SharePointOnline.CSOM" NuGet package 

      Figure 3: Office 365 - SharePoint Online - Installing "Microsoft.SharePointOnline.CSOM" NuGet package

      Once we have successfully installed the packages, references to the following assemblies will be added to our project.

      Office 365-SharePoint Online - Creating Console ApplicationOffice 365 - SharePoint Online - Assemblies references added after installing "Microsoft.SharePointOnline.CSOM" NuGet package 

      Figure 4: Office 365 - SharePoint Online - Assemblies references added after installing "Microsoft.SharePointOnline.CSOM" NuGet package

    2. We will also need to install the package “sharepoint pnp coreonline” for accessing modern page so in NuGet Manager, search for the phrase.

      Office 365 - SharePoint Online - Installing "SharePoint PnP Core Online" NuGet package

      Figure 5: Office 365 - SharePoint Online - Installing "SharePoint PnP Core Online" NuGet package

      Once we have successfully installed the above NuGet package, the following assembly references are added to our package.

      Office 365 - SharePoint Online - Assemblies references added after installing "SharePoint PnP Core Online" NuGet package 

      Figure 6: Office 365 - SharePoint Online - Assemblies references added after installing "SharePoint PnP Core Online" NuGet package

      We could also see the progress of installing respective packages in the output window. Once we have the respective references in place, we can code.

Code

Following is the method to disable the page comments for given page in a given site. 

  1. #region Public Operations - Disable the page comments  
  2.         /// <summary>  
  3.         /// Method to disable the page comments of given page on given site  
  4.         /// </summary>  
  5.         /// <param name="siteUrl">URL of the site where the our page is</param>  
  6.         /// <param name="pageName">Name of page with extension - home.aspx for which we need to disable the comments</param>  
  7.         /// <returns></returns>  
  8.         public static bool disablePageComments(string siteUrl, string pageName)  
  9.         {  
  10.             bool pageCommentsdisabled = false;  
  11.             try  
  12.             {  
  13.                 pageCommentsdisabled = true;  
  14.   
  15.                 Console.WriteLine("Getting the credentials");  
  16.   
  17.                 //Get the appsetings values - credential file path  
  18.                 string credentialFilePath = ConfigurationManager.AppSettings["CredentialsFilePath"];  
  19.                 //Read the credential file for getting user name and password  
  20.                 string[] credentialFileLines = System.IO.File.ReadAllLines(credentialFilePath);  
  21.                 //First line from credential file path - user name  
  22.                 string userName = credentialFileLines[0].Trim();  
  23.                 //Second line from credential file path - password  
  24.                 //Also converting password to securestring   
  25.                 SecureString password = GetSecureString(credentialFileLines[1].Trim());  
  26.   
  27.                 //Getting the credentials  
  28.                 ICredentials credentials;  
  29.                 credentials = new SharePointOnlineCredentials(userName, password);  
  30.   
  31.                 Console.WriteLine("Creating context");  
  32.                 //Get the client context  
  33.                 ClientContext clientContext = new ClientContext(siteUrl);  
  34.                 clientContext.Credentials = credentials;  
  35.   
  36.                 Console.WriteLine("Getting specific given page");  
  37.                 //Get the page instance  
  38.                 ClientSidePage homePage = ClientSidePage.Load(clientContext, pageName);  
  39.   
  40.                 Console.WriteLine("Disabling the page comments");  
  41.                 //Disable the comments on page  
  42.                 homePage.DisableComments();  
  43.   
  44.                 //Save the page  
  45.                 homePage.Save();  
  46.   
  47.                 //Finally Publish the page  
  48.                 homePage.Publish();  
  49.   
  50.                 Console.WriteLine("Page comments disabled successfully");  
  51.             }  
  52.             catch (Exception ex)  
  53.             {  
  54.                 Console.Write(ex.Message);  
  55.             }  
  56.             return pageCommentsdisabled;  
  57.         }//disablePageComments  
  58. #endregion  
  59.  
  60. #region Common private Methods  
  61.         /// <summary>  
  62.         /// Returns secured string for the given string  
  63.         /// </summary>  
  64.         /// <param name="str"></param>  
  65.         /// <returns></returns>  
  66.         private static SecureString GetSecureString(string str)  
  67.         {  
  68.             var secstr = new SecureString();  
  69.             str.ToCharArray().ToList().ForEach(p => secstr.AppendChar(p));  
  70.             return secstr;  
  71.         }  
  72. #endregion  

Test call to the method -

  1. disablePageComments("https://knowledgejunction.sharepoint.com/sites/en/""home.aspx");  

I am also attaching the code zip file, so you can download and directly try it.


Similar Articles