Set SharePoint Search Settings (Search Center URL, Scope Dropdown Mode) Programmatically

After migrating SharePoint 2007 sites to SharePoint 2010, we decided to set search center URL to a Enterprise Search center site and scope settings Dropdown mode to use “Show scopes dropdown” for all site collections.

To update search center and search scope dropdown settings in SharePoint sites, We used to go:

  • Site Actions >> Site Settings 
  • Click on “Search Settings” under “Site collection Administration”
  • Set search center URL and search scope dropdown
set sharepoint search center URL programmatically powershell

Update SharePoint Search Settings Programmatically:
Alright, How about new site collections which we are going to create down the line? Well, We used this code in a feature stapler to programmatically set SharePoint search center URL:

//Read Search Center URL value from Feature.xml
if (properties.Feature.Properties["SearchCenterURL"] != null)
{
   string searchCenterURL = properties.Feature.Properties["SearchCenterURL"].Value;
   //Set the Search Center URL
   web.AllProperties["SRCH_ENH_FTR_URL"] = searchCenterURL;
   web.Update();
}

property segment from Feature.xml file:

<Properties>
   <Property Key="SearchCenterURL" Value="https://SearchCenter.Crescent.com" />
</Properties>

Set Search Center URL using PowerShell

To update the Search Settings programmatically, There is no API available, but these values are stored as a part of the property bag of the SPWeb object. For existing sites that are already created prior to deploying this feature, let’s use PowerShell to set search center URL and Search scope dropdown mode:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$WebAppURL = "https://sharepoint.crescent.com"
$SearchCenterURL= "https://search.crescent.com"

#Iterate through each site in the given web application
Get-SPWebApplication -Identity $WebAppURL | Get-SPSite -Limit ALL | foreach { 
        $_.RootWeb.AllProperties["SRCH_ENH_FTR_URL"] = $SearchCenterURL
        $_.RootWeb.AllProperties["SRCH_SITE_DROPDOWN_MODE"] = "ShowDD"
        $_.RootWeb.Update()         
        Write-Host "Search settings Updated for "$_.RootWeb.Url -ForeGroundColor DarkGreen
}   

Set SharePoint Search Settings Programmatically using C#:

In another scenario, I wrote a .net C# console application that does the job of updating the search center URL and search scope dropdown mode.

SPWebApplication webApp = SPWebApplication.Lookup(new Uri("https://sharepoint.crescent.com"));

foreach (SPSite site in webApp.Sites)
{
	SPWeb web = site.OpenWeb();

	web.AllProperties["SRCH_ENH_FTR_URL"] = "https://search.crescent.com/pages/";
	web.AllProperties["SRCH_SITE_DROPDOWN_MODE"] =    "ShowDD"       ;
	//web.AllProperties["SRCH_TRAGET_RESULTS_PAGE"] = "https://search.crescent.com/pages/Results.aspx";
	
	web.Update();
	Console.WriteLine ("Updated Search settings for " + web.Url);
	web.Dispose();
	site.Dispose();
}

Search Scope Dropdown modes:

Below are the valid values you can use for SRCH_SITE_DROPDOWN_MODE

  • ShowDD – Show scopes dropdown
  • ShowDD_DefaultURL – Show, and default to ‘s’ URL
  • ShowDD_DefaultContextual – Show and default to contextual scope
  • ShowDD_NoContextual – Show, do not include contextual scopes
  • ShowDD_NoContextual_DefaultURL – Show, do not include contextual scopes, and default to ‘s’ URL paramete
  • HideScopeDD_DefaultContextual – Do Not Show Scopes Dropdown, and default to contextual scope
  • HideScopeDD – Do not show scopes dropdown, and default to target results page

For Search Scope Dropdown modes, Refer: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.portal.webcontrols.dropdownmodes%28v=office.14%29.aspx

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *