Skip to main content

Cloud

Creating User Contexts, Customizing User Context Properties, and Site Promotions from PowerShell (FAST for SharePoint)

PowerShell is a powerful tool when combined with FAST. When dealing with a large site collection with multitudes of taxonomies and sites underneath it, promoting sites and keeping user contexts from environment to environment can be a long and taxing job, especially when you are working with the SharePoint/FAST UI. Luckily, this is where PowerShell comes in and saves your day.
User Context Properties
In SharePoint, you can create and edit user contexts through the UI. Under Site Settings –> FAST Search User Context, you will find all of your user contexts. The page to create a User Context looks like this out of the box:
OOTBUsercontext_001
Useful page, but not really right? If you want some more depth, you will have to do some digging into User Profiles. Go to Central Administration –> Service Applications –> <Your User Profile Service Application> –> Manage User Properties. Here you will find a list of user properties that are used in SharePoint, and these will be the key to making User Contexts and Site Promotions valuable and create a rich user experience for searches.
So first, let’s add a new property to the User Context. Let’s say that the User’s “Department” is an important factor in search relevancy. Therefore we want items relevant to the User’s Department to show up first in search results. To do this, we need to execute a couple PowerShell scripts.
View Current User Context Properties
First, let’s see what User Context properties are already available. If we haven’t touched them before, then we should see two of them, “SPS-Location,SPS-Responsibility”. We will be using the SharePoint PowerShell.
 

1: $properties = Get-SPEnterpriseSearchExtendedQueryProperty -SearchApplication “FASTQuery” -Identity “FASTSearchContextProperties” 2: $properties.Value

