Tuesday, 27 October 2020

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

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

Using SharePoint web services to retrieve the version history and attachment URLs for the list item on the client-side.

No comments:

Post a Comment

SharePoint 2013 - Uploading Multiple Attachments To The New Item On List Using JSOM And REST API

  Introduction In this article, we will explore how we can attach multiple attachments to the new item on list using JSOM and REST API. Ther...