如何通过Graph API生成OneNote分区的SharePoint链接?
可以通过Graph API获取必要元数据来构造目标格式的SharePoint链接,无需额外调用其他接口。核心是提取笔记本、分区的ID(去除前缀)、站点URL及分区名称,再按固定格式拼接。
步骤1:修改代码获取关键元数据
调整你的代码,显式请求站点的WebUrl属性,同时提取笔记本、分区的ID和分区名称:
var graphClient = new GraphServiceClient(clientSecretCredential, scopes); // 获取站点并加载WebUrl属性 var site = await graphClient.Sites.GetByPath("/sites/NoteBookName", "companyname.sharepoint.com") .Request() .Select("WebUrl, Id") .GetAsync(); var notebooks = await graphClient.Sites[site.Id] .Onenote .Notebooks .Request() .GetAsync(); var notebook = notebooks.Where(x => x.Id == "1-abcdefgh-2364-4b40-8e33-c6ba539f98cd").FirstOrDefault(); if (notebook != null) { var sections = await graphClient.Sites[site.Id] .Onenote .Notebooks[notebook.Id] .Sections .Request() .GetAsync(); var firstSection = sections.FirstOrDefault(); if (firstSection != null) { // 提取关键信息 var siteWebUrl = site.WebUrl; var notebookIdClean = notebook.Id.Replace("1-", ""); // 去除笔记本ID前缀 var sectionIdClean = firstSection.Id.Replace("1-", ""); // 去除分区ID前缀 var sectionName = firstSection.DisplayName; // 编码并拼接链接 var encodedNotebookId = Uri.EscapeDataString($"{{{notebookIdClean}}}"); var encodedSectionTarget = Uri.EscapeDataString($"{sectionName}.one|{sectionIdClean}/"); var sharePointEditUrl = $"{siteWebUrl}/_layouts/15/Doc.aspx?sourcedoc={encodedNotebookId}&action=edit&wd=target({encodedSectionTarget})&wdorigin=717"; Console.WriteLine(sharePointEditUrl); } }
步骤2:链接参数说明
目标链接各参数对应关系:
sourcedoc:笔记本ID(去除1-前缀后用大括号包裹,URL编码)action=edit:指定以编辑模式打开wd=target(...):格式为分区名称.one|分区ID(去前缀)/,整体URL编码wdorigin=717:固定参数,标识链接来源
构造后的链接可直接在浏览器打开,跳转至对应OneNote分区的SharePoint在线编辑页面,与你提供的示例格式完全匹配。
内容的提问来源于stack exchange,提问作者Dermot
相关产品推荐
相关产品推荐

