MOSS 2007 – backup and restore keywords and best bets

If you are using standard stsadm backup and restore process, you must have seen keywords and best bets do not carry forward with the backup content. The reason is simple, although you configure keywords and best bets at the site collection level, but the data get stored in the content db of the SSP. So if you take the backup of your site collection it only takes the backup from the content db of the web application it belongs to. Here is couple of console applications to take a backup of keywords and best bets in xml format and you can restore it once again. Run the command “ConsoleAppBestBetExporter <url of your source site collection>” to take a beckup as keywords.xml. Then port the keywords.xml to the destination server and then run “ConsoleAppBestBetImporter <url of your destination site collection>”.

using System;

using System.Collections.Generic;

using Microsoft.SharePoint;

using Microsoft.Office.Server.Search.Administration;

using System.Text;

using System.IO;

using System.Xml;

namespace ConsoleAppBestBetExporter

{

    class Program

    {

        static void Main(string[] args)

        {

            using (SPSite site = new SPSite(args[0]))

            {

                SPWeb rootweb = site.OpenWeb();

                Keywords keywords = new Keywords(SearchContext.GetContext(site), new Uri(rootweb.Url));

               

                KeywordCollection keyCol = keywords.AllKeywords;

                XmlTextWriter textWriter = new XmlTextWriter(@"C:\keywords.xml", null);

                textWriter.WriteStartDocument();

                textWriter.WriteStartElement("keywords");

                foreach (Keyword kwd in keyCol)

                {

                    string synm = "";

                    foreach (Synonym snm in kwd.Synonyms)

     {

                        synm += snm.Term + "#";

                    }

                    textWriter.WriteStartElement("keyword");

                    textWriter.WriteStartElement("term");

                    textWriter.WriteString(kwd.Term);

                    textWriter.WriteEndElement();

                    textWriter.WriteStartElement("synonyms");

                    textWriter.WriteString(synm);

                    textWriter.WriteEndElement();

                    textWriter.WriteStartElement("contact");

                    textWriter.WriteString(kwd.Contact);

                    textWriter.WriteEndElement();

                    textWriter.WriteStartElement("startdate");

                    textWriter.WriteString(kwd.StartDate.ToString());

                    textWriter.WriteEndElement();

                    textWriter.WriteStartElement("reviewdate");

                    textWriter.WriteString(kwd.ReviewDate.ToString());

  textWriter.WriteEndElement();

                    textWriter.WriteStartElement("enddate");

                    textWriter.WriteString(kwd.EndDate.ToString());

                    textWriter.WriteEndElement();

                    textWriter.WriteStartElement("definition");

                    textWriter.WriteString(kwd.Definition);

                    textWriter.WriteEndElement();

                    textWriter.WriteStartElement("bestbets");

                    foreach (BestBet bbt in kwd.BestBets)

                    {

                        textWriter.WriteStartElement("bestbet");

                        string title = bbt.Title.ToString();

                        string url = bbt.Url.ToString();

                        string desc = bbt.Description.ToString();

                        textWriter.WriteStartElement("title");

                        textWriter.WriteString(title);

                        textWriter.WriteEndElement();

                        textWriter.WriteStartElement("url");

                        textWriter.WriteString(url);

                        textWriter.WriteEndElement();

                        textWriter.WriteStartElement("description");

                        textWriter.WriteString(desc);

             textWriter.WriteEndElement();

                        textWriter.WriteEndElement();

                    }

                    textWriter.WriteEndElement();

                    textWriter.WriteEndElement();

                    rootweb.Dispose();

                }

                textWriter.WriteEndElement();

                textWriter.WriteEndDocument();

                textWriter.Close();

            }

            Console.WriteLine("Export Completed....");

        }

    }

}

using System;

using System.Collections.Generic;

using Microsoft.SharePoint;

using Microsoft.Office.Server.Search.Administration;

using System.Text;

using System.IO;

using System.Xml;

namespace ConsoleAppBestBetImporter

{

    class Program

    {

        static void Main(string[] args)

        {

            using (SPSite site = new SPSite(args[0]))

            {

                SPWeb rootweb = site.OpenWeb();

                Keywords keywords = new Keywords(SearchContext.GetContext(site), new Uri(rootweb.Url));

                KeywordCollection keyCol = keywords.AllKeywords;

                List<Keyword> toDelete = new List<Keyword>();

                foreach (Keyword kwd in keyCol)

                {

                    toDelete.Add(kwd);

                }

      foreach (Keyword kwd2 in toDelete)

                {

                    kwd2.Delete();

                }

                XmlDocument doc = new XmlDocument();

                doc.Load(@"C:\keywords.xml");

                XmlElement root = doc.DocumentElement;

                XmlNodeList nodes = root.SelectNodes("keyword");

                foreach (XmlNode node in nodes)

                {

                    string term = node["term"].InnerText;

                    string[] synonyms = node["synonyms"].InnerText.Split('#');

                    string contact = node["contact"].InnerText;

                    string startdate = node["startdate"].InnerText;

                    string reviewdate = node["reviewdate"].InnerText;

                   string enddate = node["enddate"].InnerText;

                    string definition = node["definition"].InnerText;

                    Keyword keyword = keywords.AllKeywords.Create(term, Convert.ToDateTime(startdate));

                    keyword.ReviewDate = Convert.ToDateTime(reviewdate);

                    keyword.EndDate = Convert.ToDateTime(enddate);

                    keyword.Definition = definition;

                    keyword.Contact = contact;

                    for (int i = 0; i < synonyms.Length - 1; i++)

                    {

                        keyword.Synonyms.Create(synonyms[i]);

                    }

                    XmlNodeList bbNodes = node["bestbets"].SelectNodes("bestbet");

                    foreach (XmlNode bbNode in bbNodes)

                    {

                        Uri bbUri = new Uri(bbNode["url"].InnerText);

                        keyword.BestBets.Create(bbNode["title"].InnerText, bbNode["description"].InnerText, bbUri);

                    }

                    keyword.Update();

                }

                rootweb.Dispose();

            }

            Console.WriteLine("Complete....");

        }

    }

}