Create a WebAPI to Add/Edit/Delete the data from a SharePoint List


Tarun Kumar Chatterjee
.Net – Technology Specialist
Published On :   29 Mar 2016
Visit Count
Today :  8    Total :   7324



Let’s first create a SharePoint list will have the following columns

clip_image001

In the list Title, Modified, Created, Created By & Modified By are the defaults, remaining site columns we have created only.

Now we will be creating few termsets that will be used by MMSData site column

Go to SiteSettings -- > Term Store Management

Right Click on the Managed Metadata Service and create a new group named as “Test”, set the Group Managers & Contributors properly.

Right Click on the “Test” and Create New Term Set named as “TestTermSet”

Right click on the TestTermSet and Create Term named as “Term1”, “Term2” & “Term3”.

clip_image003

So, we are ready with the SharePoint list creation, will add some more data on the list

clip_image004

Now we will be creating an Asp.Net Web Application project named as “EmployeeWebAPI”

Select WebAPI & OK.

We will be fetching the data by using CSOM so, adding the references Mocrosoft.SharePoint.Client, Microsoft.SharePoint.Client.Runtime & Microsoft.SharePoint.Client.Taxonomy

To install System.Web.MVC in to our project need to execute the command

PM>Install-Package Microsoft.AspNet.Mvc -Version 5.0.0

Create a Model class named as “EmployeeModel.cs”, below is the code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 
 namespace EmployeeWebAPITest.Models
 {
     public class EmployeeModel
     {
         public int ID { get; set; }
         public DateTime? DateCreated { get; set; }
         public DateTime DOB { get; set; }
         public string EmployeeAddress { get; set; }
         public string EmployeeName { get; set; }
         public string Title { get; set; }
         public string UserName { get; set; }
         public string Manager { get; set; }
         public string MMSData { get; set; }
         public string MMSName { get; set; }
         public string MMSGUid { get; set; }
 
     }
 }
 

Create a Model class to fetch Term data named as “MMSLookup.cs”, below is the code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 
 namespace EmployeeWebAPITest.Models
 {
     public class MMSLookup
     {
         public string ID { get; set; }
         public string Name { get; set; }
     }
 }
 

Create another class within the Model folder named as “JsonpResult”, below is the code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Web;
 using System.Web.Http.Results;
 using System.Web.Mvc;
 using System.Web.Script.Serialization;
 
 namespace EmployeeWebAPITest.Models
 {
     public class JsonpResult : JsonResult
     {
          public string CallbackFunction { get; set; }
         public Encoding ContentEncoding { get; set; }
         public string ContentType { get; set; }
         public object Data { get; set; }
 
         public JsonpResult(object data) : this(data, null) { }
         public JsonpResult(object data, string callbackFunction)
         {
             Data = data;
             CallbackFunction = callbackFunction;
         }
 
 
         public override void ExecuteResult(ControllerContext context)
         {
             if (context == null) throw new ArgumentNullException("context");
 
             HttpResponseBase response = context.HttpContext.Response;
 
             response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/x-javascript" : ContentType;
 
             if (ContentEncoding != null) response.ContentEncoding = ContentEncoding;
 
             if (Data != null)
             {
                 HttpRequestBase request = context.HttpContext.Request;
 
                 var callback = CallbackFunction ?? request.Params["callback"] ?? "callback";
                 var serializer = new JavaScriptSerializer();
                 response.Write(callback.ToString() + "(" + serializer.Serialize(Data).ToString() + ")");
 
             }
         }
     }
 }
 

For JavaScriptSerializer class you need to include System.Web.Extensions

Create another class named as “JsonpMediaTypeFormatter” within the Model folder, below is the code:

 using Newtonsoft.Json.Converters;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Net.Http.Formatting;
 using System.Net.Http.Headers;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Web;
 using System.Web.Http;
 using System.Web.Script.Serialization;
 
 namespace EmployeeWebAPITest.Models
 {
     /// <summary>
     /// Handles JsonP requests when requests are fired with text/javascript
     /// </summary>
     public class JsonpMediaTypeFormatter : JsonMediaTypeFormatter
     {
         private string callbackQueryParameter;
 
         public JsonpMediaTypeFormatter()
         {
             SupportedMediaTypes.Add(DefaultMediaType);
             SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript"));
 
             MediaTypeMappings.Add(new UriPathExtensionMapping("jsonp", DefaultMediaType));
         }
 
         public string CallbackQueryParameter
         {
             get { return callbackQueryParameter ?? "callback"; }
             set { callbackQueryParameter = value; }
         }
 
         public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContent content, TransportContext transportContext)
         {
             string callback;
 
             if (IsJsonpRequest(out callback))
             {
                 return Task.Factory.StartNew(() =>
                 {
                     var writer = new StreamWriter(stream);
                     writer.Write(callback + "(");
                     writer.Flush();
 
                     base.WriteToStreamAsync(type, value, stream, content, transportContext).Wait();
 
                     writer.Write(")");
                     writer.Flush();
                 });
             }
             else
             {
                 return base.WriteToStreamAsync(type, value, stream, content, transportContext);
             }
         }
 
 
         private bool IsJsonpRequest(out string callback)
         {
             callback = null;
 
             if (HttpContext.Current.Request.HttpMethod != "GET")
                 return false;
 
             callback = HttpContext.Current.Request.QueryString[CallbackQueryParameter];
 
             return !string.IsNullOrEmpty(callback);
         }
     }
 }
 
 

In Global.asax.cs within Application_Start() method add the below piece of code :

 GlobalConfiguration.Configure(WebApiConfig.Register);
 GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpMediaTypeFormatter());            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
 

Now create an employee controller named as “EmployeeController”

 using EmployeeWebAPITest.Models;
 using Microsoft.SharePoint.Client;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Web.Http;
 using Newtonsoft.Json;
 using System.Web.Http.Cors;
 
 namespace EmployeeWebAPITest.Controllers
 {
     public class EmployeeController : ApiController
     {
         public HttpResponseMessage Options()
         {
             return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
         }
 
         [HttpGet]
         public JsonpResult GetEmployee(string filterSpan)
         {
             try
             {
                 using (ClientContext clientContext = new ClientContext("Site Collection URL"))
                 {
                     //The default network credential is nothing but the AppPool Account
                     clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
                     List<EmployeeModel> activities = null;
                     DataMangager objDataMangager = new DataMangager();
                     activities = objDataMangager.GetEmployeetDataFromHost(clientContext, filterSpan);
                     return new JsonpResult(activities.OrderBy(a => a.DateCreated).Reverse());
                 }
             }
             catch (Exception ex)
             {
                 return null;
             }
 
         }
         [HttpGet]
         public string GetMMSdata()
         {
 
             List<MMSLookup> terms = new List<MMSLookup>();
             try
             {
                 using (ClientContext clientContext = new ClientContext("Site Collection URL"))
                 {
                     //The default network credential is nothing but the AppPool Account
                     clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
                     DataMangager objDataMangager = new DataMangager();
                     terms = objDataMangager.GetTerms("TestTermSet", clientContext);
                 }
 
                 if (terms == null || terms.Count == 0) { throw new Exception("No terms fetched."); }
                 return JsonConvert.SerializeObject(terms);
             }
             catch (Exception ex)
             {
                 throw new Exception("Error fetching Terms." + ex.Message);
             }
         }
         public JsonpResult PostEmployee(List<EmployeeModel> lstClientActivity)
         {
             try
             {
                 List<Models.EmployeeModel> activities = null;
                 using (ClientContext clientContext = new ClientContext("Site Collection URL"))
                 {
                     //The default network credential is nothing but the AppPool Account
                     clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
                     DataMangager objDataMangager = new DataMangager();
                     activities = objDataMangager.AddEmployeeDataToHostBatch(clientContext, lstClientActivity, string.Empty);
                 }
                 return new JsonpResult(activities);
             }
             catch (Exception ex)
             {
                 throw new Exception("Error in create." + ex.Message);
             }
         }
 
         public JsonpResult PutEmployee(List<EmployeeModel> lstClientActivity)
         {
 
             var entities = new List<Models.EmployeeModel>();
             List<Models.EmployeeModel> activities = null;
             try
             {
                 using (ClientContext clientContext = new ClientContext("Site Collection URL"))
                 {
                     //The default network credential is nothing but the AppPool Account
                     clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
                     DataMangager objDataMangager = new DataMangager();
                     activities = objDataMangager.EditEmployeeDataOnHostBatch(clientContext, lstClientActivity, string.Empty);
                 }
                 return new JsonpResult(activities);
             }
             catch (Exception ex)
             {
                 throw new Exception("Error in edit." + ex.Message);
             }
         }
     }
 }
 

EmployeeController.cs is referring to DataManager class, here is the code snippet of DataManager class.

 using EmployeeWebAPITest.Models;
 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.Taxonomy;
 using System;
 using System.Collections.Generic;
 using System.Configuration;
 using System.Globalization;
 using System.IO;
 using System.Linq;
 using System.Net;
 using System.Web;
 using System.Xml;
 
 
 namespace EmployeeWebAPITest
 {
     public class DataMangager
     {        
         public  List<EmployeeModel> EditEmployeeDataOnHostBatch(ClientContext CSOMcontext, List<Models.EmployeeModel> input, string empname)
         {
             
 
             List<EmployeeModel> lstEmployeeModel = new List<EmployeeModel>();
             EmployeeModel objEmployeeModel = new EmployeeModel();
             if (input.Count < 1)
             {
                 throw new Exception("No records has been updated.");
             }
             Dictionary<string, string> columnNames = new Dictionary<string, string>();
             List list = null;
             string EmployeeDataListName = "Employee";
             
 
             try
             {
                 
                 list = CSOMcontext.Web.Lists.GetByTitle(EmployeeDataListName);
                 CSOMcontext.Load(list);
                 CSOMcontext.ExecuteQuery();
 
                 BuildColumnNameLookup(CSOMcontext, columnNames, list);
 
                 foreach (var item in input)
                 {
                     objEmployeeModel = new EmployeeModel();
                     ListItem oListItem;
                     oListItem = list.GetItemById(item.ID);
                     CSOMcontext.ExecuteQuery();
 
                     objEmployeeModel.ID = item.ID;
 
                     if (item.Manager != null)
                     {
                         User userTest = CSOMcontext.Web.EnsureUser(item.Manager);
                         CSOMcontext.Load(userTest);
                         CSOMcontext.ExecuteQuery();                        
                         objEmployeeModel.Manager = item.Manager;
                         oListItem[columnNames[Constants.Names.ManagerColumnName]] = userTest.Id.ToString() + ";#" + userTest.LoginName.ToString();
                         
                     }
 
                     if (item.Title != null)
                     {
                         oListItem[columnNames[Constants.Names.TitleColumnName]] = item.Title;
                         objEmployeeModel.Title = item.Title;
                     }
 
                     if (item.EmployeeName != null)
                     {
                         oListItem[columnNames[Constants.Names.EmployeeNameColumnName]] = item.EmployeeName;
                         objEmployeeModel.EmployeeName = item.EmployeeName;
                     }
 
                     if (item.EmployeeAddress != null)
                     {
                         oListItem[columnNames[Constants.Names.employeeaddressColumnName]] = item.EmployeeAddress;
                         objEmployeeModel.EmployeeAddress = item.EmployeeAddress;
                     }
                     if (item.DOB != null)
                     {
                         oListItem[columnNames[Constants.Names.dobColumnName]] = item.DOB;
                         objEmployeeModel.DOB = item.DOB;
                     }
                     
                     if (item.MMSData != null)
                     {
                         oListItem[columnNames[Constants.Names.MMSColumnName]] = string.Format("-1;#{1}|{0}", item.MMSGUid.Split((new string[] { "##" }), StringSplitOptions.None)[1], item.MMSName);
                         objEmployeeModel.MMSData = item.MMSData;
                         objEmployeeModel.MMSName = item.MMSName;
                         objEmployeeModel.MMSGUid = item.MMSGUid.Split((new string[] { "##" }), StringSplitOptions.None)[1];
                     }
 
                     oListItem.Update();
                     CSOMcontext.ExecuteQuery();
                     if (item.MMSData != null)
                     {
                         objEmployeeModel.MMSGUid = item.MMSGUid;
                     }
                     objEmployeeModel.UserName = item.UserName;
                     objEmployeeModel.DateCreated = item.DateCreated;
                     lstEmployeeModel.Add(objEmployeeModel);
                 }
 
 
             }
             catch (Exception ex)
             {
                 throw new Exception("Failed to update record." + ex.Message);
             }
             return lstEmployeeModel;
         }       
         public  List<EmployeeModel> AddEmployeeDataToHostBatch(ClientContext CSOMcontext, List<Models.EmployeeModel> input, string empname)
         {
             string EmployeeDataListName = "Employee";
             
             Dictionary<string, string> columnNames = new Dictionary<string, string>();
             List list = null;
             List<EmployeeModel> lstEmployeeModel = new List<EmployeeModel>();
             EmployeeModel objEmployeeModel = new EmployeeModel();
             try
             {
                 list = CSOMcontext.Web.Lists.GetByTitle(EmployeeDataListName);
                 CSOMcontext.Load(list);
                 CSOMcontext.ExecuteQuery();
 
                 BuildColumnNameLookup(CSOMcontext, columnNames, list);
                 ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
 
                 foreach (var item in input)
                 {
                     objEmployeeModel = new EmployeeModel();
                     ListItem oListItem;
                     oListItem = list.AddItem(itemCreateInfo);
                     User userTest = CSOMcontext.Web.EnsureUser(item.Manager);
                     CSOMcontext.Load(userTest);
                     CSOMcontext.ExecuteQuery();
                                         
                     oListItem[columnNames[Constants.Names.ManagerColumnName]] =  userTest.Id.ToString() + ";#" + userTest.LoginName.ToString();
                     objEmployeeModel.Manager = item.Manager;
                     oListItem[columnNames[Constants.Names.TitleColumnName]] = item.Title;
                     objEmployeeModel.Title = item.Title;
                     oListItem[columnNames[Constants.Names.EmployeeNameColumnName]] = item.EmployeeName;
                     objEmployeeModel.EmployeeName = item.EmployeeName;
                     oListItem[columnNames[Constants.Names.employeeaddressColumnName]] = item.EmployeeAddress;
                     objEmployeeModel.EmployeeAddress = item.EmployeeAddress;
                     oListItem[columnNames[Constants.Names.dobColumnName]] = item.DOB;
                     objEmployeeModel.DOB = item.DOB;
                    
                     oListItem[columnNames[Constants.Names.MMSColumnName]] = string.Format("-1;#{1}|{0}", item.MMSGUid.Split((new string[] { "##" }), StringSplitOptions.None)[1], empname);
                     objEmployeeModel.MMSData = item.MMSData;
                     objEmployeeModel.MMSName = item.MMSName;
                     
                     objEmployeeModel.MMSGUid = item.MMSGUid.Split((new string[] { "##" }), StringSplitOptions.None)[1];
                     oListItem.Update();
                     CSOMcontext.ExecuteQuery();
                     objEmployeeModel.MMSGUid = item.MMSGUid;
                     objEmployeeModel.DateCreated = DateTime.Now;
                     //objEmployeeModel.UserName = item.UserName;
                     lstEmployeeModel.Add(objEmployeeModel);
                 }
             }
             catch (Exception ex)
             {
                 throw new Exception("Failed to add new record." + ex.Message);
             }
             return lstEmployeeModel;
         }                
         public List<EmployeeModel> GetEmployeetDataFromHost(ClientContext CSOMcontext, string filterSpan)
         {
             string EmployeeDataListName = "Employee";
 
             //Name MMSCountry Activity Status Created Created By Modified Modified By
             Dictionary<string, string> columnNames = new Dictionary<string, string>();
 
             List<EmployeeModel> results = new List<EmployeeModel>();
             List list = null;
             Web web = null;
             try
             {
                 web = CSOMcontext.Web;
                 ListCollection lists = web.Lists;
                 CSOMcontext.Load(lists);
                 CSOMcontext.ExecuteQuery();
             }
             catch (Exception ex)
             {
                 throw new Exception("Unable to access host web at " + CSOMcontext.Url + "." + ex.Message);
             }
             try
             {
                 list = web.Lists.GetByTitle(EmployeeDataListName);
                 CSOMcontext.Load(list);
                 CSOMcontext.ExecuteQuery();
             }
             catch (Exception ex)
             {
                 throw new Exception("List " + EmployeeDataListName + " not found." + ex.Message);
             }
 
             BuildColumnNameLookup(CSOMcontext, columnNames, list);
 
             string fromDateFx = DateTime.Now.AddHours(-2).ToString("yyyy-MM-ddTHH:mm:ssZ");
             string toDateFx = DateTime.Now.AddDays(1).ToString("yyyy-MM-ddTHH:mm:ssZ");
 
             if (list != null && list.ItemCount > 0)
             {
                 CamlQuery camlQuery = new Microsoft.SharePoint.Client.CamlQuery();
                 if (filterSpan == "today")
                 {
                     camlQuery = CreateCamlDateQuery(fromDateFx, toDateFx);
                 }
 
                 ListItemCollection allItems = list.GetItems(camlQuery);
                 CSOMcontext.Load(allItems, items => items.Take(500));
                 CSOMcontext.ExecuteQuery();
 
                 foreach (ListItem item in allItems)
                 {
                     EmployeeModel emp = new EmployeeModel
                     {
                         ID = item.Id,
                         Title = "unknown",
                         EmployeeAddress = "unknown",
                         DOB = DateTime.MinValue,
                         EmployeeName = "unknown",
                         Manager = "unknown",
                         MMSData = "unknown",
                         DateCreated = DateTime.Now
 
                     };
                     try
                     {
 
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.dobColumnName]))
                         {
                             emp.DOB = Convert.ToDateTime(Convert.ToDateTime(item[columnNames[Constants.Names.dobColumnName]]).ToShortDateString());
                         }
 
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.createdColumnName]))
                         {
                             emp.DateCreated = Convert.ToDateTime(Convert.ToDateTime(item[columnNames[Constants.Names.createdColumnName]]).ToShortDateString());
                         }
 
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.employeeaddressColumnName]))
                         {
                             emp.EmployeeAddress = item[columnNames[Constants.Names.employeeaddressColumnName]].ToString();
                             emp.EmployeeAddress =  emp.EmployeeAddress.Substring(emp.EmployeeAddress.IndexOf("\">") + 2, (emp.EmployeeAddress.IndexOf("</div>") - 2 - emp.EmployeeAddress.IndexOf("\">")));
                         }
 
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.EmployeeNameColumnName]))
                         {
                             emp.EmployeeName = item[columnNames[Constants.Names.EmployeeNameColumnName]].ToString();
                         }
 
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.TitleColumnName]))
                         {
                             emp.Title = item[columnNames[Constants.Names.TitleColumnName]].ToString();
                         }
 
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.ManagerColumnName]))
                         {
                             emp.Manager = ((FieldUserValue)item[columnNames[Constants.Names.ManagerColumnName]]).LookupValue;
                         }
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.MMSColumnName]))
                         {
                             TaxonomyFieldValue mmsfield = (TaxonomyFieldValue)item[columnNames[Constants.Names.MMSColumnName]];
                             emp.MMSData = mmsfield.Label;
                             emp.MMSGUid = emp.MMSData.Split(':').Last() + "##" + mmsfield.TermGuid;
                             emp.MMSName = emp.MMSData;
                         }
 
                         if (item.FieldValues.ContainsKey(columnNames[Constants.Names.CreatedByColumnName]))
                         {
                             emp.UserName = ((FieldUserValue)item[columnNames[Constants.Names.CreatedByColumnName]]).LookupValue;
                         }
                         results.Add(emp);
 
                     }
                     catch { }
 
                 }
             }
 
             return results;
         }
         private  CamlQuery CreateCamlDateQuery(string fromDate, string toDate)
         {
             string camlString = String.Format("{0}\n{1}\n{2}\n{3}\n{4}",
                 "<View><Query><Where> <And><Geq>",
                 "<FieldRef Name='Created'/><Value Type='DateTime'>" + fromDate + "</Value>",
                 "</Geq><Leq><FieldRef Name='Created'/>",
                 "<Value Type='DateTime'>" + toDate + "</Value>",
                 "</Leq></And></Where></Query></View>");
             return new Microsoft.SharePoint.Client.CamlQuery() { ViewXml = camlString };//IncludeTimeValue='FALSE'
         }
         public  DateTime FirstDayOfWeek(DateTime date)
         {
             DayOfWeek fdow = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
             int offset = fdow - date.DayOfWeek;
             DateTime fdowDate = date.AddDays(offset);
             return fdowDate;
         }
         public DateTime LastDayOfWeek(DateTime date)
         {
             DateTime ldowDate = FirstDayOfWeek(date).AddDays(6);
             return ldowDate;
         }
         public  List<MMSLookup> GetTerms(string termSetName, ClientContext clientContext)
         {
             List<MMSLookup> mmsdata = new List<MMSLookup>();
             TaxonomySession tSession = TaxonomySession.GetTaxonomySession(clientContext);
             TermStore ts = tSession.GetDefaultSiteCollectionTermStore();
             TermSetCollection tsets = ts.GetTermSetsByName(termSetName, 1033);
             clientContext.Load(tSession);
             clientContext.Load(ts);
             clientContext.Load(tsets);
             clientContext.ExecuteQuery();
             TermSet tset = tsets[0];
             TermCollection terms = tset.GetAllTerms();//tset.GetTerms(lmi);   //
             clientContext.Load(terms);
             clientContext.ExecuteQuery();
 
 
             if (terms != null && terms.Count() > 0)
             {
                 foreach (var t in terms)
                 {
                     string[] termbits = t.PathOfTerm.Split(';');
                     
                     {
                         MMSLookup c = new MMSLookup();
                         
                         c.Name = t.PathOfTerm.Split(';').Last();
                        
                         c.ID = c.Name + "##" + t.Id.ToString();
                         mmsdata.Add(c);
                     }
                 }
             }
             return mmsdata;
         }
         private  void BuildColumnNameLookup(ClientContext CSOMcontext, Dictionary<string, string> columnNames, List list)
         {
 
             columnNames.Add(Constants.Names.dobColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.dobColumnName));
             columnNames.Add(Constants.Names.createdColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.createdColumnName));
             columnNames.Add(Constants.Names.employeeaddressColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.employeeaddressColumnName));
             columnNames.Add(Constants.Names.EmployeeNameColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.EmployeeNameColumnName));
             columnNames.Add(Constants.Names.ManagerColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.ManagerColumnName));
             columnNames.Add(Constants.Names.TitleColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.TitleColumnName)); 
             columnNames.Add(Constants.Names.MMSColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.MMSColumnName));
             columnNames.Add(Constants.Names.CreatedByColumnName, GetCSOMcolumnRef(CSOMcontext, list, Constants.Names.CreatedByColumnName));
         }
         private  string GetCSOMcolumnRef(ClientContext ctx, List list, string displayName)
         {
             Field fld = list.Fields.GetByTitle(displayName);
             ctx.Load(fld);
             ctx.ExecuteQuery();
             return fld.InternalName;
         }
 
     }
 }
 

DataManager.cs is referring to a Constant Class; here is the code snippet

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 
 namespace EmployeeWebAPITest
 {
     public class Constants
     {
         public class Names
         {
             public const string createdColumnName = "Created";
 
             public const string dobColumnName = "DOB";
 
             public const string employeeaddressColumnName = "EmployeeAddress";
 
             public const string EmployeeNameColumnName = "EmployeeName";
 
             public const string ManagerColumnName = "Manager";
 
             public const string MMSColumnName = "MMSData";
          
             public const string TitleColumnName = "Title";
 
             public const string CreatedByColumnName = "Created By";
            
         }
     }
 }
 

Within the App_Start folder in WebApiConfig.cs class add the following piece of code

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web.Http;
 
 namespace EmployeeWebAPITest
 {
     public static class WebApiConfig
     {
         public static void Register(HttpConfiguration config)
         {
             // Web API configuration and services
 
             // Web API routes
             config.MapHttpAttributeRoutes();
 
             config.Routes.MapHttpRoute(
                name: "GetEmployee",
                routeTemplate: "api/Employee/GetEmployee/{filterSpan}",
                defaults: new { controller = "Employee", action = "GetEmployee" }
          );
 
             config.Routes.MapHttpRoute(
                 name: "DefaultApi",
                 routeTemplate: "api/{controller}/{id}",
                 defaults: new { id = RouteParameter.Optional }
             );
         }
     }
 }
 

Now build the solution & run

Here is the output of MMS data

clip_image006

Here is the output of Employee List data

clip_image008

HttpGet works fine in both IE & Chrome. The problem I have with HttpPost method in chrome but not in IE.

At the time of posting the employee data through Chrome it is throwing me the following error:

XMLHttpRequest cannot load http://<Domain Name>/WebAPITest/api/Employee/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://<Domain Name>' is therefore not allowed access.

As I have mentioned in the article: http://www.sharepointpals.com/post/How-to-achieve-the-Cross-domain-WebAPI-(GetPost)-call-using-Ajax-request, we need to follow the below steps to resolve the issue

Run the below commands in Package Manager Console

PM> Install-Package NuGet.Core

PM> Update-Package Microsoft.AspNet.WebApi –reinstall

PM> Install-Package Microsoft.AspNet.WebApi.Cors.ko -Version 5.0.0

In the EmployeeController class add the following attribute:

[EnableCors(origins: "*", headers: "*", methods: "*")]

Add the following piece of the line in web.config

 <system.webServer>
     <httpProtocol>
       <customHeaders>
         <add name="Access-Control-Expose-Headers " value="WWW-Authenticate"/>
         <add name="Access-Control-Allow-Origin" value="*" />
         <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD" />
         <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
       </customHeaders>
     </httpProtocol>
   </system.webServer>
 

Now the HttpPost from Chrome will be working properly.

As this WebAPI itself becomes a very lengthy article, so planning to write to display this WebAPI data into a Kendo grid which will be hosted within a custom SharePoint WebPart .

Happy Coding

Tarun Kumar Chatterjee

Categories