Sharepoint and REST - WebClient - Insert Data to list
Problem Definition
Add attachment to SharePoint list item using REST API and WebClient.
Prerequisites
In order to effectively deal with the data in the SharePoint List I will recommend using Newtonsoft.JSON that makes dealing with List data a lot easier.
In order to get JSON plugin go to NuGet and search for JSON.NET or NEWTONSOFT
SharePointAndRest_WebClient.cs
class dealing with SharePoint Data posting:
Code Help
/// <summary>/// Implement Sharepoint Functionality using WebClient
/// </summary>
public class SharePointAndRest_WebClient : IDisposable
{
private readonly WebClient webClient;
public Uri WebUri { get; private set; }
/// <summary>
/// Intialize webclient and set it up for use with sharepoint using Default Credentials
/// </summary>
/// <param name="webUri">Basic url to sharepoint site</param>
public SharePointAndRest_WebClient(Uri webUri)
{
webClient = new WebClient();
webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
webClient.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
webClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0");
webClient.UseDefaultCredentials = true;
WebUri = webUri;
}
/// <summary>
/// Inserts item in the list
/// </summary>
/// <param name="listTitle">Title of the List</param>
/// <param name="payload">Data to store in the form of Annoymour method </param>
public void InsertItem(string listTitle, object payload)
{
var formDigestValue = GetFormDigest();
webClient.Headers.Add("X-RequestDigest", formDigestValue);
var endpointUri = new Uri(WebUri, string.Format("_api/web/lists/getbytitle('{0}')/items", listTitle));
var payloadString = JsonConvert.SerializeObject(payload);
webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
webClient.UploadString(endpointUri, "POST", payloadString);
}
/// <summary>
/// Provides form data digest
/// </summary>
/// <returns></returns>
private string GetFormDigest()
{
var endpointUri = new Uri(WebUri, "_api/contextinfo");
var result = webClient.UploadString(endpointUri, "POST");
JToken t = JToken.Parse(result);
return t["d"]["GetContextWebInformation"]["FormDigestValue"].ToString();
}
/// <summary>
/// IDisposable Implementation
/// </summary>
public void Dispose()
{
webClient.Dispose();
GC.SuppressFinalize(this);
GC.Collect();
}
}
SharePointAndRest.cs
Method calling SharePointAndRest_WebClient Class:Code Help
/// <summary>/// This method supplied the data and calls the execution of Insert
/// </summary>
public static void SharepointAndRest_WebClient_Insert()
{
//var webUri = new Uri(@"http://<your server>/<site>/");
using (var client = new SharePointAndRest_WebClient(webUri))
{
var testEntry = new
{
__metadata = new { type = "SP.Data.MessageTestListItem" },
Title = "TestEntry",
FirstName = "Byte",
LastName = "Rodeo",
EmailAddress = "test_test1234.com",
PhoneNumber = "(123) 456-7890",
AlternatePhoneNumber = "(234) 467-8901"
};
//client.InsertItem("MessageTest", testEntry);
}
}
Result
Also Look at
Sharepoint and REST - WebClient - Get List DataSharepoint and REST - WebClient - Update Data to List
Sharepoint and REST - WebClient - Delete Data from List
Comments
Post a Comment