in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Sharepoint Space

It is all about Sharepoint

export Keywords/Best Bet for Sharepoint 2007

Sharepoint 2007 save Keywords and Best Bet to the SSP_DB database rather than the content database. If you use stsadm -o backup/restore to perform backup/restore, you will quickly find it does not work for Keywords and Best Bet. My solution to address this issue is to export/import those settings by extending stsadm command. Here is the article of how to extend stsadm from MSDN. http://msdn.microsoft.com/en-us/library/bb417382.aspx

Here is my keywords/best bet xml format:
<?xml version="1.0" encoding="UTF-8"?>
<Keywords>
 <Keyword>
  <Term>Test</Term>
  <Synonyms DefaultSeperator=";">
   <Synonym>test<Synonm>
   <Synonym>TEST<Synonm>
  </Synonyms>
  <Definition></Definition>
  <Contact></Contact>
  <StartDate></StartDate>
  <EndDate></EndDate>
  <ReviewDate></ReviewDate>
  <BestBets>
   <BestBet>
    <Title></Title>
    <Url></Url>
    <Description></Description>
   </BestBet>
   <BestBet>
    <Title></Title>
    <Url></Url>
    <Description></Description>
   </BestBet>
  <BestBets>
 </Keyword>
</Keywords>

Here is the code to export

        private int ExportKeywords(StringDictionary keyValues, out string output)
        {
            if (!keyValues.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url parameter was not specified.");
            }
            if (!keyValues.ContainsKey("filename"))
            {
                throw new InvalidOperationException("The filename parameter was not specified.");
            }

            String url = keyValues["url"];
            string filename = keyValues["filename"];

            try
            {
                using (SPSite site = new SPSite(url))
                {
                    //get keyword
                    SearchContext searchContext = SearchContext.GetContext(site);
                    Keywords keywords = new Keywords(searchContext, new Uri(url));
                    //write to xml
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                    xmlWriter.WriteStartElement("Keywords");
                    xmlWriter.Close();
                    xmlDoc.Load(filename);
                    foreach (Keyword thisKeyword in keywords.AllKeywords)
                    {
                        XmlNode root = xmlDoc.DocumentElement;
                        XmlElement node_keyword = xmlDoc.CreateElement("Keyword");
                        root.AppendChild(node_keyword);

                        CreateElement("Term", thisKeyword.Term, node_keyword, xmlDoc);

                        XmlElement node_Synonyms = xmlDoc.CreateElement("Synonyms");
                        node_keyword.AppendChild(node_Synonyms);
                        foreach (Synonym syn in thisKeyword.Synonyms)
                            CreateElement("Synonym", syn.Term, node_Synonyms, xmlDoc);
                       
                        CreateElement("Definition", thisKeyword.Definition, node_keyword, xmlDoc);
                       
                        CreateElement("Contact", thisKeyword.Contact, node_keyword, xmlDoc);
                       
                        if (thisKeyword.StartDate.Date !=DateTime.MaxValue.Date)
                            CreateElement("StartDate", thisKeyword.StartDate.ToShortDateString(), node_keyword, xmlDoc);
                        else
                            CreateElement("StartDate", "", node_keyword, xmlDoc);
                       
                        if (thisKeyword.EndDate.Date != DateTime.MaxValue.Date)
                            CreateElement("EndDate", thisKeyword.EndDate.ToShortDateString(), node_keyword, xmlDoc);
                        else
                            CreateElement("EndDate", "", node_keyword, xmlDoc);
                       
                        if (thisKeyword.ReviewDate.Date != DateTime.MaxValue.Date)
                            CreateElement("ReviewDate", thisKeyword.ReviewDate.ToShortDateString(), node_keyword, xmlDoc);
                        else
                            CreateElement("ReviewDate", "", node_keyword, xmlDoc);
                       
                        XmlElement node_BestBets = xmlDoc.CreateElement("BestBets");
                        node_keyword.AppendChild(node_BestBets);
                        foreach (BestBet bestBet in thisKeyword.BestBets)
                        {
                            XmlElement node_BestBet = xmlDoc.CreateElement("BestBet");
                            node_BestBets.AppendChild(node_BestBet);
                            CreateElement("Title", bestBet.Title, node_BestBet, xmlDoc);
                            CreateElement("Url", bestBet.Url.ToString(), node_BestBet, xmlDoc);
                            CreateElement("Description", bestBet.Description, node_BestBet, xmlDoc);
                        }
                    }
                    xmlDoc.Save(filename);
                }
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Error retrieving url '" + url + "'.  Please check the format of your url, and ensure that the site exists.  Details: " + e.Message);
            }

            StringBuilder sb = new StringBuilder();

            output = "";
            return 0;
        }

        private void CreateElement(string elementName, string elementValue, XmlElement parentElement, XmlDocument xmlDoc)
        {
            XmlElement newElement = xmlDoc.CreateElement(elementName);
            XmlText newElementText = xmlDoc.CreateTextNode("");
            newElementText.Value = elementValue;
            parentElement.AppendChild(newElement);
            newElement.AppendChild(newElementText);
        }

After deploy the dll and config the xml, you could export Keyword/bestbet using following command

stsadm -o exportkeywords -url http://yoursite -filename c:\test.xml

This method can also used to export/import search scope, site settings etc.

Comments

No Comments

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