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

Sathish Nadarajan
 
Solution Architect
July 6, 2017
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
2680

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
[Total: 0    Average: 0/5]
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment