Sivarajan's Blog

SharePoint | Office 365 | Azure | JavaScript

Retrieve the List item Version History and Attachment URLs in SharePoint

Overview

I had a requirement to retrieve the version history and attachment URLs for the list item at client side. We will see here how to retrieve that.

List item Version History

Instead of using the .NET client object model, we can retrieve the list item using SharePoint list web service (Lists.asmx) easily.

        public static string GetVersions(string siteUrl, string listId, string itemId, string fieldName)
        {
            StringBuilder sb = new StringBuilder();

            Lists listService = new Lists();

            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            listService.Url = siteUrl + "/_vti_bin/lists.asmx";

            #region Get version histories
            if (!string.IsNullOrEmpty(fieldName))
            {
                XmlNode nodeVersions = listService.GetVersionCollection(listId, itemId, fieldName);

                foreach (System.Xml.XmlNode xNode in nodeVersions)
                {
                    string dateHistory = xNode.Attributes["Modified"].Value;
                    dateHistory = FormatDateFromSP(dateHistory);
                    string commentHistory = xNode.Attributes[fieldName].Value;
                    string editor = GetEditor(xNode.Attributes["Editor"].Value);

                    sb.Append(editor + " (" + dateHistory + ") " + commentHistory + "\n\n");
                }
            }
            #endregion

            return sb.ToString();
        }
 
 

This method will return the version history for the particular list column.

List Item Attachment URLs

Similarly we can retrieve the attachment URLs for the specific list item using Lists.asmx web service.

        public static string GetAttachmentUrls(string siteUrl, string listId, string itemId, string fieldName)
        {
            StringBuilder sb = new StringBuilder();

            Lists listService = new Lists();

            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            listService.Url = siteUrl + "/_vti_bin/lists.asmx";
            #region Get the list of attachments
            XmlNode nodeAttachments = listService.GetAttachmentCollection(listId, itemId);

            List<string> values = new List<string>();

            foreach (System.Xml.XmlNode xNode in nodeAttachments)
            {
                values.Add(xNode.InnerText);
            }

            return string.Join(";", values.ToArray());
            #endregion

        }

This will return the list of attachments URLs for the particular list item.

Comments (4) -

  • Ajit Bhandari

    2/17/2015 6:14:51 PM | Reply

    Can this be done using REST Services ???

  • deva

    7/20/2015 6:02:26 AM | Reply

    Hello Siva,

    Currently i am working with sharepoint list with a customer workflow in sharepoint designer. I have a list column to display the version history of the particular list item. Is there a way to bind the version history to a list column in sharepoint designer workflow steps.

  • Gokulnath Somi Sankaran

    8/17/2015 7:19:37 AM | Reply

    Thanks for this post. Can you explain how to achieve this using Sharepoint client object model or Sharepoint webservice method using _vit_bin/lists.asmx?

    • Sivarajan Raju

      9/28/2015 7:45:47 PM | Reply

      I have done this using Webservice only ( _vit_bin/lists.asmx).

Add comment

Loading