Wednesday, March 30, 2016

Web API for Microsoft Dynamics CRM Online or internet facing deployments

What is Web API?
Web API is implementation of OData (Open Data Protocol) which is an OASIS standard that defines the best practice for building for building and consuming RESTful APIs.
Web API Authentication
- Javascript Web Resources: You don't need to specify authentication pattern if call is made from the CRM application.
- CRM On-Premise: When you use the Web API for on-premises deployments you must include the user’s network credentials.
- CRM Online or IFD: When you use the Web API for CRM Online or an on-premises Internet-facing deployment (IFD) you must use OAuth.

Steps to register your IFD or CUSTOM CRM application in Azure Active Directory 
Step 1: Login to Windows Azure portal
Step 2: Select your organization and click on applications


It will list down all the application configured in selected organization.

 

Step 3: To Add your CRM application click on Add button.


Step 4: Add an application my organization is developing
Step 5: Enter name of your application

  


*Note: Web application or WEB API is selected for web application and for desktop, console it should be native client application. Since our sample is native client application, hence selected “Native” option
Step 6: Click Next and add redirect URI


Step 7: Once application is added, we need to configure it


Step 8: Add Microsoft dynamics crm permission to this application





Step 9: Assign Delegate Permission to Microsoft Dynamics CRM

 



Step 10: Save the Changes


Step 11: Copy Client ID, that we will use in native client application


Console Application: This sample demonstration will use OAuth authentication to connect CRM Services and your application MyCRM registered in Azure active directory.

 

static void Main(string[] args)
        {
          
            // TODO Substitute your correct CRM root service address,
            string resource = "https://XXXX.api.crm.dynamics.com/";

            // TODO Substitute your app registration values that can be obtained after you
            // register the app in Active Directory on the Microsoft Azure portal.
            string clientId = "db329e3d-4d6d-4ef2-9195-bfad1daed689";
            string redirectUrl = "https://birlasoft22.api.crm.dynamics.com/";


            // Authenticate the registered application with Azure Active Directory.
            AuthenticationContext authContext =
                new AuthenticationContext("https://login.windows.net/common", false);
           
            AuthenticationResult result = authContext.AcquireToken(resource, clientId, new
                                                                   Uri(redirectUrl));
            RunAsync(result).Wait();
        }

        static async Task RunAsync(AuthenticationResult result)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://XXXX.api.crm.dynamics.com/");
                client.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes
                client.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", result.AccessToken);
          
                HttpResponseMessage response = await client.GetAsync("api/data/v8.0/accounts");

                if (response.IsSuccessStatusCode)
                {
                    string s = await response.Content.ReadAsStringAsync();
                    Console.Write(s);
                }

                Console.ReadKey();
            }

        }

Make sure to add nuGet package “Active Directory Authentication Library” in your project.


Output
: Since we have not passed credentials in code, login window opens, here enter your credentials.


Once you sign-in JSON response will be returned.

 

Helpful Links:
https://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-store-dotnet-adal-sso-authentication/
https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/
https://msdn.microsoft.com/en-us/library/gg327838.aspx
https://msdn.microsoft.com/en-us/library/mt595798.aspx
https://msdn.microsoft.com/en-us/library/mt595799.aspx

9 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in MICROSOFT DYNAMICS CRM kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on MICROSOFT DYNAMICS CRM We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Saurabh Srivastava
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383
    http://www.maxmunus.com/

    ReplyDelete
  2. Hi I have developed as is and is working fine in local but when deployed in azure it doesn't work, how do we pass credentials

    ReplyDelete
  3. Great! This is really informative blog about Crm Microsoft. Your blog is really good.

    ReplyDelete
  4. Amazing information that you have shared with us, your blog is really full of knowledge about business analyst. This amazingly help me to understand Microsoft CRM consultant skills. Thank you so much for sharing this with us.

    ReplyDelete
  5. Great! This is really informative blog about Crm Microsoft. Your blog is really good.

    customized crm solution

    ReplyDelete
  6. outsourcingall.com "Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it.
    This paragraph gives clear idea for the new viewers of blogging, Thanks you. You’re doing a great job Man, Keep it up.
    Seo training
    Seo training in dhaka
    SEO Services in bangladesh

    ReplyDelete
  7. I always like and such a super contents of these post, Good info I gained more information about that. Thanks for such a helpful information. Keep it up.
    CRM Software in Dubai
    CRM Software
    CRM Software in UAE
    Best CRM Software
    Customer Relationship Management Software

    ReplyDelete
  8. I always curious to know more about how we can enhance our skill, how we can improve our portfolio to add some attractive certificate. I got some valuable points through this blog.
    best crm software

    ReplyDelete