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.

SharePoint 2013 Chart WebPart Using HTML 5 RGraph with WCF REST Services

Overview Chart WebPart is not available in SharePoint Server 2013 and we have to use excel services as a alternative to this. But it will be available only with enterprise edition. For SharePoint foundation / standard edition, we can use the ASP.NET charts or third party chart controls. In this walkthrough, we will see that how to leverage the RGraph for SharePoint charts implementations. What is RGraph? RGraph is a HTML5 charts library that uses the HTML5 canvas tag to draw and supports over twenty different types of charts and click here for more information about RGraph and also you can download the javascript libraries here. What is the Advantage of using RGraph with SharePoint? Currently I am working lot of charts related implementations in SharePoint 2013 and I found that it is more flexible and fastest one while comparing with ASP.NET chart controls. The main advantage is that it is 100% client side rendering and easily change the data source / chart types without post back. Chart Data Source For this walkthrough, I am using SharePoint foundation 2013 and I have created a list called “Income Tracker” Now the chart data source is ready and we have a multiple options to expose the data to client. SharePoint Client Object Model (ECMA Scripts) ListData.svc Custom WCF Services I am going to use the custom WCF service for data retrieval. Because I have done the detailed walkthrough for custom WCF services earlier and you can find the step by step walkthrough here. SharePoint Solution Structure I have created the solution structure like below, You can find the complete code implementation below, public class GraphData     {         public List<string> ChartLabels { get; set; }         public List<int> IncomeValues { get; set; }         public List<int> ExpenseValues { get; set; }         public GraphData()         {             ChartLabels = new List<string>();             IncomeValues = new List<int>();             ExpenseValues = new List<int>();         }              } [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]       public class TrackerService : ITrackerService     {         public GraphData Get()         {             GraphData graphData = new GraphData();             SPList incomeTracker = SPContext.Current.Web.Lists["Income Tracker"];             if(incomeTracker != null)             {                 foreach (SPListItem item in incomeTracker.Items)                 {                     graphData.ChartLabels.Add(item.Title);                     graphData.IncomeValues.Add(Convert.ToInt32(item["Income"]));                     graphData.ExpenseValues.Add(Convert.ToInt32(item["Expense"]));                                 }             }             return graphData;         }     } function RenderChart(graphData) {     var data = mixArrays(graphData.IncomeValues, graphData.ExpenseValues);     var bar = new RGraph.Bar('cvs', data);     bar.Set('chart.labels', graphData.ChartLabels);     bar.Set('chart.gutter.left', 65);     bar.Set('colors', ['#058DC7', 'Gradient(#50B332:#B1E59F)', 'Gradient(#EC561B:#F59F7D)']);     bar.Set('hmargin', 15);     bar.Set('strokestyle', 'white');     bar.Set('linewidth', 2);     bar.Set('shadow', true);     bar.Set('shadow.offsetx', 1);     bar.Set('shadow.offsety', 0);     bar.Set('shadow.blur', 1);     ClearAndRedraw(bar); } function ClearAndRedraw(obj) {     RGraph.ClearAnnotations(obj.canvas);     RGraph.Clear(obj.canvas);     obj.Draw(); }   Deploy the solution and add the VisualChart web part in site pages. The chart will be rendered like below. Summary Currently I am doing lot of fun with RGraph and surely I will write some more posts regarding RGraph integrations related challenges with SharePoint. Download Source Code