none
OneNote API RRS feed

  • Question

  • I can create a new 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/";**





    Fritz Switzer

    Thursday, May 21, 2020 9:38 PM

Answers

  • The REST API (graph.microsoft.com etc) is only available for Notebooks in the cloud.  If you want to tweak your local OneNote setups, you'll have to use the COM interfaces through interop.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by fswitz Saturday, May 23, 2020 1:29 AM
    Friday, May 22, 2020 10:42 PM

All replies

  • Do you receive an error after changing the url?

    Friday, May 22, 2020 4:33 AM
  • Hi fswitz,

    Thank you for posting here.

    If you want to create a new notebook locally, you can install a nuget package: Aspose.Note, and then try the following code.

            static void Main(string[] args)
            {
                Run();
            }
           
            public static void Run()
            {
                // ExStart:CreateDocWithPageTitle
                // The path to the documents directory.
                string dataDir = @"d:\test\onenote\";
    
                // Create an object of the Document class
                Document doc = new Aspose.Note.Document();
                // Initialize Page class object
                Aspose.Note.Page page = new Aspose.Note.Page(doc);
    
                // Default style for all text in the document.
                Aspose.Note.TextStyle textStyle = new TextStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
                // Set page title properties
                page.Title = new Title(doc)
                {
                    TitleText = new RichText(doc) { Text = "Title text.", DefaultStyle = textStyle },
                    TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), DefaultStyle = textStyle },
                    TitleTime = new RichText(doc) { Text = "12:34", DefaultStyle = textStyle }
                };
                // Append Page node in the document
                doc.AppendChildLast(page);
    
                dataDir = dataDir + "CreateDocWithPageTitle_out.one";
                // Save OneNote document
                doc.Save(dataDir);
                // ExEnd:CreateDocWithPageTitle
    
                Console.WriteLine("\nOneNote document created successfully with page title.\nFile saved at " + dataDir);
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 22, 2020 8:44 AM
  • Viorel, 

    Here is the error msg. when I changed the apiRoute to 

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

    System.ArgumentException
      HResult=0x80070057
      Message=Only 'http' and 'https' schemes are allowed.
    Parameter name: requestUri
      Source=System.Net.Http
      StackTrace:
       at System.Net.Http.HttpRequestMessage.InitializeValues(HttpMethod method, Uri requestUri)
       at System.Net.Http.HttpRequestMessage..ctor(HttpMethod method, String requestUri)
       at OneNoteSamplerUWP.MainPage.<CreateNotebookBtn_Click>d__4.MoveNext() in C:\Projects_Sandbox\OneNoteSamplerUWP\OneNoteSamplerUWP\MainPage.xaml.cs:line 62





    • Edited by fswitz Friday, May 22, 2020 2:42 PM
    Friday, May 22, 2020 2:41 PM
  • The REST API (graph.microsoft.com etc) is only available for Notebooks in the cloud.  If you want to tweak your local OneNote setups, you'll have to use the COM interfaces through interop.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by fswitz Saturday, May 23, 2020 1:29 AM
    Friday, May 22, 2020 10:42 PM