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:
- An HttpClient instance is the place to configure extensions, set default headers, cancel outstanding requests and more.
- You can issue as many requests as you like through a single HttpClient instance.
- HttpClients are not tied to particular HTTP server or host; you can submit any HTTP request using the same HttpClient instance.
- You can derive from HttpClient to create specialized clients for particular sites or patterns
- 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:
- SPHttpClient class that inherits from HttpClient and provides some additional SharePoint specific functionaly such as getting request digest
- 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: