Create OneNote Client notebook using API

%3CLINGO-SUB%20id%3D%22lingo-sub-1410388%22%20slang%3D%22en-US%22%3ECreate%20OneNote%20Client%20notebook%20using%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1410388%22%20slang%3D%22en-US%22%3E%3CP%3EI%20can%20create%20a%20new%20Web%20OneNote%20Notebook%20using%20the%20following%20code%3A%3C%2FP%3E%3CP%3Estring%20url%20%3D%20%26amp%3B%2334%3B%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fonenote%2Fnotebooks%2F%26amp%3B%2334%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fonenote%2Fnotebooks%2F%26amp%3B%2334%3C%2FA%3E%3B%3B%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20NotebookName%20%3D%20%26amp%3B%2334%3BMyEMR5%26amp%3B%2334%3B%3B%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20tempResponse%20%3D%20await%20CreateSimpleNotebookAsync(NotebookName%2C%20url)%3B%3CBR%20%2F%3E%3CBR%20%2F%3Epublic%20static%20async%20Task%26amp%3Blt%3Bstring%26amp%3Bgt%3B%20CreateSimpleNotebookAsync(string%20notebookName%2C%20string%20apiRoute)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EModels.Globals.authResult%20%3D%20authResult%3B%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20client1%20%3D%20new%20HttpClient()%3B%3CBR%20%2F%3E%3CBR%20%2F%3Eclient1.DefaultRequestHeaders.Accept.Add(new%20MediaTypeWithQualityHeaderValue(%26amp%3B%2334%3Bapplication%2Fjson%26amp%3B%2334%3B))%3B%3CBR%20%2F%3E%3CBR%20%2F%3Etry%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eclient1.DefaultRequestHeaders.Authorization%20%3D%20new%20AuthenticationHeaderValue(%26amp%3B%2334%3BBearer%26amp%3B%2334%3B%2C%20Globals.authResult.AccessToken)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch%20(Exception%20ex)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Estring%20tempEx%20%3D%20ex.ToString()%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20createMessage%20%3D%20new%20HttpRequestMessage(HttpMethod.Post%2C%20apiRoute)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EContent%20%3D%20new%20StringContent(%26amp%3B%2334%3B%7B%20displayName%20%3A%20%26amp%3B%2339%3B%26amp%3B%2334%3B%20%2B%20WebUtility.UrlEncode(notebookName)%20%2B%20%26amp%3B%2334%3B%26amp%3B%2339%3B%20%7D%26amp%3B%2334%3B%2C%20Encoding.UTF8%2C%20%26amp%3B%2334%3Bapplication%2Fjson%26amp%3B%2334%3B)%3CBR%20%2F%3E%7D%3B%3CBR%20%2F%3E%3CBR%20%2F%3EHttpResponseMessage%20response%20%3D%20await%20client1.SendAsync(createMessage)%3B%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20result%20%3D%20await%20response.Content.ReadAsStringAsync()%3B%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20tempNBook%20%3D%20response.Headers.Location.ToString()%20%2B%20%26amp%3B%2334%3B~%26amp%3B%2334%3B%3B%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20regularExpressionPattern1%20%3D%20%26amp%3B%2334%3Bnotebooks%2F(.*%3F)~%26amp%3B%2334%3B%3B%3CBR%20%2F%3ERegex%20regex%20%3D%20new%20Regex(regularExpressionPattern1%2C%20RegexOptions.Singleline)%3B%3CBR%20%2F%3EMatchCollection%20collection%20%3D%20regex.Matches(tempNBook.ToString())%3B%3CBR%20%2F%3E%3CBR%20%2F%3Ereturn%20collection%5B0%5D.Groups%5B1%5D.ToString()%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20I%20want%20to%20change%20the%20target%20and%20create%20the%20Notebook%20in%20the%20OneNote%20client%2C%20I%26amp%3B%2339%3Bve%20tried%20to%20use%20this%20apiRoute%20but%20doesn%26amp%3B%2339%3Bt%20work%3B%3C%2FP%3E%3CP%3E**string%20url%20%3D%20%26amp%3B%2334%3Bonenote%3A%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fonenote%2Fnotebooks%2F%26amp%3B%2334%3B%3B**%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fonenote%2Fnotebooks%2F%26amp%3B%2334%3B%3B**%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1410388%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOnenote%202016%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional Contributor

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/";**

0 Replies