Forum Discussion

Fritz_S's avatar
Fritz_S
Copper Contributor
May 21, 2020

Create OneNote Client notebook using API

I can create a new Web OneNote Notebook using the following code:

string url = "https://graph.microsoft.com/v1.0/me/onenote/notebooks/";

string NotebookName = "MyEMR5";

string tempResponse = await CreateSimpleNotebookAsync(NotebookName, url);

public static async Task<string> CreateSimpleNotebookAsync(string notebookName, string apiRoute)
{
Models.Globals.authResult = authResult;

var client1 = new HttpClient();

client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

try
{
client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Globals.authResult.AccessToken);
}
catch (Exception ex)
{
string tempEx = ex.ToString();
}

var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute)
{
Content = new StringContent("{ displayName : '" + WebUtility.UrlEncode(notebookName) + "' }", Encoding.UTF8, "application/json")
};

HttpResponseMessage response = await client1.SendAsync(createMessage);

var result = await response.Content.ReadAsStringAsync();

string tempNBook = response.Headers.Location.ToString() + "~";

string regularExpressionPattern1 = "notebooks/(.*?)~";
Regex regex = new Regex(regularExpressionPattern1, RegexOptions.Singleline);
MatchCollection collection = regex.Matches(tempNBook.ToString());

return collection[0].Groups[1].ToString();
}

 

But I want to change the target and create the Notebook in the OneNote client, I've tried to use this apiRoute but doesn't work;

**string url = "onenote:https://graph.microsoft.com/v1.0/me/onenote/notebooks/";**

No RepliesBe the first to reply

Resources