SharePoint 2013: Retrieve The List Item Version History And Attachment URLs Using SharePoint Web Services

Posted: March 12, 2017 in SharePoint2013
Introduction
In this article we will explore how to maintain the version of the field and fetch the attachment URL of a list item. Here, I will show how to do it using client side mode via SharePoint Web service using Webservice only ( _vit_bin/lists.asmx)..

Scenario:
The scenario is best suited when in migration from one SharePoint version to another, we need to maintain the attachment URL and the versioning of the fields using Webservice only ( _vit_bin/lists.asmx).
Solutions:
Code:
  1. public static void getListData(string weburl)
  2. {
  3.     Lists.Lists myservice = new Lists.Lists();
  4.     myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
  5.     myservice.Url = weburl + “/_vti_bin/Lists.asmx”;
  6.     try
  7.     {
  8.         /* Assign values to pass the GetListItems method*/
  9.         string listName = “Test List”;
  10.         string viewName = “”;
  11.         string rowLimit = “100”;
  12.         // Instantiate an XmlDocument object
  13.         System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
  14.         System.Xml.XmlElement query = xmlDoc.CreateElement(“Query”);
  15.         System.Xml.XmlElement viewFields = xmlDoc.CreateElement(“ViewFields”);
  16.         System.Xml.XmlElement queryOptions = xmlDoc.CreateElement(“QueryOptions”);
  17.         /*Use CAML query*/
  18.         query.InnerXml = string.Format(“<Where><And>” +
  19.             “<Gt>” +
  20.             “<FieldRef Name=’ID’ /><Value Type=’Counter’>{0}</Value>” +
  21.             “</Gt>” +
  22.             “<Leq><FieldRef Name=’ID’ /><Value Type=’Counter’>{1}</Value></Leq>” +
  23.             “</And></Where>”, 0, 100);
  24.         viewFields.InnerXml = “<FieldRef Name=’Title’ />” +
  25.             “<FieldRef Name=’Created’ />” +
  26.             “<FieldRef Name=’Modified’ />” +
  27.             “<FieldRef Name=’Author’ />” +
  28.             “<FieldRef Name=’Editor’ />”;
  29.         queryOptions.InnerXml = “”;
  30.         System.Xml.XmlNode nodeListItems = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, nullnull);
  31.         XmlDataDocument xmlDocResult = new XmlDataDocument();
  32.         xmlDocResult.LoadXml(nodeListItems.InnerXml);
  33.         XmlNodeList rows = xmlDocResult.GetElementsByTagName(“z:row”);
  34.         foreach(XmlNode attribute in rows) {
  35.             Console.WriteLine(attribute.Attributes[“ows_Title”].Value);
  36.             string AttachmentUrl = GetAttachmentUrls(weburl, listName, attribute.Attributes[“ows_ID”].Value, “”);
  37.             string vesrsion = GetVersions(weburl, listName, attribute.Attributes[“ows_ID”].Value, “Title”);
  38.         }
  39.     } catch (Exception ex)
  40.     {
  41.         Console.WriteLine(ex.Message);
  42.     }
  43. }
  44. public static string GetAttachmentUrls(string siteUrl, string listId, string itemId, string fieldName)
  45. {
  46.     StringBuilder sb = new StringBuilder();
  47.     Lists.Lists listService = new Lists.Lists();
  48.     listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
  49.     listService.Url = siteUrl + “/_vti_bin/lists.asmx”;#
  50.     region Get the list of attachments
  51.     XmlNode nodeAttachments = listService.GetAttachmentCollection(listId, itemId);
  52.     List < string > values = new List < string > ();
  53.     foreach(System.Xml.XmlNode xNode in nodeAttachments)
  54.     {
  55.         values.Add(xNode.InnerText);
  56.     }
  57.     return string.Join(“;”, values.ToArray());#
  58.     endregion
  59. }
  60. public static string GetVersions(string siteUrl, string listId, string itemId, string fieldName)
  61. {
  62.     StringBuilder sb = new StringBuilder();
  63.     Lists.Lists listService = new Lists.Lists();
  64.     listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
  65.     listService.Url = siteUrl + “/_vti_bin/lists.asmx”;
  66.   #region Get version histories
  67.     if (!string.IsNullOrEmpty(fieldName))
  68.     {
  69.         XmlNode nodeVersions = listService.GetVersionCollection(listId, itemId, fieldName);
  70.         foreach(System.Xml.XmlNode xNode in nodeVersions)
  71.         {
  72.             string dateHistory = xNode.Attributes[“Modified”].Value;
  73.             dateHistory = FormatDateFromSP(dateHistory);
  74.             string commentHistory = xNode.Attributes[fieldName].Value;
  75.             string editor = GetEditor(xNode.Attributes[“Editor”].Value);
  76.             sb.Append(editor + ” (“ + dateHistory + “) “ + commentHistory + “\n\n”);
  77.         }
  78.     }
  79.     return sb.ToString();#
  80.     endregion
  81. }
  82. private static string FormatDateFromSP(string dateHistory)
  83. {
  84.         string result;
  85.         result = dateHistory.Replace(“T”” “);
  86.         result = result.Replace(“Z”“”);
  87.         return result;
  88.     }
  89.     /// <summary>
  90.     /// The XmlNode for version on the Editor contains the Editor Name
  91.     /// </summary>
  92.     /// <param name=”ienumEditor”></param>
  93.     /// <returns></returns>
  94. private static string GetEditor(string nodeValue)
  95. {
  96.     string[] arr;
  97.     char[] sep =
  98.       {
  99.         ‘#’
  100.     };
  101.     // Go for the Editor attribute value
  102.     // A sample is: 30;#Jo�o Faneca,#VIATECLA\\jfaneca,#joao.faneca@viatecla.pt,#joao.faneca@viatecla.pt,#Jo�o Faneca
  103.     arr = nodeValue.Split(sep);
  104.     // Grab the second element for the array
  105.     nodeValue = arr[1];
  106.     // Remove the last comma from the Editor value
  107.     return nodeValue.Remove(nodeValue.Length – 1);
  108. }
  109. }
Summary
Using SharePoint web services to retrieve the version history and attachment URLs for the list item at client side.
Advertisements

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