Obtain OrganizationService using Application User in Dynamics 365

By | October 16, 2018


The Application User is the concept that allows us (developers) programmatic access to Dynamics 365 data and entities. Prior to the introduction of Application User concept we were using licensed user account’s User Name and Password to authenticate to CRM and perform operations.

As a developer, you may have come across with the situation where you had to update the password in the applications whenever licensed user account password gets reset.

Now we can use a non-interactive/ non-licensed user account to authenticate and perform operations in CRM to resolve to update the background applications for password change.

But what if you already have applications running that are still using licensed account for programmatic access to Dynamics 365. Can you now change it use this new Application User approach?

Yes, definitely we can use the Application user concept in our existing applications. We would not need to change complete code we just need to change the way we are obtaining the OrganizationService object.  So your rest of the code will remain the same.

In this blog, we will illustrate how we can obtain OrganizationService using Application User.



  • Application is registered in Azure Active directory and
  • Application User is created in CRM
  • Secret Key is ready to use

If you are new to Application user concept you can refer following articles,



Step 1 – Get Access token

OrganizationUrl will be the CRM URL, resourceURL will be Service Root URL of CRM, clientId will be Client Id of registered application in Azure Active directory and appKey will be Client Secret, as shown in below code.

string organizationUrl = "https://XXXXXXXXX.crm.dynamics.com";
                string resourceURL = "https://XXXXXXXXX.api.crm.dynamics.com" + "/api/data/";
                string clientId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; // Client Id
                string appKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; //Client Secret

                //Create the Client credentials to pass for authentication
                ClientCredential clientcred = new ClientCredential(clientId, appKey);  

                //get the authentication parameters
               AuthenticationParameters authParam = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(resourceURL)).Result;

                //Generate the authentication context - this is the azure login url specific to the tenant
               string authority = authParam.Authority;              

                                //request token
                AuthenticationResult authenticationResult = new AuthenticationContext(authority).AcquireTokenAsync(organizationUrl, clientcred).Result;

                                //get the token              
                                string token = authenticationResult.AccessToken;

Step 2 – Obtain OrganizationService using the access token

Once you get the tokens from above code you have to create OrganizationWebProxyClient object ( sdkService ) and pass this tokens in the HeaderToken of sdkService and

create a  IOrganizationService object (_orgService) and pass this sdkService to _orgService, as shown in below code.

Uri serviceUrl=  new Uri(organizationUrl + @"/xrmservices/2011/organization.svc/web?SdkClientVersion=8.2");

              using (sdkService = new OrganizationWebProxyClient(serviceUrl, false))
                    sdkService.HeaderToken = token;

                  _orgService = (IOrganizationService)sdkService != null ? (IOrganizationService)sdkService : null;

Step 3 – Use this service object in your code


Using the steps above user can Obtain OrganizationService using Application User in Dynamics 365.

Generate Your Own New Leads Within Microsoft Dynamics 365 CRM

Contact us for a demo to know more about how Maplytics can help you to generate new leads from within Microsoft Dynamics 365 CRM.

Maplytics is a 5-star rated, preferred business app on the Microsoft AppSource that is Certified for Microsoft Dynamics 365 (CfMD) and comes with powerful features like Appointment Planning, Sales Routing, Territory Management, Heat Maps, Geo-analytical Dashboards and more that empower organizations to add more value to their CRM data, improve sales & service processes, and achieve high ROI.

Get your free trial from our Website or Microsoft AppSource!

‘If data is the new oil, location intelligence is ??”