This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Configuring SharePoint Operations

Get User Credentials

Create an office365 account using URL https://login.microsoftonline.com/ , and go to SharePoint.

Note

This is a 30-day free trial account.

Follow the below mentioned steps to generate the access token

  • Log in to your Office365 account, and go to the URL:  https://your_site_name.sharepoint.com/_layouts/15/appregnew.aspx

  • Set the following options:

    • App Type - Select An app running on a web server. (You may not have this option)
    • Client Id - Click Generate, and copy the generated value to a text file.
    • Client Secret - Click Generate, and copy the generated value to a text file.
    • Title - Enter a name for the app.
    • App Domain - Enter the domain name.
    • Redirect URL -  Enter the Callback URL.
  • Click Create. Now you can configure the newly created app to access SharePoint resources.
  • Make a Get request with the Authorization header as follows.

    URL: https://your_site.sharepoint.com/_vti_bin/client.svc
    Request Header: Authorization: Bearer

    Find the bearer realm and audience principal ID(client_id) under the WWW-Authenticate response header and save them for future use. audience principal ID is a permanent security principal ID for SharePoint.

  • Get the Authorization code from Azure Access Control Service Construct the authorization url as: https://your_site.sharepoint.com/_layouts/15/OAuthAuthorize.aspx?client_id=client_GUID&scope=app_permissions_list&response_type=code&redirect_uri=redirect_uri
    Set the scope=Web.Manage
  • Once you grant the permission (by clicking trust), SharePoint Online site asks ACS to create an authorization code unique to this combination of user and app. ACS sends the authorization code to the SharePoint site. SharePoint Online site redirects the browser back to the redirect URI that was specified when the app was registered in step 1 and 2. It also includes the authorization code as a query string. The redirect URL is structured as: https://redirect_url/?code=<authcode>
    Extract query string value code from above url and it will be used in next step.
  • Get the access token and refresh token from code:
    Construct the below post request:

    URL: https://accounts.accesscontrol.windows.net/<site_realm>/tokens/OAuth/2
    Post parameters:
    grant_type=authorization_code
    &client_id=<client_id>@<site_realm>
    &client_secret=<client_secret
    &code=<auth_code>
    &redirect_uri=<redirect_url>
    &resource=<audience principal ID>/<site_host>@<site_realm>

    The example for the site_host: your_site_name.sharepoint.com

  • Get the access token from refresh token:
    This step is almost similar to step 7, except 2 differences. Here the difference is that we use:

    • grant_type as refresh_token and
    • refresh_token instead of code in step 7 and use the refresh token which we have saved in step 7.

    URL: https://accounts.accesscontrol.windows.net/<site_realm>/tokens/OAuth/2

Validity

Auth. Code: about 5 minutes.

Access token: 12 hours.

Refresh token: 6 months.

Initializing the Connector

To use the SharePoint connector, add the <sharepoint.init> element in your configuration before any other SharePoint operations. This configuration authenticates with SharePoint by configuring the user credentials using OAuth2 authentication for accessing the Microsoft office365 account that contains the sharepoint. For more information on authorizing requests in SharePoint.

init
<sharepoint.init>
 	<apiUrl>{$ctx:apiUrl}</apiUrl>
 	<accessToken>{$ctx:accessToken}</accessToken>
 	<id>{$ctx:id}</id>
 	<clientId>{$ctx:clientId}</clientId>
 	<clientSecret>{$ctx:clientSecret}</clientSecret>
 	<refreshToken>{$ctx:refreshToken}</refreshToken>
 	<redirectUri>{$ctx:redirectUri}</redirectUri>
 	<resource>{$ctx:resource}</resource>
 	<registryPath>{$ctx:registryPath}</registryPath>
    <blocking>{$ctx:blocking}</blocking>
 </sharepoint.init>
Properties 
  • accessToken:OAuth Token to read and manipulate data of Sharepoint API.
  • apiUrl: Base endpoint URL of Sharepoint API.
  • id:  Realm id, This is a constant GUID for a site.
  • registryPath: Registry Path of the Connector where the values are stored.
  • clientId: The value of your API Key given when you registered your application with Sharepoint and append realm id with client id with this formate <client_id>@<realm_id>.

  • clientSecret: The value of your secret key given when you registered your application with Sharepoint.

  • refreshToken: RefreshToken to be used to obtain the access token.

  • id: Realm id, This is a constant GUID for a site.
  • redirectUri: The redirect url, that you passed to get code.
  • resource: The App ID URI of the web API.

  • blocking: The blocking parameter is helping connector performs the blocking invocations to Sharepoint.


Note

There are 3 approaches to configure the authentication via the init template.

  1. We can use a valid access token. If we go with this, the client credentials (clientId & clientSecret) are optional.
  2. If the refresh token is provided, the access token will be refreshed. Here, the clientId & clientSecret are mandatory.
  3. If both access token and refresh token are not provided, client_credentials flow will be used to generate the access token. The clientId & clientSecret are mandatory for this flow.

Additional information

Ensure that the following Axis2 configurations are added and enabled.

Required message formatters
messageFormatters
<messageFormatter contentType="application/json;odata=verbose" class="org.apache.synapse.commons.json.JsonFormatter"/>
<messageFormatter contentType="application/octet-stream" class="org.apache.axis2.format.BinaryFormatter"/>
Required message builders
messageBuilders
<messageBuilder contentType="application/json;odata=verbose" class="org.apache.synapse.commons.json.JsonBuilder"/>
<messageBuilder contentType="binary/octet-stream" class="org.wso2.carbon.relay.BinaryRelayBuilder"/>


Ensure that
before you call connector methods with blocking mode, the following builders and formatter are added and enabled in axis2_blocking_client.xml.

Required message formatters
messageFormatters
<messageFormatter contentType="application/json;odata=verbose" class="org.apache.synapse.commons.json.JsonStreamFormatter"/>
<messageFormatter contentType="application/octet-stream" class="org.apache.axis2.format.BinaryFormatter"/>
Required message builders
messageBuilders
<messageBuilder contentType="application/json;odata=verbose" class="org.apache.synapse.commons.json.JsonStreamBuilder"/>
<messageBuilder contentType="binary/octet-stream" class="org.wso2.carbon.relay.BinaryRelayBuilder"/>

Now that you have connected to SharePoint, use the information in the following topics to perform various operations with the connector.