none
Creating multi-keyword Promoted Result with PowerShell RRS feed

  • Question

  • Hi,

    I need to import over 200 Promoted Results ("Best bets") into SharePoint's search center so I've been working for doing this with PowerShell using CSV file as source. My script is pretty much based on information collected from these two sites with some modifications.

    http://blog.sharepointsite.co.uk/2014/08/powershell-to-create-and-remove.html
    http://sharepoint-community.net/profiles/blogs/sharepoint-2013-manage-search-query-rule-using-powershell

    Closing to finalizing this but encountered issues when there are multiple trigger keywords for the rule as it seems the list of keywords is treated as one single keyword until manually editing and saving through Query Rules in Site Settings.

    So for example keyword string "workstation; computer; laptop" does not trigger in search by just with "workstation" but with whole string as is in the rule condition. It seems there is some additional parsing action which the UI does when saving through browser, just opening edit and saving without changing anything gets it working.

    Most of the promoted results have more than one keyword so it would be nice to get this working smoothly without need to go separately edit'n'save them through UI.

    Here's the function used to create the query rule

    function CreatePromotedResult([string]$title, [string[]]$keywords, [string]$url, [string]$description)
    {
    	Write-Host "Creating Promoted Result .." $title
            $queryRule = $queryRules.CreateQueryRule($title,$null,$null,$true)
            $keywordCondition = $queryRule.QueryConditions.CreateKeywordCondition($keywords,$true) # Set conditions
            $queryRule.QueryConditions[0].MatchingOptions = “FullQuery,ProperPrefix,ProperSuffix” # Set selections for query rule matching
            $queryAction = $queryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::AssignBestBet)
            $QueryRule.CreateSourceContextCondition($ResultSource)
    		
            try {
                $promotedResult = $promotedResultCollection.CreateBestBet($title, $url, $description, $false)
                #Map the best bet to the query action
                $queryAction.BestBetIds.Add($promotedResult.Id);
                #Update the query rule
                $queryRule.Update()
            }
            catch { # catch errors, mostly for already existing rules
                Write-Host "An error occurred when creating promoted result" -ForegroundColor Red
                Write-Host "  -->" $_.Exception.Message -ForegroundColor Red
            }
    
            $queryRule = $null	
    }


    Full script can be found here http://1drv.ms/1ntSAWd

    So any ideas how to trigger the keyword parsing so that search would treat each keyword as its own?


    • Edited by Kim Blomberg Monday, January 25, 2016 1:45 PM Hyperlinking links
    Monday, January 25, 2016 1:41 PM

All replies

  • In practical terms you could filter those out and import them manually. It's a rubbish solution but it does work.

    For making it work you can create a multi keyword term through the GUI then grab it in PowerShell and try to identify the way it's been built.

    Monday, January 25, 2016 1:51 PM
  • As stated, most of the promotions are multi-keyword so if I'd go adding those manually there would not be much need for the whole script.

    Okay, it is probably slightly faster to dump the rules into SP with this and then go click through them through UI (less manual copy-pasting) but still the goal is to minimize manual work as I may need to do these again in future.

    Monday, January 25, 2016 1:56 PM
  • Just pass your keywords as one string to your function like "keyTerm1;keyTerm2;keyTerm3".

    Then in your function split the string into an Array

    [string[]]$keywords=$keywordsstring.split(‘;’

    and then pass it to

    $queryRule.QueryConditions.CreateKeywordCondition($keywords,$true) function.

    Friday, December 16, 2016 10:47 AM