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.
- Create the console application as shown below.
Figure 1: Office 365-SharePoint Online - Creating Console Application
- Add following keys to app.config files.
- <appSettings>
- <add key="CredentialsFilePath" value="C:\spo\knowledgejunction\knuser.txt" />
- </appSettings>
- Install the respective NuGet packages.
- 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.
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.
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.
Figure 4: Office 365 - SharePoint Online - Assemblies references added after installing "Microsoft.SharePointOnline.CSOM" NuGet package
- We will also need to install the package “sharepoint pnp coreonline” for accessing modern page so in NuGet Manager, search for the phrase.

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.
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.
- Installing “SharePointOnline.CSOM” package for CSOM APIs to connect with our Office 365 site.
Code
Following is the method to disable the page comments for given page in a given site.
- #region Public Operations - Disable the page comments
- /// <summary>
- /// Method to disable the page comments of given page on given site
- /// </summary>
- /// <param name="siteUrl">URL of the site where the our page is</param>
- /// <param name="pageName">Name of page with extension - home.aspx for which we need to disable the comments</param>
- /// <returns></returns>
- public static bool disablePageComments(string siteUrl, string pageName)
- {
- bool pageCommentsdisabled = false;
- try
- {
- pageCommentsdisabled = true;
- Console.WriteLine("Getting the credentials");
- //Get the appsetings values - credential file path
- string credentialFilePath = ConfigurationManager.AppSettings["CredentialsFilePath"];
- //Read the credential file for getting user name and password
- string[] credentialFileLines = System.IO.File.ReadAllLines(credentialFilePath);
- //First line from credential file path - user name
- string userName = credentialFileLines[0].Trim();
- //Second line from credential file path - password
- //Also converting password to securestring
- SecureString password = GetSecureString(credentialFileLines[1].Trim());
- //Getting the credentials
- ICredentials credentials;
- credentials = new SharePointOnlineCredentials(userName, password);
- Console.WriteLine("Creating context");
- //Get the client context
- ClientContext clientContext = new ClientContext(siteUrl);
- clientContext.Credentials = credentials;
- Console.WriteLine("Getting specific given page");
- //Get the page instance
- ClientSidePage homePage = ClientSidePage.Load(clientContext, pageName);
- Console.WriteLine("Disabling the page comments");
- //Disable the comments on page
- homePage.DisableComments();
- //Save the page
- homePage.Save();
- //Finally Publish the page
- homePage.Publish();
- Console.WriteLine("Page comments disabled successfully");
- }
- catch (Exception ex)
- {
- Console.Write(ex.Message);
- }
- return pageCommentsdisabled;
- }//disablePageComments
- #endregion
- #region Common private Methods
- /// <summary>
- /// Returns secured string for the given string
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- private static SecureString GetSecureString(string str)
- {
- var secstr = new SecureString();
- str.ToCharArray().ToList().ForEach(p => secstr.AppendChar(p));
- return secstr;
- }
- #endregion
Test call to the method -
- 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.

Join the conversation! Your thoughts help the community grow.