SharePoint 2013 Retrieve the List Item Version History and Attachment URLs Using SharePoint Web Services

InfoPath alternatives for form designing SharePoint

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).

Read some SharePoint 2013 tutorials below:

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
{
/* Assign values to pass the GetListItems method*/
string listName = “Test List”;
string viewName = “”;
string rowLimit = “100”;

// Instantiate an XmlDocument object
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”);

/*Use CAML query*/
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;
}
/// <summary>
/// The XmlNode for version on the Editor contains the Editor Name
/// </summary>
/// <param name=”ienumEditor”></param>
/// <returns></returns>
private static string GetEditor(string nodeValue)
{
string[] arr;
char[] sep =
{
‘#’
};
// Go for the Editor attribute value
// A sample is: 30;#Jo�o Faneca,#VIATECLA\\jfaneca,#joao.faneca@viatecla.pt,#joao.faneca@viatecla.pt,#Jo�o Faneca
arr = nodeValue.Split(sep);
// Grab the second element for the array
nodeValue = arr[1];
// Remove the last comma from the Editor value
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.


You May Also like the Following SharePoint Online Tutorials:

About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →

Leave a Reply