in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Life of a Hungarian SharePoint Geek

I've got a lot of help from the SharePoint community, so at the spring of 2007 I decided to create a blog of my own and share my SharePoint experiences.

Creating Keywords and Best Bets for MOSS Search programmatically

You can administer keywords and best bets on the MOSS admin UI at Search keywords at Site Collection Administration.

Stefan Goßner gave a code snippet about How To: create Keywords and Best Bets for MOSS Search programmatically. A similar code can be found in the very useful SharePoint Server 2007 Presentations: Enterprise Search Deep Dives presentation series, Customizing and Extending Search in Office SharePoint Server 2007.

Based on my experiments there might be a little but important problem with this code. When creating the new Keyword instance, you should use DateTime.UtcNow instead of DateTime.Now to enable the Keyword immediately:

keywords.AllKeywords.Create("myKeyword", DateTime.UtcNow);

When you create the keyword through the UI, you can specify only the date part, no hours and minutes. In this case the keyword is created with a start date (StartDate property) 0:00 AM UTC for the specified date. For example, currently our local time is GMT+1, so the start date would be 23:00 PM for the previous day.

When I used the code samples “as is”, the keywords were not displayed in the results. After one hour, the repeated search already displayed the keyword matches. When I used the DateTime.UtcNow, the results were displayed immediately. Of course, if your configured time zone is west from the GMT time line, then DateTime.Now should work also, as it is refers to a time in the past if you interpret it as UTC time.

If you try to create a best bet on the UI that refers to an URL already used in an existing best bet you cannot save the new best bet. I found another interesting behaviour of creating keywords and best bets from code. The code that creates the best bets with a common URL but different titles and descriptions will run without errors:

Keyword keyword1= keywords.AllKeywords.Create("keyword1", DateTime.UtcNow);
BestBet bestBet1 = keyword1.BestBets.Create("BestBet1", "Description1", new Uri(http://www.company.com));
keyword1.Update();
Keyword keyword2= keywords.AllKeywords.Create("keyword2", DateTime.UtcNow);
BestBet bestBet1 = keyword2.BestBets.Create("BestBet2", "Description2", new Uri(http://www.company.com));
keyword2.Update();

In the example above the best bet for keyword2 will refer to the BestBet with title BestBet1 created for keyword1.
Published Nov 13 2007, 11:42 PM by pholpar
Filed under: ,

Comments

 

SharePoint, SharePoint and stuff said:

Tipps Setting the Global Navigation Use Conditional Formatting in Data Views based on User Security Permissions

November 21, 2007 2:22 AM
 

SharePointPodcast.de said:

Direkter Download: SPPD-082-2007-11-26 Gewinnspiel: Antworten an sharepointpodcast (at) gmail.com Interview

November 26, 2007 6:55 AM
 

SharePoint, SharePoint and stuff said:

Direkter Download: SPPD-082-2007-11-26 Gewinnspiel: Antworten an sharepointpodcast (at) gmail.com Interview

November 26, 2007 6:58 AM
 

Mirrored Blogs said:

Direkter Download: SPPD-082-2007-11-26 Gewinnspiel: Antworten an sharepointpodcast (at) gmail.com Interview

January 9, 2008 6:55 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts