最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Microsoft Graph notification not called when SharePoint file updated - Stack Overflow

programmeradmin0浏览0评论

I want to setup a webhook so that I get notified when a file has been changed. The file's storage location can be a OneDrive (business) or SharePoint document library.

I can poll the file and get the last modified date:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    var response = await client.GetAsync($".0/sites/{siteId}/drive/items/{fileId}");
    var content = await response.Content.ReadAsStringAsync();
    var json = JsonSerializer.Deserialize<JsonDocument>(content);
    return json.RootElement.GetProperty("lastModifiedDateTime").GetString();
}

I want a notification instead of polling. The Microsoft Graph docs show a Notification should be possible by looking for changes on the root of a drive or a list such as this:

Authenticate in Graph Explorer and POST to .0/subscriptions with the following:

{
  "changeType": "updated",
  "notificationUrl": ";,
  "resource": "drives/{drive-id}/root",
  "expirationDateTime": "2025-02-13T18:23:45.9356913Z",
  "clientState": "Foo"
}
**or**
{
  "changeType": "updated",
  "notificationUrl": ";,
  "resource": "sites/{site-id}/lists/{list-id}",
  "expirationDateTime": "2025-02-13T18:23:45.9356913Z",
  "clientState": "Foo"
}

And the associated called API (asp core 8):

[HttpPost]
public async Task<IActionResult> Post([FromQuery] string validationToken)
{
   _logger.LogInformation("C# HTTP trigger function processed a request.");

   // Handle webhook validation
   if (!string.IsNullOrEmpty(validationToken))
      return Content(validationToken, "text/plain"); 

   string requestBody;
   using (var reader = new StreamReader(Request.Body))
   {
      requestBody = await reader.ReadToEndAsync();
   }

   dynamic data = JsonConvert.DeserializeObject(requestBody);

My api gets called for the POST subscription registration but not when a file or list item is modified, created, or deleted.

API permissions:

Why is the API not being called when a file is changed?

I want to setup a webhook so that I get notified when a file has been changed. The file's storage location can be a OneDrive (business) or SharePoint document library.

I can poll the file and get the last modified date:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    var response = await client.GetAsync($"https://graph.microsoft/v1.0/sites/{siteId}/drive/items/{fileId}");
    var content = await response.Content.ReadAsStringAsync();
    var json = JsonSerializer.Deserialize<JsonDocument>(content);
    return json.RootElement.GetProperty("lastModifiedDateTime").GetString();
}

I want a notification instead of polling. The Microsoft Graph docs show a Notification should be possible by looking for changes on the root of a drive or a list such as this:

Authenticate in Graph Explorer and POST to https://graph.microsoft/v1.0/subscriptions with the following:

{
  "changeType": "updated",
  "notificationUrl": "https://myapi.azurewebsites/api/fileWebhook",
  "resource": "drives/{drive-id}/root",
  "expirationDateTime": "2025-02-13T18:23:45.9356913Z",
  "clientState": "Foo"
}
**or**
{
  "changeType": "updated",
  "notificationUrl": "https://myapi.azurewebsites/api/fileWebhook",
  "resource": "sites/{site-id}/lists/{list-id}",
  "expirationDateTime": "2025-02-13T18:23:45.9356913Z",
  "clientState": "Foo"
}

And the associated called API (asp core 8):

[HttpPost]
public async Task<IActionResult> Post([FromQuery] string validationToken)
{
   _logger.LogInformation("C# HTTP trigger function processed a request.");

   // Handle webhook validation
   if (!string.IsNullOrEmpty(validationToken))
      return Content(validationToken, "text/plain"); 

   string requestBody;
   using (var reader = new StreamReader(Request.Body))
   {
      requestBody = await reader.ReadToEndAsync();
   }

   dynamic data = JsonConvert.DeserializeObject(requestBody);

My api gets called for the POST subscription registration but not when a file or list item is modified, created, or deleted.

API permissions:

Why is the API not being called when a file is changed?

Share Improve this question edited Mar 24 at 19:06 Coden00b asked Feb 12 at 20:22 Coden00bCoden00b 3522 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To set up a webhook for notifications when a file is updated in OneDrive (business) or SharePoint, you need to ensure that you have correctly registered the subscription and that your application is set up to handle incoming notifications. Here are some steps and considerations to help troubleshoot why your API is not receiving notifications:

1.Subscription Registration: Make sure that your subscription is correctly registered for the specific resource you want to monitor (e.g., a drive or a list). The you provide must be publicly accessible and must support HTTPS.

2.Authorization: Verify that the user has authorized your app to access the OneDrive or SharePoint content. If the app does not have the necessary permissions, it will not receive notifications.

3.Testing: You can test the webhook by manually updating a file or list item to see if your endpoint receives the notification.

发布评论

评论列表(0)

  1. 暂无评论