0

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;
}
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • There are 3 hits on Google for GetOrCreateAnonymousSharingLink. One is this post and another is the MSDN documentation for the API call. Should an alternate API call be used to create a sharing link for a SectionGroup? –  May 15 '18 at 17:23
  • @MarcLaFleur I'm not having any luck on SO for this issue and wondering if it might be a known bug. Seeing OneDrive instead of OneNote when clicking a shared link is not the expected behavior. Any guidance you can provide would be appreciated. –  May 22 '18 at 18:23
  • I'm not an expert on the legacy OneNote API but I seem to recall that `GetOrCreateAnonymousSharingLink` was limited to just Section Groups (not normal sections, pages or notebooks). Is it possible this wasn't a section group? – Marc LaFleur May 22 '18 at 19:21
  • It's a SctionGroup. `code SectionGroup sectionGroup = new SectionGroup { DisplayName = displayName }; return await client.Users[ME].Onenote.Notebooks[notebookId].SectionGroups.Request().AddAsync(sectionGroup); code` –  May 23 '18 at 18:15
  • @MarcLaFleur Definitely a SecionGroup. I can easily switch to the graph api however the approach I'm using worked and is no longer working. Is this the correct way to do it using the graph? `code POST ../sites/{site-id}/drive/items/{item-id}/createLink code` –  May 23 '18 at 18:25

0 Answers0