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. Console.WriteLine("Getting the credentials");
  15. //Get the appsetings values - credential file path
  16. string credentialFilePath = ConfigurationManager.AppSettings["CredentialsFilePath"];
  17. //Read the credential file for getting user name and password
  18. string[] credentialFileLines = System.IO.File.ReadAllLines(credentialFilePath);
  19. //First line from credential file path - user name
  20. string userName = credentialFileLines[0].Trim();
  21. //Second line from credential file path - password
  22. //Also converting password to securestring
  23. SecureString password = GetSecureString(credentialFileLines[1].Trim());
  24. //Getting the credentials
  25. ICredentials credentials;
  26. credentials = new SharePointOnlineCredentials(userName, password);
  27. Console.WriteLine("Creating context");
  28. //Get the client context
  29. ClientContext clientContext = new ClientContext(siteUrl);
  30. clientContext.Credentials = credentials;
  31. Console.WriteLine("Getting specific given page");
  32. //Get the page instance
  33. ClientSidePage homePage = ClientSidePage.Load(clientContext, pageName);
  34. Console.WriteLine("Disabling the page comments");
  35. //Disable the comments on page
  36. homePage.DisableComments();
  37. //Save the page
  38. homePage.Save();
  39. //Finally Publish the page
  40. homePage.Publish();
  41. Console.WriteLine("Page comments disabled successfully");
  42. }
  43. catch (Exception ex)
  44. {
  45. Console.Write(ex.Message);
  46. }
  47. return pageCommentsdisabled;
  48. }//disablePageComments
  49. #endregion
  50. #region Common private Methods
  51. /// <summary>
  52. /// Returns secured string for the given string
  53. /// </summary>
  54. /// <param name="str"></param>
  55. /// <returns></returns>
  56. private static SecureString GetSecureString(string str)
  57. {
  58. var secstr = new SecureString();
  59. str.ToCharArray().ToList().ForEach(p => secstr.AppendChar(p));
  60. return secstr;
  61. }
  62. #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.