none
Sharepoint 2013 : Create Query rule and define item number in result block with Powershell RRS feed

  • Question

  • Hi everybody,

    I create a powerShell script to automatically create query rule with result block as query action.

    Creation works well but i have a very frustrating problem.

    I can not define item number in result block so it display only two results...


    Normally in GUI in result block properties windows you have an option "Item" and you can define the number of item you want to display... (by default in GUI set to 2)

    But unable to find a properties to set this parameters in powershell...

    Below sample of my script

    function CreatePromotedResult([string]$title, [string]$KeyWord, [string]$PromotedResult)
    {
        $queryRule = $queryRules.CreateQueryRule($title,$null,$null,$true)
        $queryRule.CreateSourceContextCondition($resultSource)
        [string[]]$keywords = $KeyWord.split(';')		
        $keywordCondition = $queryRule.QueryConditions.CreateKeywordCondition($keywords,$false)
        $queryAction = $queryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::CreateResultBlock)
        $queryAction.AlwaysShow = $true
        $queryAction.QueryTransform.QueryTemplate = "WORDS($promotedID)"
        $queryAction.QueryTransform.SourceId = $resultSource.Id
        $queryRule.Update()
        $queryRule = $null	
    }
    
    #Declaration for getting query rules object
    $siteUrl = "http://******"
    $resultSourceName = "Annuaire****"
    $web = Get-SPWeb $siteUrl
    $ssa = Get-SPEnterpriseSearchServiceApplication
    $queryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($ssa)
    $searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner -ArgumentList  @([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPWeb,$web)
    $searchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($searchOwner)
    $federationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($ssa)
    $resultSource = $federationManager.GetSourceByName($resultSourceName,$searchOwner)
    $queryRules = $queryRuleManager.GetQueryRules($searchObjectFilter)
    
    #my variable that i normally get from xml file
    $title = "Informatique"
    $Keyword = "Helpdesk;IT"
    $PromotedResults = "1306,5897,4619"
    $Keyword = $Keyword + ";$title"
    
    CreatePromotedResult $title $Keyword $PromotedID

    Thanks for your help :)

    But i am starting to think that this parameters is not implemented in powershell... (strange)

    Friday, February 3, 2017 9:12 AM

Answers

  • Hi,

     

    Try this.

     

    function CreatePromotedResult([string]$title, [string]$KeyWord, [string]$PromotedResult)
    {
        $queryRule = $queryRules.CreateQueryRule($title,$null,$null,$true)
        $queryRule.CreateSourceContextCondition($resultSource)
        [string[]]$keywords = $KeyWord.split(';')		
        $keywordCondition = $queryRule.QueryConditions.CreateKeywordCondition($keywords,$false)
    	$queryAction = $queryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::CreateResultBlock)   
    	$resultBlock=$queryRule.CreateResultBlockActions[0]
    	$resultBlock.QueryTransform.OverrideProperties=New-Object Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties
    	$resultBlock.QueryTransform.OverrideProperties["RowLimit"] = 5    
        $queryAction.AlwaysShow = $true
        $queryAction.QueryTransform.QueryTemplate = "{subjectTerms}"
        $queryAction.QueryTransform.SourceId = $resultSource.Id	
        $queryRule.Update()
        $queryRule = $null	
    }
    
    #Declaration for getting query rules object
    $siteUrl = "http://sp:12001"
    $resultSourceName = "Local SharePoint Results"
    $web = Get-SPWeb $siteUrl
    $ssa = Get-SPEnterpriseSearchServiceApplication
    $queryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($ssa)
    $searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner -ArgumentList  @([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPWeb,$web)
    $searchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($searchOwner)
    $federationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($ssa)
    $resultSource = $federationManager.GetSourceByName($resultSourceName,$searchOwner)
    $queryRules = $queryRuleManager.GetQueryRules($searchObjectFilter)
    
    #my variable that i normally get from xml file
    $title = "MyQueryRule7"
    $Keyword = "Helpdesk;IT"
    $PromotedResults = "1306,5897,4619"
    $Keyword = $Keyword + ";$title"
    
    CreatePromotedResult $title $Keyword $PromotedID

    Or you could check below thread which use server side object model for your reference.

    https://nelsonlamprecht.wordpress.com/2013/12/27/sharepoint-2013-search-result-types/

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Armand Degouis Tuesday, February 7, 2017 12:13 PM
    Monday, February 6, 2017 5:40 AM

All replies

  • Hi,

     

    Try this.

     

    function CreatePromotedResult([string]$title, [string]$KeyWord, [string]$PromotedResult)
    {
        $queryRule = $queryRules.CreateQueryRule($title,$null,$null,$true)
        $queryRule.CreateSourceContextCondition($resultSource)
        [string[]]$keywords = $KeyWord.split(';')		
        $keywordCondition = $queryRule.QueryConditions.CreateKeywordCondition($keywords,$false)
    	$queryAction = $queryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::CreateResultBlock)   
    	$resultBlock=$queryRule.CreateResultBlockActions[0]
    	$resultBlock.QueryTransform.OverrideProperties=New-Object Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties
    	$resultBlock.QueryTransform.OverrideProperties["RowLimit"] = 5    
        $queryAction.AlwaysShow = $true
        $queryAction.QueryTransform.QueryTemplate = "{subjectTerms}"
        $queryAction.QueryTransform.SourceId = $resultSource.Id	
        $queryRule.Update()
        $queryRule = $null	
    }
    
    #Declaration for getting query rules object
    $siteUrl = "http://sp:12001"
    $resultSourceName = "Local SharePoint Results"
    $web = Get-SPWeb $siteUrl
    $ssa = Get-SPEnterpriseSearchServiceApplication
    $queryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($ssa)
    $searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner -ArgumentList  @([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPWeb,$web)
    $searchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($searchOwner)
    $federationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($ssa)
    $resultSource = $federationManager.GetSourceByName($resultSourceName,$searchOwner)
    $queryRules = $queryRuleManager.GetQueryRules($searchObjectFilter)
    
    #my variable that i normally get from xml file
    $title = "MyQueryRule7"
    $Keyword = "Helpdesk;IT"
    $PromotedResults = "1306,5897,4619"
    $Keyword = $Keyword + ";$title"
    
    CreatePromotedResult $title $Keyword $PromotedID

    Or you could check below thread which use server side object model for your reference.

    https://nelsonlamprecht.wordpress.com/2013/12/27/sharepoint-2013-search-result-types/

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Armand Degouis Tuesday, February 7, 2017 12:13 PM
    Monday, February 6, 2017 5:40 AM
  • Hello Lee_li,

    First time I ask something on technet forum.

    Tree days later, one answer and a correct one...

    I don't know how you find this but Thank you very much ! =)


    Tuesday, February 7, 2017 12:17 PM