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:
A while back ago we already discussed how to consume SharePoint Online (SPO) REST in PowerShell. Here is a brief recap:
This time I would like demonstrate another approach, in particular how PowerShell can gain authorization to SharePoint resources by passing an access token to SharePoint with each HTTP request. To issue an access token from Microsoft Azure Access Control Service (ACS) that allows the app access to the resources in the SharePoint tenancy we will implement the corresponding PowerShell function. Let’s get started.
Getting Access Token from Microsoft Azure Access Control Service
The Get-SPOAccessToken function demonstrates how to obtain the access token from a Microsoft Azure Access Control Service (ACS) account that is associated with the customer’s Microsoft Office 365 tenancy:
Get-SPOAccessToken function is intended for requesting an access token from Azure ACS, it accepts Client Id and Client Secret parameters that are generated while App registration with Azure ACS (see “How to register App” for a more details).
Using Invoke-RestMethod in Office 365
Invoke-SPORestMethod function demonstrates how to include the access token to make a REST API call to SharePoint, passing the OAuth access token in the HTTP Authorization header:
The following example demonstrates how to retrieve List resource properties:
But before running the specified script we need to perform one more step in order to grant permissions to the app principal otherwise the unauthorized error will occur as shown on picture below:
- Navigate to http://<SharePointWebsite>/_layouts/15/AppInv.aspx
- Look up the app based on the Client ID that you just generated and click Lookup, it will find the app principal. Then paste the AppPermissionRequests XML into the Permissions text box and click Create
Once you click Create, the Trust dialog will appear, click Trust
That’s it. Now, after executing the specified script, the output will look like shown below
How to register App
Below is provided a step by step instruction how to register an App, for a complete guide follow this article:
- To create the app identity, navigate to http://<SharePointWebsite>/_layouts/15/AppRegNew.aspx on the tenancy or farm
- Enter values for the form fields as shown below on picture
App ID: App ID, also known as client ID, is a GUID that can be generated (when you click Generate) or pasted into AppRegNew.aspx. The value must be unique for each app, and must be lower case
App Secret: The app secret, also known as the client secret, is an opaque string. It is generated on the AppRegNew.aspx page by using the Generate button. The following is an example of an app secret: Ywjaoz7DJBGhoLQ2t0IbVCA5pfqqI722ZIVt+ENLk0g=
Title: Choose your own user-friendly title; for example, PowerShell Console
App Domain: The host name of the remote component of the app for SharePoint
Redirect URI: The endpoint in your remote application or service to which ACS sends an authentication code
- Click Create on the form. The page will reload and show a confirmation of the values you entered as shown on picture below
- Save Client Id and Client Secret values. After that you could verify whether Get-SPOAccessToken function returns access token. The picture below shows the output after executing the command:
Get-SPOAccessToken -ClientId “1523cf05-b437-4e73-9ad1-a652da8f2ae5” -ClientSecret “Ywjaoz7DJBGhoLQ2t0IbVCA5pfqqI722ZIVt+ENLk0g=” -WebUri “https://contoso.sharepoint.com/”
SharePoint 2013 has introduced Microsoft.SharePoint.Client.Taxonomy namespace for metadata management via CSOM. Since Taxonomy API also allows to manage Keywords, i would like to demonstrate how to get/set Enterprise Keywords field values in this post.
Adding Enterprise Keywords column into List
The first example demonstrates how to add Enterprise Keywords column into List:
Setting Enterprise Keywords field value
The below example demonstrates how to set Enterprise Keywords field value:
- The operation of setting Enterprise Keywords field value consists of two steps, first one to resolve Keyword in Managed Metadata service application (MMS), this what EnsureKeyword method is intended for. After Keyword has been retrieved or created if it not existed, the value of Enterprise Keyword field is set (TaxonomyFieldValueCollection type).
Example 1. Enable jQuery
The following Activate-JQuery.ps1 script demonstrates how to enable jQuery library in Office 365/SharePoint Online site
Example 2. Enable Google Analytics
The following Activate-GoogleAnalytics.ps1 script demonstrates how to activate tracking code in Office 365/SharePoint Online site
Follow these instructions to use Google Analytics to collect data from Office 365/SharePoint Online sites.
To set up the web tracking code:
- Find the tracking code snippet for your property.
Sign in to your Google Analytics account, and click Admin in the top menu bar. From the Account and Propertycolumns, select the property you’re working with. Click Tracking Info / Tracking Code.
<script> and ends with
The tracking code contains a unique ID that corresponds to each Google Analytics property. Don’t mix up tracking code snippets from different properties, and don’t reuse the same tracking code snippet on multiple domains. Click to expand this image and see where the tracking code snippet is in the interface.
- Open Activate-GoogleAnalytics.ps1, paste the tracking code into $TrackingCode variable and run the script to register tracking code in Office 365/SharePoint Online site
- Check your set up.
Make sure that the tracking snippet installed on your website matches the code shown in the view, and see more ways you can verify your set up.