SharePoint basic comparison tool

2

December 23, 2013 by Ravi C Khambhati

Let me tell you that this is very basic comparison tool which will compare below artifacts and generates XML report with the difference,

1. List schema
2. Document Library Schema
3. Document Library items(root level)
4. Feature

Let me know your feedback and will add more functionality to this tool.

You can keep yourself updated on CodePlex as well,

https://sharepointcomparisontool.codeplex.com/

class Program
{
    enum SharePointArtifactType
    {
        DocumentLibrary,
        List,
        Feature
    }

    static void Main(string[] args)
    {
        try
        {
            if (args.Count() != 2)
            {
                throw new Exception("Missing parameter");
            }

            SharePointArtifactType[] artifact = new SharePointArtifactType[] {
                SharePointArtifactType.DocumentLibrary,
                SharePointArtifactType.List,
                SharePointArtifactType.Feature
            };

            string logFolderPath = Path.Combine(Environment.CurrentDirectory, "Log");
            if (!Directory.Exists(logFolderPath))
            {
                Directory.CreateDirectory(logFolderPath);
            }

            CompareWebsite(args[0], args[1], artifact, logFolderPath);
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine("=================================================================");
            Console.WriteLine(ex.ToString());
            Console.WriteLine("=================================================================");
        }

        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine();
        Console.WriteLine("Press any key to continue...");
        Console.ReadLine();
    }

    static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
    {
        try
        {
            e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
        }
        catch
        {
            throw;
        }
    }

    private static void CompareWebsite(string url1, string url2, SharePointArtifactType[] artifacts, string logFolderPath)
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("=================================================================");
        Console.WriteLine(string.Format("Url1: {0}", url1));
        Console.WriteLine(string.Format("Url2: {0}", url2));

        foreach (SharePointArtifactType artifact in artifacts)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("=================================================================");
            Console.WriteLine(string.Format("Comparing {0}...", artifact));
            Console.WriteLine("=================================================================");

            string fileName = string.Format("{0}_{1}.xml", artifact, DateTime.Now.ToString("yyyy-MM-dd_hhmmss"));
            fileName = Path.Combine(logFolderPath, fileName);

            WriteToFile(fileName, "<!--?xml version=\"1.0\" encoding=\"utf-8\" ?-->");

            WriteToFile(fileName, string.Format("<{0}s>", artifact));
            CompareWebsiteArtifact(url1, url2, artifact, fileName);
            WriteToFile(fileName, string.Format("", artifact));
        }
    }

    private static void CompareWebsiteArtifact(string url1, string url2, SharePointArtifactType artifact, string fileName)
    {
        using (SPSite spSite = new SPSite(url1))
        {
            using (SPWeb spWeb = spSite.OpenWeb())
            {
                foreach (SPList spList in spWeb.Lists)
                {
                    if (!spList.Hidden)
                    {
                        switch (artifact)
                        {
                            case SharePointArtifactType.DocumentLibrary:
                                if (spList is SPDocumentLibrary)
                                {
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(string.Format("Comparing {0}...", spList.RootFolder.Url));

                                    CompareDocumentLibrary(url2, spList, fileName);
                                }
                                break;
                            case SharePointArtifactType.List:
                                if (spList is SPDocumentLibrary)
                                {

                                }
                                else
                                {
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(string.Format("Comparing {0}...", spList.RootFolder.Url));

                                    CompareList(url2, spList, fileName);
                                }
                                break;
                        }
                    }
                }

                switch (artifact)
                {
                    case SharePointArtifactType.Feature:
                        CompareFeature(spWeb, url2, fileName);
                        break;
                }
            }
        }
    }

    private static void CompareFeature(SPWeb spWeb, string url2, string fileName)
    {
        ClientContext clientContext = new ClientContext(url2);
        clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest;
        FeatureCollection features = clientContext.Web.Features;
        clientContext.Load(features);
        clientContext.ExecuteQuery();

        foreach (SPFeature feature in spWeb.Features)
        {
            Feature searchResult = features.FirstOrDefault(f => f.DefinitionId == feature.DefinitionId);

            if (searchResult == null)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("MISSING FEATURE:");

                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine(string.Format("{0}:{1}", feature.Definition.DisplayName, feature.DefinitionId));

                WriteToFile(fileName, string.Format("{0}{1}", feature.Definition.DisplayName, feature.DefinitionId));
            }
        }
    }

    private static void CompareList(string url2, SPList spList, string fileName)
    {
        bool listFound = false;
        List foundList = null;

        ClientContext clientContext = new ClientContext(url2);
        clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest;
        Web oWebsite = clientContext.Web;
        ListCollection collList = oWebsite.Lists;
        clientContext.Load(collList);
        clientContext.ExecuteQuery();

        foreach (List oList in collList)
        {
            if (string.Compare(oList.Title, spList.Title, true) == 0)
            {
                listFound = true;
                foundList = oList;
                break;
            }
        }

        if (!listFound)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("MISSING LIST:");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine(spList.RootFolder);

            WriteToFile(fileName, string.Format("", spList.RootFolder));
        }
        else
        {
            string columnDifference = CompareColumns(clientContext, spList, foundList, fileName);

            if (!string.IsNullOrEmpty(columnDifference))
            {
                WriteToFile(fileName, string.Format("{1}", spList.RootFolder, columnDifference));
            }
        }
    }

    private static void CompareDocumentLibrary(string url2, SPList spList, string fileName)
    {
        bool listFound = false;
        List foundList = null;

        ClientContext clientContext = new ClientContext(url2);
        clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest;
        Web oWebsite = clientContext.Web;
        ListCollection collList = oWebsite.Lists;
        clientContext.Load(collList);
        clientContext.ExecuteQuery();

        foreach (List oList in collList)
        {
            if (string.Compare(oList.Title, spList.Title, true) == 0)
            {
                listFound = true;
                foundList = oList;
                break;
            }
        }

        if (!listFound)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("MISSING DOCUMENTLIBRARY:");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine(spList.RootFolder);

            WriteToFile(fileName, string.Format("", spList.RootFolder));
        }
        else
        {
            StringBuilder content = new StringBuilder();

            foreach (SPListItem spListItem in spList.GetItems(new SPQuery()))
            {
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = string.Format("" +
                                                    "" +
                                                        "" +
                                                            "" +
                                                                "" +
                                                                "{0}" +
                                                            "" +
                                                        "" +
                                                    "" +
                                                "", spListItem.Name);

                ListItemCollection collListItem = foundList.GetItems(camlQuery);
                clientContext.Load(collListItem);
                clientContext.ExecuteQuery();

                if (collListItem.Count <= 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("MISSING ITEM:");

                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine(string.Format("{0}:{1}", spList.RootFolder, spListItem.Name));

                    content.AppendFormat("", spListItem.Name);
                }
            }

            if (content.Length > 0)
            {
                content = new StringBuilder(string.Format("{0}", content));
            }

            string columnDifference = CompareColumns(clientContext, spList, foundList, fileName);

            if (!string.IsNullOrEmpty(columnDifference))
            {
                content.Append(columnDifference);
            }

            if (content.Length > 0)
            {
                WriteToFile(fileName, string.Format("{1}", spList.RootFolder, content.ToString()));
            }
        }
    }

    private static string CompareColumns(ClientContext clientContext, SPList spList, List foundList, string fileName)
    {
        FieldCollection fields = foundList.Fields;
        clientContext.Load(fields);
        clientContext.ExecuteQuery();

        StringBuilder content = new StringBuilder();

        foreach (SPField spField in spList.Fields)
        {
            if (!spField.Hidden)
            {
                Field searchResult = fields.SingleOrDefault(f => f.InternalName == spField.InternalName);

                if (searchResult == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("MISSING COLUMN:");

                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine(string.Format("{0}.{1}", spList.RootFolder, spField.InternalName));
                    Console.WriteLine(string.Format("SCHEMA:{0}", spField.SchemaXml));

                    content.AppendFormat("{1}", spField.InternalName, spField.SchemaXml);
                }
                else
                {
                    if (string.Compare(searchResult.TypeAsString, spField.TypeAsString, true) != 0)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("MISSMATCH COLUMN TYPE:");

                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine(string.Format("{0}:{1}", spList.RootFolder, spField.InternalName, spField.SchemaXml));

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("OLD SCHEMA:");

                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine(searchResult.SchemaXml);

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("NEW SCHEMA:");

                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine(spField.SchemaXml);

                        content.AppendFormat("{1}{2}", spField.InternalName, searchResult.SchemaXml, spField.SchemaXml);
                    }
                }
            }
        }

        if (content.Length > 0)
        {
            return string.Format("{0}", content);
        }
        else
        {
            return string.Empty;
        }
    }

    private static void WriteToFile(string fileName, string content)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName, true))
        {
            file.WriteLine(content);
        }
    }
}

Google+

Advertisements

2 thoughts on “SharePoint basic comparison tool

  1. manish patil says:

    Good work and good tool too 🙂
    Best Regards,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: