// This function will get triggered/executed when a new message is written // on an Azure Queue called queue. public static void ProcessQueueMessage([QueueTrigger(ChangeManager.StorageQueueName)] NotificationModel notification, TextWriter log) { log.WriteLine($"Processing subscription {notification.SubscriptionId} for site {notification.SiteUrl}"); var changeManager = new ChangeManager(); changeManager.ProcessNotification(notification); }
/// <summary> /// Responds to requests generated by subscriptions registered with /// the SharePoint WebHook REST API. /// </summary> /// <param name="validationToken">The validation token (guid) sent by SharePoint when /// validating the Notification URL for the web hook subscription.</param> public async Task <HttpResponseMessage> Post(string validationToken = null) { // If a validation token is present, we need to respond within 5 seconds by // returning the given validation token. This only happens when a new // web hook is being added if (validationToken != null) { var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(validationToken); return(response); } // Read and parse the request body. var content = await Request.Content.ReadAsStringAsync(); var notifications = JsonConvert.DeserializeObject <ResponseModel <NotificationModel> >(content).Value; if (notifications.Count > 0) { // do something with the received notification ChangeManager changeManager = new ChangeManager(); foreach (var notification in notifications) { // Recommended async pattern: this request must be processed within 5 seconds, // hence it's better to do the processing using an asynchronous pattern. //changeManager.AddNotificationToQueue(CloudConfigurationManager.GetSetting("StorageConnectionString"), notification); // Synchronous pattern: only for very simple operations that always complete within 5 seconds! changeManager.ProcessNotification(notification); } } // if we get here we assume the request was well received return(new HttpResponseMessage(HttpStatusCode.OK)); }
// This function will get triggered/executed when a new message is written // on an Azure Queue called queue. This triggering is done due to the QueueTrigger attribute public static void ProcessQueueMessage([QueueTrigger(ChangeManager.StorageQueueName)] NotificationModel notification, TextWriter log) { log.WriteLine(String.Format("Processing subscription {0} for site {1}", notification.SubscriptionId, notification.SiteUrl)); ChangeManager changeManager = new ChangeManager(); changeManager.ProcessNotification(notification); }
public async void ProcessNotificationTest() { NotificationModel notification = new NotificationModel() { ClientState = Guid.NewGuid().ToString(), ExpirationDateTime = DateTime.Now.AddMonths(6), Resource = "477db145-8939-4684-9ec4-8988ffb0d2b8", SiteUrl = "/teams/CrossSiteApprovalsDemo", SubscriptionId = "ac5f9901-9231-4d8b-ba6f-e3cae3856b30", TenantId = "3aad59e2-dbed-4fc0-9780-91156f9e0bc4", WebId = "d0c0c59a-b834-48f4-aa00-4e23e5b18b47" }; ChangeManager changeManager = new ChangeManager(); await changeManager.ProcessNotification(notification, null); }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info("Notification from webhook received"); try { log.Info("Storage connection string:" + System.Environment.GetEnvironmentVariable("StorageConnectionString")); string validationToken = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "validationtoken", true) == 0) .Value; // If a validation token is present, we need to respond within 5 seconds by // returning the given validation token. This only happens when a new // web hook is being added if (validationToken != null) { log.Info($"Validation token {validationToken} received"); var response = req.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(validationToken); return(response); } // Get notification from body var notificationText = await req.Content.ReadAsStringAsync(); log.Info("Webhook text: " + notificationText); NotificationCollection notifications = JsonConvert.DeserializeObject <NotificationCollection>(notificationText); var notification = notifications.value[0] as NotificationModel; //NotificationModel notification = await req.Content.ReadAsAsync<NotificationModel>(); ChangeManager changeManager = new ChangeManager(); log.Info("Url: " + notification.SiteUrl); await changeManager.ProcessNotification(notification, log); return(req.CreateResponse(HttpStatusCode.OK)); } catch (System.Exception exp) { log.Error("Unable to complete function: " + exp.Message + ":::" + exp.StackTrace); throw exp; } }