Create search center site programmatically using CSOM in SharePoint Online

This SharePoint tutorial explains, how to create a search center site programmatically using CSOM in SharePoint Online. We will here basically use the .Net managed object model code (C#) to connect to the SharePoint Online site.

We can use a console application or we can also use the windows form application. Here we will add the windows form application.

To work with SharePoint objects we need to add the below details:

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

Create a SharePoint search center site programmatically using CSOM

Here we need to pass the credentials to connect to the SharePoint Online site and then we are calling the CreateSearchCenterSite() method which will create the search center site.

We need to use the WebCreationInformation class to create the site. Here we need to pass parameters like:

  • Url
  • Title
  • Description
  • WebTemplate (For search the template is “SRCHCEN#0”)
  • UseSamePermissionsAsParentSite
  • Language etc.

Below is the full code to create a search center site using csom in SharePoint Online.

private void button2_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
var secureString = new SecureString();
foreach (char c in "*********")
{
secureString.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials("*****@onlysharepoint2013.onmicrosoft.com", secureString);
try
{
CreateSearchCenterSite(context);
}

catch (Exception ex)
{
throw;
}
}
}

void CreateSearchCenterSite(ClientContext context)
{
WebCreationInformation wci = new WebCreationInformation();
wci.Url = "Search";
wci.Title = "Search";
wci.Description = "Our Search Center Site";
wci.UseSamePermissionsAsParentSite = true;
wci.WebTemplate = "SRCHCEN#0";
wci.Language = 1033;
Web w = context.Site.RootWeb.Webs.Add(wci);
context.ExecuteQuery();
}

Once you run the code, the search center site will be created successfully like below:

create search center site programmatically using CSOM

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we learned how to create a search center site programmatically using CSOM in SharePoint Online.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>