Hi, guys. Today I would like to share your guys how to operate CRM Online through O365 APIs. This is a very new topic because O365 APIs just went into Preview program.
Can we create CRM online user using CRM OOB APIs? No, imagine that if you want to create a user in on premise environment, then you should create an account in AD first, and then create an CRM account using created AD account. As same as on premise environment, we also need to create an online AD account in Azure AD first.
OK, let's break up the confused concept. What's O365 APIs? O365 is office 365, Microsoft was migrated all of office suit software e.g. word, excel, sharepoint, exchange and so on into Cloud environment. And they are got very user experience for integration. Image that you just need to buy one software, and then you can use all of competition software on longer do anything for configurations or setups. For O365 APIs, it was expose to end-developer to invoke to control resources of Office 365 such creating an calendar, sending email, creating user, assigning user license and so on.
In this article, we will follow below steps to describe this topic:
1. How to invoke O365 APIs.
Microsoft had published one VS extension tool. It can generate sample codes about how to invoke O365 services.
2. How to use O365 APIs to create CRM Online user
Right now Microsoft was expose oen type APIs called Azure AD graph based web api to us to operate CRM Online user. And there is one helper library can be used to operate CRM Online user, you guys can download it in here. Below is a code snipet about how to user it to create CRM Online user.
string userName = new Random().Next(1000, 9999).ToString(); var verifiedDomains = DDS.tenantDetails.Single().verifiedDomains.ToList(); var verifiedDomainValues = verifiedDomains.Select(it => it.name); ViewData["selectedDomain"] = new SelectList(verifiedDomainValues); Microsoft.WindowsAzure.ActiveDirectory.User user = new Microsoft.WindowsAzure.ActiveDirectory.User(); user.userPrincipalName = string.Format(CultureInfo.InvariantCulture, "{0}@{1}", userName, "crm2014.onmicrosoft.com"); user.mailNickname = userName; user.displayName = userName; user.accountEnabled = true; user.passwordProfile.password = "123456"; DDS.AddTousers(user); DDS.SaveChanges();
3. How to assign O365 license to created CRM Online user
We still can't use the created account after assiging a license to it. So we need to use below code to assign a license to the created account.
string[] queryParameters = { "api-version=2013-11-08" }; string requestUrl = String.Format(CultureInfo.InvariantCulture, "{0}/{1}/users/{2}/assignLicense?{3}", serviceInfo.ApiEndpoint, HttpUtility.UrlEncode(Office365CommonController.TenantId), HttpUtility.UrlEncode(user.userPrincipalName), String.Join("&", queryParameters)); // Prepare the HTTP request: using (HttpClient client = new HttpClient()) { Func<HttpRequestMessage> requestCreator = () => { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); request.Content =new StringContent(@"{ 'addLicenses':[{'disabledPlans':[ ],'skuId':'d17b27af-3f49-4822-99f9-56a661538792'}], 'removeLicenses':[ ]}",System.Text.Encoding.UTF8,"application/json"); return request; }; // Send the request using a helper method, which will add an authorization header to the request, // and automatically retry with a new token if the existing one has expired. using (HttpResponseMessage response = await Office365CommonController.SendRequestAsync( serviceInfo, client, requestCreator)) { // Read the response and deserialize the data: string responseString = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return Office365CommonController.ShowErrorMessage(serviceInfo, responseString); } return View(); } } }
Resources
Office 365 APIsAuthority Documents
http://msdn.microsoft.com/en-us/library/office/dn605892.aspx
Office 365 API forVisual Studio 2013 Extension
http://visualstudiogallery.msdn.microsoft.com/7e947621-ef93-4de7-93d3-d796c43ba34f
Office 365 API Workthrough
http://blogs.msdn.com/b/officeapps/archive/2014/03/12/announcing-office-365-api-tools-for-visual-studio-preview.aspx
Announcing the new version of the Graph API: api-version=2013-11-08( how to assign license to users)
http://blogs.msdn.com/b/aadgraphteam/archive/2013/11/14/announcing-the-new-version-of-the-graph-api-api-version-2013-11-08.aspx>