MS Graph OneNote 获取分区显示名称
MS Graph OneNote Get Section displayName
我正在尝试使用 MS Graph 获取 OneNote 中某个部分的父笔记本的“显示名称”。我可以使用以下代码获取 Id 和 Body。
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();
但是我找不到任何给我“displayName”的示例代码?
这是 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;
}
}
更新我的答案(再次),所以我找到了获取父笔记本的显示名称的方法。
基本上,返回的 content
对象是您的请求 returns.
的集合
如果您想要一种简单的方法来创建 class 来表示您的 JSON 响应(包括获取 parentNotebook),这里有一个方便的 link:
按照上述方法,我根据JSON 响应创建了一个class,并将其创建的“Value”class 重命名为“Section”。然后我就这样去了:
var content = JObject.Parse(body);
List<Section> sections = new();
foreach (JToken t in content["value"])
{
sections.Add(t.ToObject<Section>());
}
如您所见,这样做基本上会为您提供返回的部分列表以及相应的对象/属性:
这是我为该部分创建的 class 的副本:
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; }
}
希望对您有所帮助!
我正在尝试使用 MS Graph 获取 OneNote 中某个部分的父笔记本的“显示名称”。我可以使用以下代码获取 Id 和 Body。
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();
但是我找不到任何给我“displayName”的示例代码?
这是 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;
}
}
更新我的答案(再次),所以我找到了获取父笔记本的显示名称的方法。
基本上,返回的 content
对象是您的请求 returns.
如果您想要一种简单的方法来创建 class 来表示您的 JSON 响应(包括获取 parentNotebook),这里有一个方便的 link:
按照上述方法,我根据JSON 响应创建了一个class,并将其创建的“Value”class 重命名为“Section”。然后我就这样去了:
var content = JObject.Parse(body);
List<Section> sections = new();
foreach (JToken t in content["value"])
{
sections.Add(t.ToObject<Section>());
}
如您所见,这样做基本上会为您提供返回的部分列表以及相应的对象/属性:
这是我为该部分创建的 class 的副本:
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; }
}
希望对您有所帮助!