Tuesday, March 26, 2013

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="http://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 which are already created prior to deploying this feature, Lets use PowerShell to set search center URL and Search scope dropdown mode:
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$WebAppURL = "http://sharepoint.crescent.com"
$SearchCenterURL= "http://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 an another scenario, I wrote a .net C# console application that does the job of updating search center URL and search scope dropdown mode.
SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://sharepoint.crescent.com"));

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

                web.AllProperties["SRCH_ENH_FTR_URL"] = "http://search.crescent.com/pages/";
                web.AllProperties["SRCH_SITE_DROPDOWN_MODE"] =    "ShowDD"       ;
                //web.AllProperties["SRCH_TRAGET_RESULTS_PAGE"] = "http://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: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.portal.webcontrols.dropdownmodes%28v=office.14%29.aspx



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...