.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode, .ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .rem
{color:#008000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .str
{color:#006080;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .op
{color:#0000c0;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .preproc
{color:#cc6633;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .html
{color:#800000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .attr
{color:#ff0000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .lnum
{color:#606060;}

.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode, .ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .rem
{color:#008000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .str
{color:#006080;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .op
{color:#0000c0;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .preproc
{color:#cc6633;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .html
{color:#800000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .attr
{color:#ff0000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .lnum
{color:#606060;}
 
Where “FASTQuery” is the <Name of your FAST Query SSA>.
beforeuc
Add User Context Property
We need to make sure that we use the Name of the User Profile Property rather than the Display Name. This is especially important if we are using custom user profile properties. Also, we will need to include the old user context properties in this script, otherwise, they will be overwritten.
 

1: Set-SPEnterpriseSearchExtendedQueryProperty -SearchApplication “FASTQuery” -Identity “FASTSearchContextProperties” -Value “SPS-Location,SPS-Responsibility,Department”

.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode, .ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .rem
{color:#008000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .str
{color:#006080;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .op
{color:#0000c0;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .preproc
{color:#cc6633;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .html
{color:#800000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .attr
{color:#ff0000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .lnum
{color:#606060;}

.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode, .ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .rem
{color:#008000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .str
{color:#006080;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .op
{color:#0000c0;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .preproc
{color:#cc6633;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .html
{color:#800000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .attr
{color:#ff0000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .lnum
{color:#606060;}
 
 
 
 
 
 
 
 
Troubleshooting: Now if you go to your UI, often you won’t see any changes (the changes will be reflected in PowerShell however)! The fastest way that I’ve seen to get these changes to show is an IIS Reset. If there is a way around this, please let us know. Therefore, type in iisreset into your PowerShell, then check the UI after it has completed. It should now look like this:
afterDepartment
pcafterdepartment
 
Add User Contexts Using PowerShell
Now that we have some new properties to play around with, let’s add some User Contexts! In the FAST PowerShell, run the command Get-FASTSearchSearchSettingGroup. This will return the FAST Search Setting Group(s) that are on the machine. There should only be one straight out of the box. We will need the Name for the next step.
GSSGName

1: $searchSettingGroup = Get-FASTSearchSearchSettingGroup -Name “69d025ce-96a7-4131-adc0-7da1603e8d24”
2: $context = $searchSettingGroup.Contexts.AddContext(“USA Marketing”) 3: $andExpression = $context.AddAndExpression() 4: $orExpression = $andExpression.AddOrExpression() 5: $orExpression.AddMatchExpression(‘SPS-Location’,‘USA’) 6: $orExpression.AddMatchExpression(‘Department’,‘Marketing’) 7: 8: # Optional not Expression (remove #) 9: # $not = andExpression.AddNotExpression() 10: # $not.AddMatchExpression(‘SPS-Location’,‘Japan’) 11: # $not.AddMatchExpression(‘SPS-Location’,‘Canada’)

.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode, .ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .rem
{color:#008000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .str
{color:#006080;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .op
{color:#0000c0;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .preproc
{color:#cc6633;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .html
{color:#800000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .attr
{color:#ff0000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .lnum
{color:#606060;}
 
This next script will add a new User Context. The true usefulness of doing this in PowerShell is the added ability to use the “not” expression to truly filter your User Context. Besides that, why is this even useful over the UI? Mainly because it’s easily portable, and you can quickly execute your PowerShell script when it’s time to move from development to production. This is handy, especially if you have multiple User Contexts, but even more so when we begin adding Site Promotions.
addeducmarket
 
Add Site Promotions Using PowerShell
Handling site promotions and demotions can be a real hassle, especially when we begin dealing with an environment with many sites and sub-sites. Having PowerShell will make this process quick and portable from a development environment to a production environment.
Here, we begin talking about the use of “boosting” relevance and using Site Promotions. Here, we will be working with a promotion without using Keywords. This could be considered something closer to “scoping”, however the search will still extend beyond the scope of the sites included in the search. PowerShell allows you to choose your boost value automatically, assign multiple websites to your boost, and select the User Context(s) to include.
 

1: # This PowerShell Script Adds Site Promotions for USA Marketing 3: 4: # Register the FASTSearch Powershell Snapin (if needed) 5: Add-PSSnapin Microsoft.FASTSearch.Powershell 6: 7: # Current Search Setting Group 8: $searchSettingGroup = Get-FASTSearchSearchSettingGroup -Name “69d025ce-96a7-4131-adc0-7da1603e8d24” 9: 10: # Type of Promotion 11: $globalPromotions = $searchSettingGroup.PromotionsWithoutKeyword 12: 13: # Promotion Name 14: $globalPromotion = $globalPromotions.AddPromotion(“USA Marketing Sites”) 15: 16: # Boost Value 17: $globalPromotion.BoostValue = 12000 18: 19: 20: ######### Begin Site Promotions Section ######### 21: 22: $sitePromotionsToBeAdded = @( 23: 24: “http://intranet/USA/”, 25: “http://intranet/marketing/”, 26: “http://intranet/marketing/USA/”, 27: “http://intranet/HR/Marketing”, 28: “http://intranet/USA/Research/Marketing/”, 29: “http://intranet/USA/Support/Marketing/” 30: 31: ) 32: 33: # loop through site collection 34: foreach ($site in $sitePromotionsToBeAdded) 35: { 36: $uri = New-Object -Typename System.Uri -ArgumentList $site 37: $globalPromotion.PromotedItems.AddPromotedLocation($uri) 38: } 39: 40: # Add Site Promotion Individually (if needed) 41: # $uri = New-Object -TypeName System.Uri –ArgumentList http://intranet/USA 42: # $globalPromotion.PromotedItems.AddPromotedLocation($uri) 43: 44: 45: ########### End Site Promotions Section ########## 46: 47: # Added User Context 48: $userContexts = ({USA Marketing}) 49: $globalPromotion.Contexts.AddContext($userContexts)

 

.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode, .ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .rem
{color:#008000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .str
{color:#006080;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .op
{color:#0000c0;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .preproc
{color:#cc6633;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .html
{color:#800000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .attr
{color:#ff0000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .lnum
{color:#606060;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode, .ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode pre
{margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .rem
{color:#008000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .str
{color:#006080;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .op
{color:#0000c0;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .preproc
{color:#cc6633;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .html
{color:#800000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .attr
{color:#ff0000;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass35C7684F8D3F46ACAC92B08CAAE6D498 .csharpcode .lnum
{color:#606060;}
PowerShell is a clean method of adding Site Promotions to specific User Contexts in FAST Search. If you check under Site Settings –> FAST Search site promotion and demotion, you should see your new site promotion with its User Context(s) attached.
sitepromotion
References: http://technet.microsoft.com/en-us/library/ff191225.aspx, http://blogs.technet.com/b/speschka/archive/2009/12/09/using-custom-properties-to-create-a-fast-search-for-sharepoint-2010-user-context.aspx

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.