Create, Open and Delete OneNote From Windows Phone 8.1

This article describes about how we can create, open and delete an OneNote page from Windows Phone 8.1 app.

Create a universal app project; by creating a Universal app project, you will get two projects, project for windows store and project for Windows Phone by sharing the shared project.

Note: Make sure you install VS 2013 with Update 2 for creating a Universal app project.

If you want to access the OneNote from app, as a first step you should implement the LIVE Authentication.
Please refer the below NullSkull article which describes about the Live Authentication mechanism in detail.

http://nullskull.com/a/10478702/live-authentication-and-onedrive-in-universal-appswindows-storephone-81.aspx

Create a new note:  
Use below Base URI for creating a new note
https://www.onenote.com/api/v1.0/pages

We have to post the new note information as HTTPRequestMessage to the base Uri for creating a new note or page in OneNote.
var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/pages")
                {
                    Content = new StringContent(TitleBuilder().ToString(), System.Text.Encoding.UTF8, "text/html")
                };

From above, HttpMethod type is Post, request Uri is base Uri and the Content is a new note details (note title and DateTime) which has to be framed in Html format as below.
private StringBuilder TitleBuilder()
        {
            StringBuilder tilteBuilder = new StringBuilder();
             tilteBuilder.Append("<html>");
            tilteBuilder.Append("<head>");
            tilteBuilder.Append("<title>");
            tilteBuilder.Append(txtNote.Text);
            tilteBuilder.Append("<meta name='created' content='");
            tilteBuilder.Append(DateTime.Now + "' />");
            tilteBuilder.Append("</head>");
            tilteBuilder.Append("</html>");
            return tilteBuilder;
        }

Post this message with access token (which will be generated during the Live Authentication) to the note uri by using HttpClient.
public async Task<string> SendAsync(string accessToken, System.Net.Http.HttpRequestMessage message)
        {
            HttpClient httpClient = new HttpClient();
             httpClient.DefaultRequestHeaders.Accept.Add(
                 new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.BaseAddress = new Uri("https://www.onenote.com/api/v1.0/pages");
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var response = await httpClient.SendAsync(message);
             if (response.IsSuccessStatusCode)
             {
                 return await response.Content.ReadAsStringAsync();
             }
             return null;
        }

Now you can open the OneNote app, you can find this created note.

Open Note:
For opening the note, DE serialize the response content which will be received when a note has been created successfully.
var responseObject = JsonConvert.DeserializeObject<OneNoteResponse>(response);

From this response you will get the link to open a Note, just give this link to Launcher to launch a note.
await Windows.System.Launcher.LaunchUriAsync(new Uri(responseObject.Links.OneNoteClientUrl.Href));

Delete Note:
Use below base uri for deleting a note.
https://www.onenote.com/api/beta/me/notes/pages/

Prepare the HttpRequestMessage with HttpMethod type as delete, requestUri and noteId(Id can be found from the response which will be received while creating a note)
var deleteMessage = new HttpRequestMessage(HttpMethod.Delete, "https://www.onenote.com/api/beta/me/notes/pages/" + this.responseObject.Id);

Send the message with access token by using HttpClient as below.
public async Task<string> DeleteOneNotePageAsync(string accessToken, System.Net.Http.HttpRequestMessage message)
        {
            HttpClient httpClient = new HttpClient();
             httpClient.DefaultRequestHeaders.Accept.Add(
                 new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var response = await httpClient.SendAsync(message);
             if (response.IsSuccessStatusCode)
             {
                 return await response.Content.ReadAsStringAsync();
             }
             return null;
        }

Download the working sample from below location.
http://nullskull.com/FileUpload/-407123783_LiveOneNote.zip      

By Siva Jagan Dhulipalla   Popularity  (2268 Views)