Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
210 lines (188 sloc) 10.5 KB
using Newtonsoft.Json;
using SharePoint.WebHooks.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace SharePoint.WebHooks.Common
{
/// <summary>
/// Class with methods to manage SharePoint web hooks
/// </summary>
public class WebHookManager
{
#region Add a list web hook
/// <summary>
/// This method adds a web hook to a SharePoint list. Note that you need your webhook endpoint being passed into this method to be up and running and reachable from the internet
/// </summary>
/// <param name="siteUrl">Url of the site holding the list</param>
/// <param name="listId">Id of the list</param>
/// <param name="webHookEndPoint">Url of the web hook service endpoint (the one that will be called during an event)</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <param name="validityInMonths">Optional web hook validity in months, defaults to 3 months, max is 6 months</param>
/// <returns>subscription ID of the new web hook</returns>
public async Task<SubscriptionModel> AddListWebHookAsync(string siteUrl, string listId, string webHookEndPoint, string accessToken, int validityInMonths = 3)
{
string responseString = null;
using (var httpClient = new HttpClient())
{
string requestUrl = String.Format("{0}/_api/web/lists('{1}')/subscriptions", siteUrl, listId);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Content = new StringContent(JsonConvert.SerializeObject(
new SubscriptionModel()
{
Resource = String.Format("{0}/_api/web/lists('{1}')", siteUrl, listId.ToString()),
NotificationUrl = webHookEndPoint,
ExpirationDateTime = DateTime.Now.AddMonths(validityInMonths).ToUniversalTime(),
}),
Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
responseString = await response.Content.ReadAsStringAsync();
}
else
{
// Something went wrong...
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
return await Task.Run(() => JsonConvert.DeserializeObject<SubscriptionModel>(responseString));
}
#endregion
#region Update a list web hook
/// <summary>
/// Updates the expiration datetime (and notification URL) of an existing SharePoint list web hook
/// </summary>
/// <param name="siteUrl">Url of the site holding the list</param>
/// <param name="listId">Id of the list</param>
/// <param name="subscriptionId">Id of the web hook subscription that we need to update</param>
/// <param name="webHookEndPoint">Url of the web hook service endpoint (the one that will be called during an event)</param>
/// <param name="expirationDateTime">New web hook expiration date</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <returns>true if succesful, exception in case something went wrong</returns>
public async Task<bool> UpdateListWebHookAsync(string siteUrl, string listId, string subscriptionId, string webHookEndPoint, DateTime expirationDateTime, string accessToken)
{
using (var httpClient = new HttpClient())
{
string requestUrl = String.Format("{0}/_api/web/lists('{1}')/subscriptions('{2}')", siteUrl, listId, subscriptionId);
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Content = new StringContent(JsonConvert.SerializeObject(
new SubscriptionModel()
{
NotificationUrl = webHookEndPoint,
ExpirationDateTime = expirationDateTime.ToUniversalTime(),
}),
Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.StatusCode != System.Net.HttpStatusCode.NoContent)
{
// oops...something went wrong, maybe the web hook does not exist?
throw new Exception(await response.Content.ReadAsStringAsync());
}
else
{
return await Task.Run(() => true);
}
}
}
#endregion
#region Delete a list web hook
/// <summary>
/// Deletes an existing SharePoint list web hook
/// </summary>
/// <param name="siteUrl">Url of the site holding the list</param>
/// <param name="listId">Id of the list</param>
/// <param name="subscriptionId">Id of the web hook subscription that we need to delete</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <returns>true if succesful, exception in case something went wrong</returns>
public async Task<bool> DeleteListWebHookAsync(string siteUrl, string listId, string subscriptionId, string accessToken)
{
using (var httpClient = new HttpClient())
{
string requestUrl = String.Format("{0}/_api/web/lists('{1}')/subscriptions('{2}')", siteUrl, listId, subscriptionId);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, requestUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.StatusCode != System.Net.HttpStatusCode.NoContent)
{
// oops...something went wrong, maybe the web hook does not exist?
throw new Exception(await response.Content.ReadAsStringAsync());
}
else
{
return await Task.Run(() => true);
}
}
}
#endregion
#region Get defined list web hooks
/// <summary>
/// Get all webhooks on a given SharePoint list
/// </summary>
/// <param name="siteUrl">Url of the site holding the list</param>
/// <param name="listId">Id of the list</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <returns>Collection of <see cref="SubscriptionModel"/> instances, one per returned web hook</returns>
public async Task<ResponseModel<SubscriptionModel>> GetListWebHooksAsync(string siteUrl, string listId, string accessToken)
{
string responseString = null;
using (var httpClient = new HttpClient())
{
string requestUrl = String.Format("{0}/_api/web/lists('{1}')/subscriptions", siteUrl, listId);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
responseString = await response.Content.ReadAsStringAsync();
}
else
{
// oops...something went wrong
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
return await Task.Run(() => JsonConvert.DeserializeObject<ResponseModel<SubscriptionModel>>(responseString));
}
/// <summary>
/// Get a specific webhook for a given SharePoint list
/// </summary>
/// <param name="siteUrl">Url of the site holding the list</param>
/// <param name="listId">Id of the list</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <returns>Collection of <see cref="SubscriptionModel"/> instances, one per returned web hook</returns>
public async Task<SubscriptionModel> GetListWebHookAsync(string siteUrl, string listId, string subscriptionId, string accessToken)
{
string responseString = null;
using (var httpClient = new HttpClient())
{
string requestUrl = String.Format("{0}/_api/web/lists('{1}')/subscriptions('{2}')", siteUrl, listId, subscriptionId);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
responseString = await response.Content.ReadAsStringAsync();
}
else
{
// oops...something went wrong, maybe the web hook does not exist?
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
return await Task.Run(() => JsonConvert.DeserializeObject<SubscriptionModel>(responseString));
}
#endregion
}
}