Microsoft Graph订阅:如何获取SharePoint列表更新项信息
你当前的Graph API webhook订阅配置本身没问题,但新版通知默认只传递事件元数据而非完整项数据,需要通过以下步骤获取更新项的具体信息:
1. 调整Webhook接收端,提取事件元数据
Durable Orchestrator无法直接处理HTTP请求的原始内容,建议新增一个HTTP触发的Function作为webhook接收入口,从请求体中解析出更新项的核心标识:
- 请求体是JSON格式,包含
value数组,每个元素对应一条更新事件 - 从
value[].ResourceData.Id可获取更新的列表项ID - 从
value[].Resource字段(格式如sites/{siteId}/lists/{listId}/items/{itemId})可拆分出站点ID、列表ID
示例接收函数代码:
[FunctionName("SharePointWebhookReceiver")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, [DurableClient] IDurableOrchestrationClient starter, ILogger log) { var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var notification = JsonConvert.DeserializeObject<GraphWebhookNotification>(requestBody); foreach (var eventItem in notification.Value) { // 提取关键参数 var itemId = eventItem.ResourceData.Id; var resourceParts = eventItem.Resource.Split('/'); var siteId = resourceParts[1]; var listId = resourceParts[3]; // 启动审批编排并传入参数 await starter.StartNewAsync("startapproval", null, new { SiteId = siteId, ListId = listId, ItemId = itemId }); } return new OkResult(); } // 对应Graph通知的模型类 public class GraphWebhookNotification { public List<NotificationEvent> Value { get; set; } } public class NotificationEvent { public string Resource { get; set; } public ResourceData ResourceData { get; set; } } public class ResourceData { public string Id { get; set; } }
2. 在Durable Orchestration中获取完整项数据
拿到项ID后,在编排的活动函数中调用Graph API获取列表项的完整数据:
- 使用Azure Function的系统托管身份调用Graph API,无需硬编码凭据
- 给托管身份授予SharePoint站点的列表读取权限
示例活动函数代码:
[FunctionName("GetUpdatedListItem")] public static async Task<ListItem> Execute( [ActivityTrigger] dynamic input, ILogger log) { var siteId = input.SiteId; var listId = input.ListId; var itemId = input.ItemId; // 初始化Graph客户端 var credential = new ManagedIdentityCredential(); var graphClient = new GraphServiceClient(credential); // 获取列表项详情 return await graphClient.Sites[siteId].Lists[listId].Items[itemId] .Request() .GetAsync(); }
3. 权限配置说明
- 在Azure Function门户的「身份」设置中,启用系统分配的托管身份
- 通过Graph API或SharePoint管理中心,给该托管身份授予
Sites.Read.All或目标站点的Site.ReadWrite.All应用权限
注意事项
- 旧版Durable Function使用的是SharePoint传统通知服务(返回XML),当前你配置的是Graph API webhook(返回JSON),数据格式已变更
- 确保Function的.NET版本支持
Microsoft.Graph和Azure.IdentityNuGet包
内容的提问来源于stack exchange,提问作者lcarmona
相关产品推荐
相关产品推荐

