How to Configure Search Centre URL in a SharePoint Office 365 Site Programmatically Using C# Client Site Object Model (CSOM)


Sathish Nadarajan
SharePoint MVP
Published On :   06 Jul 2017
Visit Count
Today :  2    Total :   627
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


How to Configure Search Centre URL in a SharePoint Office 365 Site Programmatically Using C# Client Site Object Model (CSOM)

Today, we are going to have a look on a small tip, which will be useful during the deployment package. Usually, when we are provisioning a site, we need to configure the corresponding search centre URL for that site. But, when we try to automate the deployment process, we need C# code for each and every small piece of work. In that aspect, let us see how to configure the search centre URL here.

Usually, the Search URL at the site collection level should be set in the below screen.

1. Go to Site Settings.

clip_image002

2. Click on the Search Settings.

clip_image004

3. By default, the Search Centre URL will be empty.

4. We can type our Search Site here and click on OK.

5. Now, let us see how to do that programmatically.

 using Microsoft.SharePoint.Client;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             ConfigureSearchCentreURL();
         }
 
         public static void ConfigureSearchCentreURL()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string sourceSiteUrl = "https://*****.sharepoint.com/sites/communitysite";
             
             string userName = "Sathish@*****.onmicrosoft.com";
             string password = "*********";
 
             var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(sourceSiteUrl, userName, password);
 
             Web web = clientContext.Web;
             clientContext.Load(web);
 
             //web.SetWebSearchCenterUrl("https:// *****.sharepoint.com/sites/searchcentresiteurl");
             web.SetSiteCollectionSearchCenterUrl("https:// *****.sharepoint.com/sites/searchcentresiteurl");
 
             web.Update();
 
             clientContext.ExecuteQuery();
         }
 
     }
 }
 

6. The above piece of code will be updating at the site collection search centre URL.

7. If we want to update the Web Level URL, then the commented line will be helpful.

clip_image006

8. Click on the Site Level Search Settings.

clip_image008

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint