Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

How to create application user by code?

Posted on by 15

I'm creating app in aad.portal.azure and succeed integrate with CRM, but I miss one step that I'm creating application user manually through admin.powerplatform.microsoft, how can I create user by code?

note: I'm using nodejs and for authentication I use url

login.microsoftonline.com/${tenantid}/oauth2/v2.0/authorize?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}&response_mode=query&scope=globaldisco.crm.dynamics.com/.default offline_access

I use scope=globaldisco.crm.dynamics.com/.default because i need to get the crm ApiUrl, but if I use that scope I cant create application user

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: How to create application user by code?

    Hi Firman,

    To create an application user in admin.powerplatform.microsoft using code in Node.js, you will need to use the Microsoft Graph API. The Microsoft Graph API allows you to access and manage various resources within the Power Platform, including users. Here's an example of how you can use the Microsoft Graph API to create an application user:

    1. First, you will need to register an app in Azure Active Directory. This will give you an clientId and clientSecret which will be used for authentication.

    2. Once you have the clientId and clientSecret, you can use the msal library to obtain an access token. Here's an example of how to do this:

      const msal = require("msal");
      
      const clientId = "";
      const clientSecret = "";
      
      const client = new msal.ConfidentialClientApplication({
        auth: {
          clientId: clientId,
          clientSecret: clientSecret,
          authority: "https://login.microsoftonline.com/common",
          tokenCache: new msal.TokenCache()
        }
      });
      
      const scopes = ["https://graph.microsoft.com/.default"];
      
      const getAccessToken = async () => {
        try {
          const authResponse = await client.acquireTokenForClient(scopes);
          return authResponse.accessToken;
        } catch (error) {
          console.log("Error getting access token: "   error);
        }
      };
    1. Once you have the access token, you can use it to make a POST request to the Microsoft Graph API to create the user. Here's an example of how to do this using the request library:

    const request = require("request");
    const token = await getAccessToken();
    
    const options = {
      method: "POST",
      url: "https://graph.microsoft.com/v1.0/users",
      headers: {
        "Authorization": "Bearer "   token,
        "Content-Type": "application/json"
      },
      json: true,
      body: {
        "accountEnabled": true,
        "displayName": "Application User",
        "passwordProfile": {
          "forceChangePasswordNextSignIn": true,
          "password": "Pass@word1"
        },
        "userPrincipalName": "applicationuser@yourdomain.onmicrosoft.com"
      }
    };
    
    request(options, (error, response, body) => {
      if (error) {
        console.log("Error creating user: "   error);
      } else {
        console.log("User created successfully: "   JSON.stringify(body));
      }
    });

    You can also use other libraries like axios or node-fetch for making HTTP call for the above steps

    Please note that this is just an example and the request body may vary based on the required fields of the application user. You will also have to check the permissions of your app registration to ensure that it has the necessary permissions to create an application user. You can refer to the Microsoft Graph API documentation for more information on creating application users and managing users within the Power Platform.

  • Firman Profile Picture
    Firman 15 on at
    RE: How to create application user by code?

    I have tried it but doesn't know how to test in case user application is not created yet because i already create it manually and i cant find how to delete it, I also have tried deactivate user application that I create manually and when try to create by that code it suddenly cant access the CRM dataverse

  • Firman Profile Picture
    Firman 15 on at
    RE: How to create application user by code?

    which tips?

  • tech-44 Profile Picture
    tech-44 on at
    RE: How to create application user by code?

    Creating an application used by code is a simple process that can be complete quickly. The key to creating a practical application user by code is understanding the basic concepts and using them correctly. By following these tips, you can create an efficient and successful user interface for your application.

  • Suggested answer
    RE: How to create application user by code?

    Hi Firman,

    You can create application by code. Please refer the below link

    https://www.inogic.com/blog/2018/07/is-it-possible-to-createupdate-application-user-in-dynamics-365-programmatically/

    If you found this useful, like or verify my answer

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans