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....");

        }

    }

}

Comments

  • Anonymous
    February 28, 2010
    Thank you to share knowledge

  • Anonymous
    November 01, 2010
    The blog provides helpful information regarding the topic and it also gives a vast knowledge as well which helps us in our studies and in practical life.

  • Anonymous
    November 03, 2010
    The blog provides helpful information regarding the topic and it also gives a vast knowledge as well which helps us in our studies and in practical life.

  • Anonymous
    November 08, 2010
    Indeed a very good read! Very informative post with pretty good insight on all aspects of the topic! Will keep visiting in future too!

  • Anonymous
    November 22, 2010
    I would like to appreciate the work of blog author that the person provided us with an extremely excellent information regarding the topic. I really learned something from this blog and started to contribute my ideas via commenting on this blog. Keep it up!

  • Anonymous
    December 01, 2010
    What is th format to Import the best bets? Do you have any other program for 2010. Thanks, Rao

  • Anonymous
    February 16, 2011
    The blog provides helpful information regarding the topic and it also gives a vast knowledge as well which helps us in our studies and in practical life.

  • Anonymous
    February 16, 2011
    It's really glad to read about the following topic which has enhanced my knowledge regarding topic and plus has given alot of ideas which I can think on. So I would say thank you to the blog owner for providing this amazing information.

  • Anonymous
    April 28, 2011
    Indeed a very good read! Very informative post with pretty good insight on all aspects of the topic! Will keep visiting in future too!

  • Anonymous
    June 27, 2011
    Indeed a very good read! Very informative post with pretty good insight on all aspects of the topic! Will keep visiting in future too!