Tuesday, June 15, 2010

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.

Searching by Columns in MOSS 2007

Searching by Columns in MOSS 2007

How can we search by columns in MOSS? This is a very common question from users. When we want to search by a column in announcements, document library, tasks or any kind of sharepoint lists, we need to create a managed property to map wih the column first and then we will be able to search for that column in the Advanced Search page. I will explain the steps of creating managed properties below.

Create a custom column and start a incremental crawl

  • After you create a new column, e.g. "Status", in a SharePoint list, you need to kick off a crawl. In order to have the crawler find the new column quickly, simply add an new entry to the list with some value in the new column temporarily and then start an incremental crawl. This will make the crawler to find and only crawl the new column you created very quickly.
  • After the crawl is completed, go to the "Search Settings" page of your Shared Service Provider (SSP) Admin page
  • Click on "Metadata property mapping"

Create a Managed Property and map it to a Crawled Property

  • Click on "New Managed Property" in the toolbar
  • In the new form, please enter a name for the managed property, e.g. "PropjectStatus"
  • Select the correct data type, e.g. "Text"
  • Click on "Add Mapping" to open up a "Crawled Property selection" WebPage dialog
  • Type the name of your column in the "Crawled property name" field and click "Find" button, e.g. type "Status"
  • You will see the column name showing in the "Select a crawled property" list.
  • For new column, you should select the column name without "ows_" prefix.
  • But for existing/built-in SharePoint column, you can select the column name with "ows_" prefix. Every built-in SharePoint colum has a crawled property and has the "ows_" prefix.
  • Press OK to complete the form.
  • Start another full crawl. This crawl will map column value/data to the managed property for faster searching.

Modify the Advanced Search page

In order to search for the column from the UI, you need to add an new entity into the property dropdown in the Advanced Search page.

  • Go to the Search Center page of your portal
  • Click on "Advanced Search"
  • Click on "Site Action" and select "Edit Page"
  • Click on "Edit" in the Advanced Search box and select "Modify Shared Web Part"
  • On the right pane, find the XML text box under Properties section.
  • Copy and paste the XML text into notepad to edit the XML.
  • Make the following modification:
    • In the node, please add , e.g. , where DisplayName attribute is to specify the name showed in the property dropdown.
    • In one of the nodes, please add , e.g.
  • Copy and paste the XML text from notepad back into the XML text box
  • Click OK.
  • Check if the dropdown menu has the new property added

Test the Managed Property

  • Go to the "Advanced Search" page
  • Select the Result Type you added the property
  • In the "Pick Property" dropdown, select the managed property you newly added.
  • Type some value, e.g the value you temporarily entered in the very first step
  • Click Search
If you have any questions, send me an email at anand.chandawarkar@gmail.com