How to create Power Platform Custom Connector using Graph API to Update Azure Active Directory User’s Information?
Introduction:
Power Platform is a collection of tools that allows users to easily create custom business applications. One of the key components of Power Platform is the ability to create custom connectors, which allow users to connect to external data sources and perform actions on them. In this tutorial, we will guide you through the process of creating a custom connector using Graph API to update Azure Active Directory User’s information using Power Apps.
Prerequisites:
Before we begin, make sure you have the following prerequisites:
- An Azure subscription with Active Directory configured
- A Power Apps environment
- An Azure AD user account with permissions to manage users
- An Azure AD application with Graph API permissions
Step 1: Create an Azure AD application
The first step in creating a custom connector is to create an Azure AD application. This application will be used to authenticate and authorize the connector to access the Graph API. Follow these steps to create a new Azure AD application:
- Sign in to the Azure portal using your Azure AD account.
1.1 Navigate to the “App registrations” tab and click on the “New registration” button.
1.2 Enter a “Name” for the application (it can be any name that suitable to your project) and select the option “Accounts in this organizational directory only” for the Supported account types.
- Enter a valid redirect URI (such as “https://localhost:1234“) and click on the “Register” button
1.3 Note down the “Application (client) ID” and “Client Secret” values as you will need them later.
1.4 Create a “New client secret”
- Navigate to the “Certificates & secrets” tab and click on the “New client secret” button.
- Write the description on “Description” field and click on “Add” button.
- Note down the “Client secrets” value.
Once the Azure AD application is created, we need to configure the Microsoft Graph API permissions that the connector will need to access.
Step 2: Configure Graph API permissions
Follow these steps to configure Graph API permissions:
2. Navigate to the “API permissions” tab for the Azure AD application.
- Click on the “Add a permission” button.
- Select “Microsoft Graph” and then select the “Application permissions” option.
- Scroll down to the “User” section and select the “User.ReadWrite.All” permission.
- Click on the “Add permissions” button to save the changes.
- Finally, “Grant admin consent for …” the permissions.
Now, that the Azure AD application is created and Graph API permissions are configured, we can create a custom connector in Power Apps.
Step 3: Create a custom connector in Power Apps
Follow these steps to create a custom connector:
3. Sign in to Power Apps and navigate to the Left side menu.
- Click on the “Custom connectors” option and then click on the “New custom connector” button.
3.1 Enter a name for the custom connector and select “HTTPS” as the “Scheme” type.
- “Host” should be: graph.microsoft.com
- “Base URL”: Leave it as it is.
3.2 Enter the following information for the Security Section:
- Authentication type: OAuth 2.0
- Identity Provider: Azure Active Directory
- Provide the Client ID and Client Secret obtained earlier in Step 1.
- Authorization URL: Leave it as it is.
- Tenant ID: Leave it as it is.
- Resource URL: https://graph.microsoft.com
- Enable on-behalf-of login: Leave it as it is.
- Set the scope to ‘User.ReadWrite.All‘.
- Redirect URL: Leave it as it is.
- Click on the “Create connector” button.
After creating the custom connector, we need to define the actions that the connector can perform. In this tutorial, we will create an action to update a user’s information in Azure AD.
Step 4: Define actions for the custom connector
4. Follow these steps to define the action:
- In the custom connector editor, click on the “New action” button.
- Enter a name for the Summary, Description and Operation ID (such as “Update user”).
- Click on Import from sample and Select the “PATCH” method and enter the following URL in the “URL” field: https://graph.microsoft.com/v1.0/users/{id}
- Copy following two lines in the Headers section:
Content-Type application/json
Accept application/json
- Define the Body of the request to include the user properties to be updated. For example, to update the user’s name, jobTitle, Phone, and Department, the body could be as following:
{
'displayName': 'New Display Name',
'jobTitle': 'New Job Title',
'businessPhones': [
'555-555-5555'
],
'department': 'New Department'
}
Click on the “Import” button and Test the action to ensure that it updates the user’s information
correctly.
Step 5: Test the custom connector using Power apps
Create a new Power App or Flow and use the custom connector to update a user’s information. To Update from the Power Apps, We need to do following:
- Add or connect custom connector on the power apps
- Add Button on the Power App Screen
- OnSelect properties of the button we need to provide following information,
1. Name of the custom connector
2. Name of the Action()
3. UserID
4. Headers
5. Body (Name of properties that need to be update)
For example:
Name of the Custom Connector is: "graphAPI"
Name of the Action () is "UpdateUserInfo"
"UserID" we can grab by doing follow:
Add Label Control on the Power Apps Screen give it a name "userID" and paste following code on "Text" properties of Label Control:
"Office365Users.UserProfile(User().Email).Id"
Headers are: 'Content-Type':"application/json",
Accept: "application/json"
Then Provide the Body that we need to update the info.
If we want to change the Department of the Person. Our code should be like this:
OnSelect properties of the Button control:
graphAPI.UpdateUserInfo(
//UserID
userID.Text,
{
//Headers
'Content-Type': "application/json",
Accept: "application/json",
department: Label_department.Text or "just type the name of the department"
})
Click on the Button it should work.
Verify that the user's information is updated in Azure AD.
That’s it! We just have created a custom Power Platform connector using Graph API to update Azure Active Directory users’ information such as name, jobTitle, phone, and department etc.