Hi All,
I have requirement to dynamically create users and assign them customer engagement license on online D365. I am able to create user in D365 systemuser entity but didn,t found way to assign them license.
is there any way to achieve this using C# code.
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Management.Automation.Runspaces;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace PowerApps.Samples
{
public partial class SampleProgram
{
static void Main(string[] args)
{
JObject azureUser = new JObject();
JObject retrievedResult;
string queryOptions = string.Empty;
string domainName = string.Empty;
try
{
Console.WriteLine("Please enter domain name...");
domainName = Console.ReadLine();
string connectionString = ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
using (HttpClient client = SampleHelpers.GetHttpClient(
connectionString,
SampleHelpers.clientId,
SampleHelpers.redirectUrl,
"v9.1"))
{
queryOptions = "systemusers?$select=domainname&$filter=domainname eq '" + domainName + "'";
HttpResponseMessage retrieveResponse = client.GetAsync(client.BaseAddress.AbsoluteUri + queryOptions,
HttpCompletionOption.ResponseHeadersRead).Result;
if (retrieveResponse.IsSuccessStatusCode) //200
{
retrievedResult = JObject.Parse(retrieveResponse.Content.ReadAsStringAsync().Result);
string outputDomainname = (string)retrievedResult.SelectToken("value[0].domainname");
Console.WriteLine("Domain: " + outputDomainname);
if (outputDomainname == null)
{
Console.WriteLine("Create new user...");
HttpRequestMessage createrequest = new HttpRequestMessage(HttpMethod.Post,
client.BaseAddress + "systemusers");
Console.WriteLine("Enter first name");
azureUser.Add("firstname", Console.ReadLine());
Console.WriteLine("Enter last name");
azureUser.Add("lastname", Console.ReadLine());
Console.WriteLine("Enter internal email address");
azureUser.Add("internalemailaddress", Console.ReadLine());
Console.WriteLine("Enter windows live email address");
azureUser.Add("windowsliveid", Console.ReadLine());
azureUser.Add("isdisabled", false);
azureUser.Add("caltype", 7);
azureUser.Add("businessunitid@odata.bind", "/businessunits(8c44c8ac-f6a3-ea11-a812-000d3a0a74cb)");
createrequest.Content = new StringContent(azureUser.ToString());
createrequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
HttpResponseMessage createResponse = client.SendAsync(createrequest, HttpCompletionOption.ResponseHeadersRead).Result;
if (createResponse.IsSuccessStatusCode)
{
string userUri = createResponse.Headers.GetValues("OData-EntityId").FirstOrDefault();
Console.WriteLine("Account created...");
JObject userUpdate = new JObject();
userUpdate.Add("islicensed", true);
userUpdate.Add("issyncwithdirectory", true);
userUpdate.Add("isintegrationuser", true);
HttpRequestMessage updaterequest = new HttpRequestMessage(new HttpMethod("PATCH"), userUri);
updaterequest.Content = new StringContent(userUpdate.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage updateresponse = client.SendAsync(updaterequest, HttpCompletionOption.ResponseContentRead).Result;
if (updateresponse.IsSuccessStatusCode)
{
Console.WriteLine("User updated...");
}
else
{
Console.WriteLine("Failed to update user for reason: {0}",
updateresponse.ReasonPhrase);
}
}
else
{
throw new Exception(string.Format("Failed to Post Records", createResponse.ReasonPhrase));
}
}
else
{
Console.WriteLine("User exists...");
}
}
else
{
Console.WriteLine("Failed to retrieve domain: {0}",
retrieveResponse.ReasonPhrase);
throw new Exception(string.Format("Failed to retrieve domain: {0}", retrieveResponse.Content));
}
}
}
catch (Exception ex)
{
SampleHelpers.DisplayException(ex);
throw ex;
}
finally
{
Console.WriteLine("Press <Enter> to exit the program.");
Console.ReadLine();
}
}
}
}