Office 365 – SharePoint Online – CSOM – Modern Page Operations for beginners – Disabling the comments on modern page

Hi All,

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 looking for Modern sites in Office 365.


I am using Visual Studio 2017 community edition since which is freely available 🙂 . Following are the common steps required to connect to modern site and to access the page. Once preparation is done we will do actual code…

Create the console application as:


Figure 1: Office 365-SharePoint Online – Creating Console Application

Add following keys to app.config file

Installing respective NuGet packages:

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

In Solution Explorer, right click on references and click on option “Manage NuGet Packages” as

fig2_Opening NuGet manager

Figure 2: Office 365 – SharePoint Online – Manage NuGet Packages…

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

fig2_Installing Microsoft.SharePointOnline.CSOM package

Figure 3: Office 365 – SharePoint Online – Installing “Microsoft.SharePointOnline.CSOM” NuGet package

Once we have successfully installed packages, references to following assemblies will be added to our project as shown in below figure:

fig4_References added after installing Microsoft.SharePoint.CSOM NuGet package

Figure 4: Office 365 – SharePoint Online – Assemblies references added after installing “Microsoft.SharePointOnline.CSOM” NuGet package

We will also require installing the package “sharepoint pnp core online” for accessing modern page so in NuGet manager search for the phrase as

fig5_Installing SharePoint PnP Core Online package

Figure 5: Office 365 – SharePoint Online – Installing “SharePoint PnP Core Online” NuGet package

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

fig6_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 output window.

Once we have respective references in place, we can code 🙂


Following is the method to disable the page comments for given page in given site. Added detailed self-explanatory comments to the method.

#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;
                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

                //Save the page

                //Finally Publish the page

                Console.WriteLine("Page comments disabled successfully");
            catch (Exception ex)
            return pageCommentsdisabled;

#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;

Test call to the method:

disablePageComments("", "home.aspx");

Thanks for reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles.

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: