SPDiffUtility: An excellent class to find differences in strings with HTML highlighting

While exploring Sharepoint utility classes, I found an excellent class which deserve more credit then it currently enjoys. It is SPDiffUtility. This class has only one static method called Diff which determines the text differences between two strings. It takes three parameters:

  1. str1: The original string
  2. str2: The changed string
  3. maxDifferences: A 32-bit integer representing the maximum number of differences to find.

The beauty of this function lies in its return value which is a merged version of the specified strings that contain HTML tags indicating inserted, deleted, or changed text.

This returned string contains HTML tags to highlight the changes, insertions and deletions. The best part is that you can set these tags with your HTML tags if you want. Following is the sample usage of this class.

  1. Create a web site project in Viusal Studio
  2. Add reference to Microsoft.Sharepoint.dll found in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll
  3. Paste the following code in Default.aspx
  4. <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>

    <head runat=”server”>
    <title>Untitled Page</title>
    </head>

    <style type=”text/css”>
    .ms-diffdeletenostrike {
    BACKGROUND-COLOR: #e0e0e0;
    }
    .ms-diffdelete {
    BACKGROUND-COLOR: #e0e0e0;
    }
    .ms-diffdelete {
    TEXT-DECORATION: line-through;
    }
    .ms-diffinsert {
    BORDER-RIGHT: #ffffff 1px solid;
    BORDER-TOP: #ffffff 1px solid;
    = BORDER-LEFT: #ffffff 1px solid;
    BORDER-BOTTOM: #ffffff 1px solid;
    = BACKGROUND-COLOR: #ffeaad;
    }
    </style>
    <body>

    <form id=”form1″ runat=”server”>
    <div>
    <asp:Label ID=”lblResult” runat=”server” Text=””></asp:Label>
    </div>
    </form>

    </body>

    </html>


  5. Open Default.aspx.cs and paste the following code

  6. using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using Microsoft.SharePoint.Utilities;
    using System.Diagnostics;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    string firstString = “This is an initial string”;
    string secondString = “This”;

    //Set the tags
    SPDiffUtility.ChangeOpenTag = ““;
    SPDiffUtility.ChangeCloseTag = “
    “;
    SPDiffUtility.InsertOpenTag = ““;
    SPDiffUtility.InsertCloseTag = “
    “;
    SPDiffUtility.DeleteOpenTag = ““;
    SPDiffUtility.DeleteCloseTag = “
    “;

    //end setting tags

    //Find the difference
    string diffString = SPDiffUtility.Diff(firstString, secondString, Int32.MaxValue);
    lblResult.Text = diffString;

    }
    }

  7. Run web application by pressing Ctrl+F5

You will see “This is an intial string where striked out letters are those which have been deleted in the second string.

Share

3 thoughts on “SPDiffUtility: An excellent class to find differences in strings with HTML highlighting”

  1. really good way to see the difference. whatif we want to have similar functionality in normal c# code. (not in a sharepoint project)

  2. 2 things:

    1) Comment out the code block that sets the tags to empty strings, the defaults are what you need and this example will override them to do nothing. Also – if you copy and paste, check the ” characters and rogue = characters.

    2) I’ve had difficulty using this to get insertions AND deletions to highlight. It seems to work for specific cases only. i.e only insert OR delete. I think maybe that the web control that uses this (VersionDiff, VersionDiffIterator) must have a specific algorithm to get the output we all have seen from SharePoint page diff’ing. Anyone using this to get insertions AND deletions from 2 strings?

Comments are closed.

Share via
Copy link
Powered by Social Snap