Wednesday, January 14, 2015

Automate hybrid search center configuration in SharePoint Server 2013



In order to configure a SharePoint hybrid environment to display both SharePoint online search result and on-premises result, you would need to configure search Result Source and  Query Rules. The configuration process is tedious and I would like to provide two scripts to automate the process.

Here are the scripts for both  Result Source and Query Rules creation. I’m creating both on the search service level so they could be used by multiple on-premises search centers.

# Create Result Source on search service level for SharePoint online
if(-not (Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue)){Add-PSSnapin "Microsoft.SharePoint.PowerShell"}

# Parameter settings
$RemoteSharePointUrl = "https://qualcomm.sharepoint.com";
$ResultSourceName = "SharePoint Online1";
$ProviderID = "1e0c8601-2e5d-4ccb-9561-53743b5dbde7";
# Parameter settings end

#Get the Search Service Application
$SSA = Get-SPEnterpriseSearchServiceApplication;

#Get the Search Service Application Owner
$SSAOwner = Get-SPEnterpriseSearchOwner -Level SSA ;

#Try getting the result source
$resultSource = Get-SPEnterpriseSearchResultSource -Owner $SSAOwner -SearchApplication $SSA -Identity $ResultSourceName;

if(-not $resultSource)
{
$resultSource = New-SPEnterpriseSearchResultSource -Name $ResultSourceName -Owner $SSAOwner -ProviderId $ProviderID -SearchApplication $SSA -RemoteUrl $RemoteSharePointUrl;   
}

 
# Create Query Rules search service level for SharePoint online Result Source
if(-not (Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue)){Add-PSSnapin "Microsoft.SharePoint.PowerShell"}
# Parameter settings
$queryRuleName='SharePoint Online - Query Rule'
$resultSourceName='SharePoint Online' #This is the Result Source created above
$allSourceContext='TRUE'
# The more URL is the new page created on-premises for SharePoint online search
$moreItemsURL='/search/pages/o365results.aspx?k={subjectTerms}'

#Get the Search Service Application
$SSA = (Get-SPEnterpriseSearchServiceApplication)[0] ;

#Get the Search Service Application Owner
$SSAOwner = Get-SPEnterpriseSearchOwner -Level SSA ;

$resultSource = Get-SPEnterpriseSearchResultSource -Owner $SSAOwner -SearchApplication $SSA -Identity $resultSourceName;

if($resultSource)
{

        #Get the Query Rule Manager
        $QueryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($SSA);

        # Create a search object filter using a $SearchOwner object  (Site collection level - in this case)
        $SearchObjectFilter =  New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($SSAOwner);

        $QueryRules = $QueryRuleManager.GetQueryRules($SearchObjectFilter);
        $queryRule = $QueryRules | Where-Object{$_.DisplayName -eq $queryRuleName };
       
       
        # Create a new rule as a active one if does not exists
        if(-not $queryRule)
        {
            Write-Host -ForegroundColor White "Creating New Query Rule with Name  $queryRuleName";
             "Creating New Query Rule .... " >> $logFile;
           
            $queryRule = $QueryRules.CreateQueryRule($queryRuleName,$null,$null,$true);
           
            $allSource = [bool]::Parse($AllSourceContext);
           
            #Add the Result Source..
            if(-not $allSource )
            {
                $QuerySourceContextCondition = $QueryRule.CreateSourceContextCondition($resultSource);
            }

            # Create result block for the Query Rule
            $QueryRuleAction = $QueryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::CreateResultBlock);

            #Set the values as seen when creating through UI.
            $QueryRuleAction.QueryTransform.OverrideProperties = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties;
            $QueryRuleAction.QueryTransform.SourceId = $resultSource.Id;
            $QueryRuleAction.QueryTransform.QueryTemplate = "{subjectTerms}";
            $QueryRuleAction.ResultTitle.DefaultLanguageString = 'Results for "{subjectTerms}"';
            $QueryRuleAction.ResultTitleUrl = $MoreItemsURL;
            $QueryRuleAction.GroupTemplateId = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js";
            $QueryRuleAction.AlwaysShow = $true;

            #Update the object
            $QueryRule.Update();
           
        }

}

1 comment: