When creating an anonymous sharing link for a notebook section group using the OneNote API, the shared link opens the OneDrive directory listing instead of opening the OneNote web page for the shared section group.
The URL returned from the call to GetOrCreateAnonymousSharingLink
looks like this:
https://company-my.sharepoint.com/:o:/g/personal/aaa_company_onmicrosoft_com/Eas72kadUjRBs9_EmWWWVncBaNLxzujnkFlGRobX6IOBHw
The directory listing displayed has a .one
link and a .onetoc2
link. Clicking either link will open the OneNote site and display the section and its pages.
Shared links that are manually created are in the same format as above, however, they contain a query parameter e.g. e=1zreF2
Should this parameter be included in the URL returned by GetOrCreateAnonymousSharingLink
? Or, have I done something to my Notebooks that might be causing the directory listing to display instead of the actual notebook.
Here's the code:
internal static async Task<string> GenerateSharingLink(string sectionGroupId, SDKHelper.NotebookSecurityRole securityRole) {
string result;
string accessLevel = "{\"accessLevel\":\"Edit\"}";
var accessToken = await SDKHelper.GetAccessToken(SDKHelper.ApiResourceType.OneNoteApi);
string requestUri = $"https://www.onenote.com/api/v1.0/users/aaa@company.onmicrosoft.com/notes/sectiongroups/{sectionGroupId}/Microsoft.OneNote.Api.GetOrCreateAnonymousSharingLink";
var accessLevelJson = new StringContent(JsonConvert.SerializeObject(accessLevel), Encoding.UTF8, "application/json");
using(var client = new HttpClient()) {
client.BaseAddress = new Uri("https://onenote.com");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using(var request = new HttpRequestMessage(HttpMethod.Post, requestUri)) {
request.Content = new StringContent(accessLevel, Encoding.UTF8, "application/json");
using(var response = await client.SendAsync(request)) {
if (!response.IsSuccessStatusCode) {
throw new Microsoft.Graph.ServiceException(
new Error {
Code = response.StatusCode.ToString(),
Message = await response.Content.ReadAsStringAsync()
});
}
result = await response.Content.ReadAsStringAsync();
}
}
}
return result;
}