简体   繁体   中英

How to create a sharepoint link for a onenote section, using Graph API

I am using Graph API in.Net to access a OneNote notebook in a client's Sharepoint. So far, so good. I can access the notebook, and loop through sections.

However, the requirement is for me to return a sharepoint link for a notebook section, in the form https://companyname.sharepoint.com/sites/NotebookName/_layouts/15/Doc.aspx?sourcedoc={7636j9a7-2364-4b40-8h33-c6ba539f98cd}&action=edit&wd=target%28SectionName.one%7C5jd216c9-bcn4-43e2-896e-86p2b765a415%2F%29&wdorigin=717

Using Graph API, I get a URL like this one: https://graph.microsoft.com/v1.0/sites/companyname.sharepoint.com,81ba2d09-8378-5d84-b365-9gd9bb83f9c3,f5bd4b76-7796-4354-8d53-07d5dbb843f6/onenote/sections/1-328ab290-37bd-4495-9009-2437bef11d54/pages

I am unable to get from the graph-api url to the sharepoint url. Is it possible to construct a sharepoint url like the one above using Graph API?

Here is the code I'm using:

`

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

            var site = await graphClient.Sites.GetByPath("/sites/NoteBookName", "companyname.sharepoint.com")
                .Request()
                .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)
                {
                    Console.WriteLine(firstSection.Self);
                } 
            }

`

Thanks, Dermot

I have drilled down into the notebook as per the code above, but am unable to construct a sharepoint URL.

I found the answer to this, and it was a lot more straightforward than I expected. The Sharepoint URL is stored in the section object, in Links.OneNoteWebUrl.

Hopefully this saves someone else from all the head-scratching I did over the past few weeks.

var sections = await graphClient.Sites[site.Id]
.Onenote
.Notebooks[notebook.Id]
.Sections
.Request()
.GetAsync();

foreach (var section in sections)
{
    Console.WriteLine(section.DisplayName);
    Console.WriteLine(section.Links.OneNoteWebUrl.Href);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM