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

I want to setup a webhook so that I get notified when a file has been changed.The file's storage

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204235a4616507.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信