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.
Code
Following is the method to disable the page comments for given page in a given site.
- #region Public Operations - Disable the page comments
-
-
-
-
-
-
- public static bool disablePageComments(string siteUrl, string pageName)
- {
- bool pageCommentsdisabled = false;
- try
- {
- pageCommentsdisabled = true;
-
- Console.WriteLine("Getting the credentials");
-
-
- string credentialFilePath = ConfigurationManager.AppSettings["CredentialsFilePath"];
-
- string[] credentialFileLines = System.IO.File.ReadAllLines(credentialFilePath);
-
- string userName = credentialFileLines[0].Trim();
-
-
- SecureString password = GetSecureString(credentialFileLines[1].Trim());
-
-
- ICredentials credentials;
- credentials = new SharePointOnlineCredentials(userName, password);
-
- Console.WriteLine("Creating context");
-
- ClientContext clientContext = new ClientContext(siteUrl);
- clientContext.Credentials = credentials;
-
- Console.WriteLine("Getting specific given page");
-
- ClientSidePage homePage = ClientSidePage.Load(clientContext, pageName);
-
- Console.WriteLine("Disabling the page comments");
-
- homePage.DisableComments();
-
-
- homePage.Save();
-
-
- homePage.Publish();
-
- Console.WriteLine("Page comments disabled successfully");
- }
- catch (Exception ex)
- {
- Console.Write(ex.Message);
- }
- return pageCommentsdisabled;
- }
- #endregion
-
- #region Common private Methods
-
-
-
-
-
- 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.