Dialogue Cloud

How to authenticate SharePoint Online without a user in Dialogue Studio

Accessing SharePoint Online REST API requires an access token. This guide will tell you how to achieve this. In this example we will be looking up a contact.

Prerequisites

Get an access

First step is generating access token. We start with a function node.

Steps:

  1. Drag and drop a Function node.

  2. Open Function node.

  3. Enter the following function:

    Note: Replace the following:
    • [TenantID] = Directory (Tenant) ID

    • [ClientID] = SharePoint App Client ID

    • [ClientSecret] = SharePoint App Client Secret, Make sure ULR encode.
      https://www.url-encode-decode.com/

    • [Resource] = yourtenantname.sharepoint.com

    Copy
    Function
    msg.credentials =
    {
    "realm": "[TenantID]",
    "client_id":"[ClientID]",
    "client_secret":"[ClientSecret]",
    "resource":"[Resource]",
    "principal" : "00000003-0000-0ff1-ce00-000000000000"
    }
    msg.payload = "grant_type=client_credentials&client_id=" + msg.credentials.client_id + "@" + msg.credentials.realm + "&client_secret=" + msg.credentials.client_secret + "&resource=" + msg.credentials.principal + '/' + msg.credentials.resource + "@" + msg.credentials.realm
    return msg;

Now that we prepared our message we can send the request. This is done using a HTTP Request node.

Steps:

  1. Drag and drop a http request node.

  2. Open Node:

    1. Method = Post

    2. URL = https://accounts.accesscontrol.windows.net/[TenantID]/tokens/OAuth/2

      Note: Replace [TenantID] with Directory (Tenant) ID
    3. Return = a parsed JSON object

  3. Connect the end of the Function node to the begin of the HTTP request node.

Next we want to check if our output was successful. This can be done with a Switch node.

Steps:

  1. Drag and drop a Switch Node.

  2. Open Node:

    1. Change Property to msg.statusCode.

    2. Add an option for a OK response.

      == 200

    3. Add the option otherwise for any other response.

  3. Connect end of the HTTP request node to begin of the Switch Node.

If the response was 200 OK then we can store the tokens. Note that an access token is only valid for one hour, for that reason we also store the expiration time. This can be done with a Function node.

Steps:

  1. Drag and drop a Function node.

  2. Open Function node.

  3. Enter the following function:

    Copy
    Function
    var access_token = msg.payload.access_token
    flow.set('at', access_token);
    var date = new Date();
    msg.date = new Date();
    flow.set('expire', new Date(date.setHours(date.getHours()+1)));
    return msg;
  4. Connect "== 200" of the Switch node to begin of the Function node.

Now we have our token and we can start using it.

Call SharePoint Online with the access token

With our tokens in memory we can call Dynamics 365 Before we start we need to check if the token is still active. We will do this with a Function node.

Steps:

  1. Drag and drop a Function node.

  2. Open Function node.

  3. Enter the following function:

    Copy
    Function
    var date = new Date();
    msg.date = new Date();
    return msg;

Next we need to check if our expire time is not past the current time. This is done with a Switch node.

Steps:

  1. Drag and drop a Switch Node.

  2. Open Node:

    1. Change Property to flow.expire

    2. Add an option for if not expired

      >= msg.date

    3. Add the option otherwise for expired

  3. Connect end of the Function node to begin of the Switch Node.

We are going to continue on the not expired flow. The expired flow will loop back to chapter Request an access token.

Our next step is to prepare the request, this is done using a Function node.

Steps:

  1. Drag and drop a Function node.

  2. Open Function node.

  3. Enter the following function:

    Copy
    Function
    msg.headers = {
    "Authorization": "Bearer " + flow.get('at'),
    "Accept": "application/json",
    };
    return msg;
  4. Connect ">= msg.date" of the Switch node to begin of the Function node.

Now that we prepared our message we can send the request. This is done using a HTTP Request node.

Steps:

  1. Drag and drop a http request node.

  2. Open Node:

    1. Method = Post

    2. URL = Example url to get all agent uri's:

      Copy
      URL
      https://[TenantName].sharepoint.com/[PathUccSite]/_api/web/lists/getbytitle('Agents')/items?$select=wsp_ucc_Agent
      Note: Replace the following:
      • [TenantName] = Your Tenant name, example : contoso

      • [PathUccSite] = For example: sites/ucc/uccsupport

    3. Return = a parsed JSON object

  3. Connect the end of the Function node to the begin of the HTTP request node.

The output is stored in msg.payload and can be used in the rest of your flow. You can use the following expression to greet the customer by name: