Last active
November 1, 2015 20:37
-
-
Save vgrem/7eff63ad7d001d0e4c8f to your computer and use it in GitHub Desktop.
SharePoint REST client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using Newtonsoft.Json.Linq; | |
namespace SharePoint.Client.Rest | |
{ | |
/// <summary> | |
/// Simple SharePoint REST client | |
/// </summary> | |
public class SPWebClient : WebClient | |
{ | |
public SPWebClient(string webUrl) | |
{ | |
BaseAddress = webUrl; | |
JsonFormatType = JsonFormatType.Verbose; | |
} | |
/// <summary> | |
/// Perform REST request | |
/// </summary> | |
/// <param name="endpointUrl"></param> | |
/// <param name="method"></param> | |
/// <param name="headers"></param> | |
/// <param name="payload"></param> | |
/// <returns></returns> | |
public JObject ExecuteJson(string endpointUrl, string method, IDictionary<string, string> headers, JObject payload) | |
{ | |
string result; | |
var requestUri = string.Format("{0}{1}", BaseAddress,endpointUrl); | |
PrepareRequest(headers); | |
switch (method) | |
{ | |
case "GET": | |
result = DownloadString(requestUri); | |
break; | |
case "POST": | |
result = payload != null | |
? UploadString(requestUri, method, payload.ToString()) | |
: UploadString(requestUri, method); | |
break; | |
default: | |
throw new NotSupportedException(string.Format("Method {0} is not supported", method)); | |
} | |
return string.IsNullOrEmpty(result) ? new JObject() : JObject.Parse(result); | |
} | |
/// <summary> | |
/// Perform REST request | |
/// </summary> | |
/// <param name="endpointUrl"></param> | |
/// <param name="method"></param> | |
/// <returns></returns> | |
public JObject ExecuteJson(string endpointUrl, string method) | |
{ | |
return ExecuteJson(endpointUrl, method, null, null); | |
} | |
/// <summary> | |
/// Perform REST request | |
/// </summary> | |
/// <param name="endpointUrl"></param> | |
/// <param name="method"></param> | |
/// <param name="headers"></param> | |
/// <param name="payload"></param> | |
/// <returns></returns> | |
public JObject ExecuteJsonWithDigest(string endpointUrl, string method, IDictionary<string, string> headers, | |
JObject payload) | |
{ | |
var formDigestValue = RequestFormDigest(); | |
if (headers == null) | |
{ | |
headers = new Dictionary<string, string>(); | |
} | |
headers.Add("X-RequestDigest", formDigestValue); | |
var result = ExecuteJson(endpointUrl, method, headers, payload); | |
return result; | |
} | |
/// <summary> | |
/// Request Form Digest | |
/// </summary> | |
/// <returns></returns> | |
private string RequestFormDigest() | |
{ | |
var result = ExecuteJson("/_api/contextinfo", "POST", null, null); | |
return result["d"]["GetContextWebInformation"]["FormDigestValue"].ToString(); | |
} | |
private void PrepareRequest(IDictionary<string, string> headers) | |
{ | |
Headers.Clear(); | |
Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); | |
Headers.Add(HttpRequestHeader.ContentType, JsonFormatTable[JsonFormatType]); | |
Headers.Add(HttpRequestHeader.Accept, JsonFormatTable[JsonFormatType]); | |
if (headers != null) | |
{ | |
foreach (var key in headers.Keys) | |
{ | |
Headers.Add(key, headers[key]); | |
} | |
} | |
} | |
/// <summary> | |
/// Upload a file | |
/// </summary> | |
/// <param name="uri"></param> | |
/// <param name="data"></param> | |
/// <returns></returns> | |
public byte[] UploadFile(Uri uri, byte[] data) | |
{ | |
var formDigestValue = RequestFormDigest(); | |
var finalHeaders = new Dictionary<string, string>(); | |
finalHeaders.Add("X-RequestDigest", formDigestValue); | |
PrepareRequest(finalHeaders); | |
var result = UploadData(uri, data); | |
return result; | |
} | |
public JsonFormatType JsonFormatType { get; set; } | |
public static Dictionary<JsonFormatType, string> JsonFormatTable = new Dictionary<JsonFormatType, string> | |
{ | |
[JsonFormatType.Verbose] = "application/json; odata=verbose", | |
[JsonFormatType.MinimalMetadata] = "application/json; odata=minimalmetadata", | |
[JsonFormatType.NoMetadata] = "application/json; odata=nometadata" | |
}; | |
} | |
public enum JsonFormatType | |
{ | |
Verbose, | |
MinimalMetadata, | |
NoMetadata | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment