protected void Create_CRM_User(string domainname, string firstname, string lastname, string emailid, string contactnumber, ITracingService tracingService, IOrganizationService service)
{
tracingService.Trace("Inside Create_CRM_User");
const string BusinessUnitEntityName = "businessunit";
const string BusinessUnitIdColumnName = "businessunitid";
const string ParentBusinessUnitIdColumnName = "parentbusinessunitid";
const string SystemUserEntityName = "systemuser";
const string FirstNameColumnName = "firstname";
const string DomainNameColumnName = "domainname";
const string LastNameColumnName = "lastname";
const string InternalEmailAddress = "internalemailaddress";
const string TelephoneNumber = "address1_telephone1";
Guid userId = Guid.Empty;
QueryExpression businessUnitQuery = new QueryExpression
{
EntityName = BusinessUnitEntityName,
ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(BusinessUnitIdColumnName),
Criteria =
{
Conditions =
{
new ConditionExpression(ParentBusinessUnitIdColumnName,
ConditionOperator.Null)
}
}
};
// Get the business unit id from the returned entity
EntityCollection entities = service.RetrieveMultiple(businessUnitQuery);
Guid defaultBusinessUnitId = entities[0].Id;
string new_domainname = createuser_domain + domainname;
//Populate an entity with data for a new system user.
Entity entity = new Entity(SystemUserEntityName);
entity.Attributes.Add(DomainNameColumnName, new_domainname);
entity.Attributes.Add(FirstNameColumnName, firstname);
entity.Attributes.Add(LastNameColumnName, lastname);
entity.Attributes.Add(InternalEmailAddress, emailid);
entity.Attributes.Add(TelephoneNumber, contactnumber);
entity.Attributes.Add(BusinessUnitIdColumnName, new EntityReference
{
Id = defaultBusinessUnitId,
Name = BusinessUnitEntityName,
LogicalName = BusinessUnitEntityName
});
userId = service.Create(entity);
string crm_successvar = "Created CRM User with GUID " + userId.ToString();
tracingService.Trace(crm_successvar);
}
This code is done by our senior Techincal Consultant Palani
No comments:
Post a Comment