2

Is it possible to get only the changes (changed field values) from every version of a List Item in SP 2013? If so, can anyone please explain it in detail with code?

1 Answer 1

3

There's no direct way provided by the server side API to get version changes. But you can compare the fields of different versions:

using (SPSite site = new SPSite("http://my.domain.com/"))
using (SPWeb web = site.OpenWeb())
{
    SPList list = web.GetList("http://my.domain.com/Lists/Stuff");
    SPListItem item = list.Items[1];

    Console.Out.WriteLine(item.Title);

    SPListItemVersion currentVersion = item.Versions[0];
    SPListItemVersion previousVersion = item.Versions.Count > 1 ? item.Versions[1] : null;

    Console.Out.WriteLine("Version is current: {0}", currentVersion.IsCurrentVersion);

    foreach (SPField field in currentVersion.Fields)
    {
        if (field.ShowInVersionHistory == false)
        {
            continue;
        }

        if (previousVersion == null)
        {
            Console.Out.WriteLine("  > {0} changed to \"{1}\"",
                field.StaticName, currentVersion[field.StaticName]);
            continue;
        }

        if (currentVersion[field.StaticName].Equals(previousVersion[field.StaticName]))
        {
            continue;
        }

        Console.Out.WriteLine("  > {0} changed from \"{1}\" to \"{2}\"",
            field.StaticName, previousVersion[field.StaticName], currentVersion[field.StaticName]);
    }
}

Source

3
  • I am sorry, but I have seen this code and I know this. I was looking for a better solution. Commented Feb 10, 2016 at 9:45
  • I'm afraid a better solution likely doesn't exist. Commented Feb 10, 2016 at 9:57
  • If you are looking for client code, CSOM or Rest API doesn't support this, for no real reason, MS just forgot to implement this. However you should be able to dig out list item versions and values calling old school list.asmx web service. Commented Oct 8, 2018 at 6:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.