Programmatically accessing FAST Search admin features
Introduction
The FAST Search administration features are public with SharePoint Server 2010. All FAST admin services are accessed through the FASTAdminProxy property on the SearchServiceApplication or the SearchServiceApplicationProxy. You can among other things manage keywords and the schema. The documentation will contain examples on how to use the different contexts to perform administration tasks, so I won’t focus much on that. The examples I’ve seen do however not show how to access these contexts in a SharePoint setting, so that’s the main purpose of this blog post.
In this blog post I’ll create a console application that creates a keyword with a best bet using the KeywordContext class.
Setup development environment
Setting up Visual Studio is pretty straight forward if you have Visual Studio 2010 RC running on the same box as SharePoint.
- Create a new console application that targets x64 platform
- Add a reference to the following binaries, located in "%CommonProgramFiles%\microsoft shared\web server extensions\14\isapi\"
- Microsoft.SharePoint.dll
- Microsoft.Office.Server.Search.dll
- Microsoft.SharePoint.Search.Extended.Administration.dll
In addition, the user you’re running as must be part of the FASTSearchAdministrators group on the FAST Search for SharePoint installation you have configured.
You should now be ready to start coding.
Implementation
Below is code that gets the FASTAdminProxy’s KeywordContext and from this retrieves the search setting group for the given site collection. With a reference to the search setting group keywords can be added. What the code examples I’ve seen so far have lacked is how you get hold of the FASTAdminProxy. This is shown in the first few lines of the code: You get hold of the SearchServiceApplicationProxy associated with the site which you want to manage keywords for. When you have the SSAProxy you can get a reference to the FASTAdminProxy on this, if it’s configured with FAST Search. If the SSA is not configured with FAST Search, the FASTAdminProxy reference will be null.
Here’s the code, note that most error checking has been omitted for readability.
// Copyright © Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, https://opensource.org/licenses/ms-pl.html.)
using System;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
namespace FASTAdminOperations
{
class Program
{
static void Main(string[] args)
{
try
{
const string siteCollectionUrl = "https://localhost/fast/";
using (var site = new SPSite(siteCollectionUrl))
{
var guid = site.ID.ToString();
var ssaProxy =
(SearchServiceApplicationProxy)
SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(site));
var adminProxy = ssaProxy.FASTAdminProxy;
// If the SSA is not configured with FAST Search it will be null
if (adminProxy == null)
{
Console.Out.WriteLine("No FASTAdminProxy found for site " + siteCollectionUrl);
Environment.Exit(1);
}
// KeywordContext gives you access to the keyword functionality of FAST Search
var keywordContext = adminProxy.KeywordContext;
// get or create a search setting group
var ssgs = keywordContext.SearchSettingGroups;
var searchSettingGroup = ssgs.ContainsSearchSettingGroup(guid)
? ssgs.GetSearchSettingGroup(guid)
: ssgs.AddSearchSettingGroup(guid);
// setup keyword and best bet data
const string keywordTerm = "britney";
const string keywordDefinition = "Britney Jean Spears (born December 2, 1981) is an American singer, " +
"songwriter, dancer, actress, author and entertainer.";
const string bestBetTitle = "Britney Spears - official web site and blog";
var bestBetUri = new Uri("https://www.britneyspears.com");
const string bestBetDescription = "Welcome to the official Britney Spears website featuring Britney " +
"spears music, Britney Spears lyrics, Britney Spears photos, Britney Spears news and more.";
// fetch all keywords for this search setting group
var keywords = searchSettingGroup.Keywords;
// Let's just remove the keyword we want to add in case it exists
if (keywords.ContainsKeyword(keywordTerm))
keywords.RemoveKeyword(keywordTerm);
// Add a keyword and assign a definition
var keyword = keywords.AddKeyword(keywordTerm);
keyword.Definition = keywordDefinition;
// Associate a time limited best bet with the keyword
var bestbet = keyword.BestBets.AddBestBet(bestBetTitle, bestBetUri);
bestbet.Description = bestBetDescription;
bestbet.StartDate = DateTime.Now;
bestbet.EndDate = DateTime.Now.AddDays(7.0);
}
}
catch (Exception e)
{
Console.Out.WriteLine("Exception caught: " + e);
Environment.Exit(1);
}
}
}
}
Summary
Given you’re part of the FASTSearchAdministrators group you can programmatically access the FAST Search administration features through the FASTAdminProxy.
Comments
Anonymous
April 15, 2010
Does this code only work with the RC of SharePoint? In the beta version the FastAdminProxy doesn't have a KeywordContext property.Anonymous
April 18, 2010
That's correct, this code only works with RC.Anonymous
April 11, 2011
I have been getting a "File not found - Could not load assembly" error against the assembly "Microsoft.SharePoint.Search.Extended.Administration.dll". I have made sure my user is part of the FASTSearchAdministrators group plus the assembly is there in GAC so I don't know why I am getting this. Do you have any idea as to why this could be happening?