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:
- public static void getListData(string weburl)
- {
- Lists.Lists myservice = new Lists.Lists();
- myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
- myservice.Url = weburl + "/_vti_bin/Lists.asmx";
-
- try
- {
-
- string listName = "Test List";
- string viewName = "";
- string rowLimit = "100";
-
-
- System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
- System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
- System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
- System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
-
-
- query.InnerXml = string.Format("<Where><And>" +
- "<Gt>" +
- "<FieldRef Name='ID' /><Value Type='Counter'>{0}</Value>" +
- "</Gt>" +
- "<Leq><FieldRef Name='ID' /><Value Type='Counter'>{1}</Value></Leq>" +
- "</And></Where>", 0, 100);
- viewFields.InnerXml = "<FieldRef Name='Title' />" +
- "<FieldRef Name='Created' />" +
- "<FieldRef Name='Modified' />" +
- "<FieldRef Name='Author' />" +
- "<FieldRef Name='Editor' />";
- queryOptions.InnerXml = "";
-
- System.Xml.XmlNode nodeListItems = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, null, null);
- XmlDataDocument xmlDocResult = new XmlDataDocument();
- xmlDocResult.LoadXml(nodeListItems.InnerXml);
- XmlNodeList rows = xmlDocResult.GetElementsByTagName("z:row");
- foreach(XmlNode attribute in rows) {
-
- Console.WriteLine(attribute.Attributes["ows_Title"].Value);
- string AttachmentUrl = GetAttachmentUrls(weburl, listName, attribute.Attributes["ows_ID"].Value, "");
- string vesrsion = GetVersions(weburl, listName, attribute.Attributes["ows_ID"].Value, "Title");
- }
-
- } catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
-
- public static string GetAttachmentUrls(string siteUrl, string listId, string itemId, string fieldName)
- {
- StringBuilder sb = new StringBuilder();
-
- Lists.Lists listService = new Lists.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
-
- }
- public static string GetVersions(string siteUrl, string listId, string itemId, string fieldName)
- {
- StringBuilder sb = new StringBuilder();
- Lists.Lists listService = new Lists.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");
- }
- }
- return sb.ToString();#
- endregion
- }
-
- private static string FormatDateFromSP(string dateHistory)
- {
- string result;
-
- result = dateHistory.Replace("T", " ");
- result = result.Replace("Z", "");
-
- return result;
- }
-
-
-
-
-
- private static string GetEditor(string nodeValue)
- {
- string[] arr;
- char[] sep =
- {
- '#'
- };
-
-
- arr = nodeValue.Split(sep);
-
- nodeValue = arr[1];
-
- return nodeValue.Remove(nodeValue.Length - 1);
- }
- }
Summary
Using SharePoint web services to retrieve the version history and attachment URLs for the list item at client side.