Consume SharePoint Online REST service using .NET

Overview

Since the introduction of REST interface in SharePoint 2010 you have probably used WebClient or HttpWebRequest in .NET applications.
HttpClient is a modern HTTP client for .NET, it provides a flexible and extensible API for accessing resources via HTTP(S).
HttpClient offers some advantages over WebClient/HttpWebRequest such as:

  1. An HttpClient instance is the place to configure extensions, set default headers, cancel outstanding requests and more.
  2. You can issue as many requests as you like through a single HttpClient instance.
  3. HttpClients are not tied to particular HTTP server or host; you can submit any HTTP request using the same HttpClient instance.
  4. You can derive from HttpClient to create specialized clients for particular sites or patterns
  5. HttpClient uses the new Task-oriented pattern for handling asynchronous requests making it dramatically easier to manage and coordinate multiple outstanding requests.

HttpClient for SharePoint Online

The solution consists of:

  1. SPHttpClient class that inherits from HttpClient and provides some additional SharePoint specific functionaly such as getting request digest
  2. SPHttpClientHandler class that basically hides all the intricacies related to SharePoint Online authentication

SharePoint Online client implementation

SharePoint Online HTTP handler implementation

Working with list items with REST

The following example shows how to retrieve all of a list’s items:

The following example shows how to retrieve a specific list item:

The following example shows how to create a list item:

The following example shows how to update a list item:

The following example shows how to delete a list item: