Search code examples
c#onenote-api

MS Graph OneNote Get Section displayName


I'm trying to get the "displayName" of the parentNotebook of a Section in OneNote using MS Graph. I can get the Id and Body, using the following code.

public static readonly string apiSectionRoute = "https://graph.microsoft.com/v1.0/me/onenote/sections";

 HttpRequestMessage createMessage = new HttpRequestMessage(HttpMethod.Get, apiSectionRoute);

            HttpResponseMessage response = await client.SendAsync(createMessage);

            var apiBaseResponse = new List<ApiBaseResponse>();
            string body = await response.Content.ReadAsStringAsync();
            if (response.StatusCode == HttpStatusCode.OK
                /* GET Notebooks calls always return 200-OK upon success */)
            {
                var content = JObject.Parse(body);
                apiBaseResponse = new List<ApiBaseResponse>(JsonConvert.DeserializeObject<List<GenericEntityResponse>>(content["value"].ToString()));
            }
            if (apiBaseResponse.Count == 0)
            {
                apiBaseResponse.Add(new ApiBaseResponse());
            }

            // Extract the correlation id.  Apps should log this if they want to collect the data to diagnose failures with Microsoft support
            IEnumerable<string> correlationValues;
            if (response.Headers.TryGetValues("X-CorrelationId", out correlationValues))
            {
                apiBaseResponse[0].CorrelationId = correlationValues.FirstOrDefault();
            }
            apiBaseResponse[0].StatusCode = response.StatusCode;
            apiBaseResponse[0].Body = body;

            ///TTD this finds the first one and just happens to be for CWON
            ///

            Globals.CurrentSectionsId=apiBaseResponse[0].Id.ToString();

But I can't find any sample code that gives me the "displayName" ?

Here is the APIBaseResponse class:

 public class ApiBaseResponse
{
    /// <summary>
    /// All OneNote API reponses return a meaningful Http status code
    /// Typical pattern for Http status codes are used:
    /// 1 1xx Informational
    /// 2 2xx Success. e.g. 200-OK for GETs, 201 -Created for POSTs
    /// 3 3xx Redirection
    /// 4 4xx Client Error e.g. 400-Bad Request
    /// 5 5xx Server Error e.g. 500-Internal Server Error
    /// </summary>
    public HttpStatusCode StatusCode { get; set; }

    /// <summary>
    /// Per call identifier that can be logged to diagnose issues with Microsoft support
    /// CorrelationId is included in all Response Headers
    /// </summary>
    public string CorrelationId { get; set; }

    /// <summary>
    /// Body of the OneNote API response represented as a string.
    /// For error cases, this will typically include an error json intended for developers, not for end users.
    /// For success cases, depending on the type API call/HTTP verb this may or may not include a json value
    /// </summary>
    public string Body { get; set; }

    /// <summary>
    /// URLs to launch OneNote rich client/web app
    /// </summary>
    public Links Links { get; set; }

    /// <summary>
    /// Unique identifier of the object
    /// </summary>
    public string Id { get; set; }


}

public class Links
{
    /// <summary>
    /// URL to launch OneNote rich client
    /// </summary>
    public HrefUrl OneNoteClientUrl { get; set; }

    /// <summary>
    /// URL to launch OneNote web experience
    /// </summary>
    public HrefUrl OneNoteWebUrl { get; set; }
}

public class HrefUrl
{
    public string Href { get; set; }
}

public class GenericEntityResponse : ApiBaseResponse
{
    /// <summary>
    /// Name of the entity
    /// </summary>
    public string displayName;

    /// <summary>
    /// Self link to the given entity
    /// </summary>
    public string Self { get; set; }

 
    public List<GenericEntityResponse> Sections { get; set; }

    public List<GenericEntityResponse> SectionGroups { get; set; }

    public override string ToString()
    {
        return "Name: " + displayName + ", Id: " + Id;
    }
}

public class NBookSections
{
    public string displayName { get; set; }
    public string id { get; set; }
}

public class CaroItem
{
    public string CItem
    {
        get;
        set;
    }
}

Solution

  • Updating my answer (yet again), so I found a way to get the parent notebook's displayName.

    Basically the content object that's being returned is a collection of what your request returns.

    If you wanted an easy way to create a class to represent your JSON response (including getting the parentNotebook), here's a handy link: https://stackoverflow.com/a/34303130/9437098

    By following the above method, I created a class based on the JSON response, renamed the "Value" class that it created as "Section". I then went like so:

    var content = JObject.Parse(body);
    
    List<Section> sections = new();
    
    foreach (JToken t in content["value"])
    {
       sections.Add(t.ToObject<Section>());
    }
    

    As you can see, doing it this way will give you basically a list of the sections returned with corresponding objects / properties: Here's a screencap (I set a breakpoint right afterwards to check the value of parentDisplayName):
[![enter image description here][1]][1]

    Here's a copy of the class I created for the Section:

        public class Section
        {
            public string id { get; set; }
            public string self { get; set; }
            public DateTime createdDateTime { get; set; }
            public string displayName { get; set; }
            public DateTime lastModifiedDateTime { get; set; }
            public bool isDefault { get; set; }
            public string pagesUrl { get; set; }
            public Createdby createdBy { get; set; }
            public Lastmodifiedby lastModifiedBy { get; set; }
            public Links links { get; set; }
            public string parentNotebookodatacontext { get; set; }
            public Parentnotebook parentNotebook { get; set; }
            public string parentSectionGroupodatacontext { get; set; }
            public object parentSectionGroup { get; set; }
        }
    
        public class Createdby
        {
            public User user { get; set; }
        }
    
        public class User
        {
            public object id { get; set; }
            public string displayName { get; set; }
        }
    
        public class Lastmodifiedby
        {
            public User1 user { get; set; }
        }
    
        public class User1
        {
            public object id { get; set; }
            public string displayName { get; set; }
        }
    
        public class Links
        {
            public Onenoteclienturl oneNoteClientUrl { get; set; }
            public Onenoteweburl oneNoteWebUrl { get; set; }
        }
    
        public class Onenoteclienturl
        {
            public string href { get; set; }
        }
    
        public class Onenoteweburl
        {
            public string href { get; set; }
        }
    
        public class Parentnotebook
        {
            public string id { get; set; }
            public string displayName { get; set; }
            public string self { get; set; }
        }
    
    

    Hopefully this helps a little!