Wednesday, 9 September 2009

Problem adding Keywords and Best Bets programmatically in MOSS 2007

I was writing a small Windows application for a customer to create keywords and best bets using a CSV file and experienced a weird problem. I used the code published here by Stefan Goßner as a starting point, which successfully added the keywords to the Manage Keywords UI in Site Collection Administration, but when I tried to search on one of the keywords, no best bets were shown in the search results.

The format of the CSV file was, as follows:

Keyword name, Synonyms (semi-colon delimited), Best bet URL

For example: Acme Primary School, Acme;Acme School;Acme Primary, http://www.acme.com

The code I used to create the keywords was:

keywords.AllKeywords.Create(keywordTerm, DateTime.Today.AddHours(-1));
keywords.AllKeywords[keywordTerm].Synonyms.Create(objSynonyms);
keywords.AllKeywords[keywordTerm].BestBets.Create(objBestBet.ToString() + " Profile", "Full school profile for " + objBestBet.ToString(), new Uri(objUrl.ToString()));
keywords.AllKeywords[keywordTerm].Update();

I then found that if I clicked on one of these keywords in the Manage Keywords UI and clicked OK without changing anything, the search for that keyword worked. I eventually discovered that SharePoint didn’t like adding the semi-colon delimited synonyms as a single string and that they needed to be created separately. Therefore, I changed the code to below, using the semi-colons to split the string up into an array of separate synonyms, which worked:

keywords.AllKeywords.Create(keywordTerm, DateTime.Today.AddHours(-1));
//Create synonyms from semi-colon separated array
string[] synonymArray = objSynonyms.ToString().Split(new char[] { ';' });
foreach (string synonym in synonymArray)
{
     keywords.AllKeywords[keywordTerm].Synonyms.Create(synonym);
}
keywords.AllKeywords[keywordTerm].BestBets.Create(objBestBet.ToString() + " Profile", "Full school profile for " + objBestBet.ToString(), new Uri(objUrl.ToString()));
keywords.AllKeywords[keywordTerm].Update();

12 comments:

  1. Hi Phil,

    I too face the same problem.
    I have added only Keyword, definition & best bet but still i am not getting keyword in search.
    then if i simply edit the keyword in UI & then clickin OK, now keyword is displayed in serach.

    Kindly let me know how to solve this issue bcoz u have mentioned that u have seperated Synonyms by semicolon";" but i havn't added

    ReplyDelete
  2. Hi,

    Have you updated your keywords after the changes (see last line in my code above)? The problem I was having was with synonyms, so if you haven't got any then it is probably a different issue to the one I had.

    Phil

    ReplyDelete
  3. Can you post a "step-by-step" for doing this with a CSV for SharePoint / FAST 2010? I'm starting to loose all my hair on this ;) Any help would be greatly appreciated.

    Thanks!

    ReplyDelete
  4. Hi Bryan - I have posted a reply to this on my PowerShell blog at http://get-spscripts.com/2010/10/create-search-keywords-and-best-bets.html

    Phil

    ReplyDelete
  5. https://www.facebook.com/bantalsilikongrosir
    http://bantalsilikon01.blogspot.com/2014/12/a.html
    http://jualsangkarpleci.blogspot.com/2014/12/a.html
    http://kopiluwakliar01.blogspot.com/2014/12/a.html
    http://vvty.in/uncategorized/marinir-seo/

    http://marinirseo.blogspot.com/2014/12/a.html
    http://marinir-seo.blogspot.com/2014/12/a.html
    http://bumbu-pecel-malang.blogspot.com/2014/12/a.html
    http://distro-seo.blogspot.com/2014/12/a.html
    http://restoran-seo.blogspot.com/2014/12/a.html
    http://bantalmalangmurah.blogspot.com/2014/12/a.html
    http://distrobantal.blogspot.com/2014/12/a.html
    http://kesethandukmalang.blogspot.com/2014/12/a.html


    http://tasya.marinirseo.web.id/?p=4http://jeannet.marinirseo.web.id/?p=4http://anne.marinirseo.web.id/?p=4http://jelita.marinirseo.web.id/?p=4http://ruth.marinirseo.web.id/?p=4http://caca.marinirseo.web.id/?p=4http://brenda.marinirseo.web.id/?p=1 || http://tasya1.marinirseo.web.id/?p=4http://jeannet1.marinirseo.web.id/?p=4http://anne1.marinirseo.web.id/?p=4http://jelita1.marinirseo.web.id/?p=4http://ruth1.marinirseo.web.id/?p=4http://caca1.marinirseo.web.id/?p=5http://brenda1.marinirseo.web.id/?p=4 || http://tasya2.marinirseo.web.id/?p=5http://jeannet2.marinirseo.web.id/?p=4http://anne2.marinirseo.web.id/?p=4http://jelita2.marinirseo.web.id/?p=4http://ruth2.marinirseo.web.id/?p=4http://caca2.marinirseo.web.id/?p=4http://brenda2.marinirseo.web.id/?p=4http://brenda3.marinirseo.web.id/?p=4http://tasya3.marinirseo.web.id/dua/http://jeannet3.marinirseo.web.id/?p=4http://anne3.marinirseo.web.id/?p=4http://jelita3.marinirseo.web.id/?p=4http://ruth3.marinirseo.web.id/?p=4http://caca3.marinirseo.web.id/?p=4 || nhttp://brenda4.marinirseo.web.id/?p=4http://tasya4.marinirseo.web.id/?p=4http://jeannet4.marinirseo.web.id/?p=4http://anne4.marinirseo.web.id/?p=4http://jelita4.marinirseo.web.id/?p=5http://ruth4.marinirseo.web.id/?p=4http://caca4.marinirseo.web.id/?p=4 || http://tasya5.marinirseo.web.id/?p=4http://anne5.marinirseo.web.id/?p=4http://jelita5.marinirseo.web.id/?p=4http://ruth5.marinirseo.web.id/?p=4http://caca5.marinirseo.web.id/?p=4http://synganne.marinirseo.web.id/?p=4
    http://tasya.marinirseo.web.id/?p=4http://jeannet.marinirseo.web.id/?p=4http://anne.marinirseo.web.id/?p=4http://jelita.marinirseo.web.id/?p=4http://ruth.marinirseo.web.id/?p=4http://caca.marinirseo.web.id/?p=4http://brenda.marinirseo.web.id/?p=1 || http://tasya1.marinirseo.web.id/?p=4http://jeannet1.marinirseo.web.id/?p=4http://anne1.marinirseo.web.id/?p=4http://jelita1.marinirseo.web.id/?p=4http://ruth1.marinirseo.web.id/?p=4http://caca1.marinirseo.web.id/?p=5http://brenda1.marinirseo.web.id/?p=4 || http://tasya2.marinirseo.web.id/?p=5http://jeannet2.marinirseo.web.id/?p=4http://anne2.marinirseo.web.id/?p=4http://jelita2.marinirseo.web.id/?p=4http://ruth2.marinirseo.web.id/?p=4http://caca2.marinirseo.web.id/?p=4http://brenda2.marinirseo.web.id/?p=4http://brenda3.marinirseo.web.id/?p=4http://tasya3.marinirseo.web.id/dua/http://jeannet3.marinirseo.web.id/?p=4http://anne3.marinirseo.web.id/?p=4http://jelita3.marinirseo.web.id/?p=4http://ruth3.marinirseo.web.id/?p=4http://caca3.marinirseo.web.id/?p=4 || nhttp://brenda4.marinirseo.web.id/?p=4http://tasya4.marinirseo.web.id/?p=4http://jeannet4.marinirseo.web.id/?p=4http://anne4.marinirseo.web.id/?p=4http://jelita4.marinirseo.web.id/?p=5http://ruth4.marinirseo.web.id/?p=4http://caca4.marinirseo.web.id/?p=4 || http://tasya5.marinirseo.web.id/?p=4http://anne5.marinirseo.web.id/?p=4http://jelita5.marinirseo.web.id/?p=4http://ruth5.marinirseo.web.id/?p=4http://caca5.marinirseo.web.id/?p=4http://synganne.marinirseo.web.id/?p=4

    ReplyDelete