Custom routing based on SharePoint and Azure AD (Office 365) groups

You can configure routing by user group using automatic routing. But if you need more complex conditions including not only user membership but also field values, profile properties, or other extra clauses, use programmatic routing.

This page provides code examples for routing based on user membership and current item field values.

Routing based on SharePoint security groups

You can use custom routing and PnPjs library to check if the current user is a member of SharePoint group.

Redirect to the custom Form Set depending on the user membership and Status field value:

var userGroups = []
// get all groups the current user belongs to
return web.currentUser.groups().then(function(groups) {
    for (var i = 0; i < groups.length; i++) {
        userGroups.push(groups[i].Title);
    }

    return item.get();
}).then(function (item) {
    //check user membership and item Status
    if (userGroups.indexOf('Managers') >= 0 && item.Status === 'Waiting for approval') {
        return '9c49268c-8d61-4069-99b3-ff394cbb07dd';
    }
    if (userGroups.indexOf('Managers') >= 0 && item.Status === 'Approved') {
        return '830f561b-544b-42da-9f86-774e64503183';
    }

});

Routing based on Azure AD (Office 365) groups

You can use custom routing and Microsoft Graph client to retrieve Office365 groups and check the current user membership.

Important

In order to retrieve user profile properties with Graph API, you first need to make sure that the Microsoft Graph client has the permissions to check if the current user belongs to specific 365 groups of your tenant.

For this, make sure to install Microsoft 365 CLI (you’ll need to install Node.js first in order to do that).

Connect it to your Office 365 tenant from the command line:

m365 login

And then use the following line to give Microsoft Graph the required permissions:

m365 spo serviceprincipal grant add --resource "Microsoft Graph" --scope "Directory.Read.All"

We check the user’s groups and only redirect user to the Form if they belong to the HelpDesk Agents group, using the following code, just replacing Form Set ID with the one that we’ve copied on the form:

var userGroups = []
// get all groups the current user belongs to
return graph.me.memberOf().then(function(groups) {
    for (var i = 0; i < groups.length; i++) {
            userGroups.push(groups[i].Title);
        }
    return item.get();
}).then(function (item) {
    //check user membership and item Status
    if (userGroups.indexOf('Managers') >= 0 && item.Status === 'Waiting for approval') {
        return '9c49268c-8d61-4069-99b3-ff394cbb07dd';
    }
    if (userGroups.indexOf('Managers') >= 0 && item.Status === 'Approved') {
        return '830f561b-544b-42da-9f86-774e64503183';
    }

});