Hello,
I am trying to get the current logged in user or create a user and assign it to the look up field using client services in a windows form. But it come up with the error -"An Error Occurred". My code can be seen below:
//creating a user SystemUser systemUser = new SystemUser(); systemUser.FirstName = "Victor"; systemUser.LastName = "*******"; Guid systemID = (Guid)Service().Create(systemUser); systemUser = (SystemUser)Service().Retrieve(systemUser.LogicalName, systemID, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname")); //this field is a lookup field to User. Assigned to the user created above clinic.new_ClinicCoordinator = new CrmEntityReference (systemUser.LogicalName, systemID);
Thanks in advance people.
*This post is locked for comments
Thanks , now i get it.
Hi Victor,
As mentioned above, you cannot create user for CRM online directly using CRM SDK. You need to create users in O365 and assign licence. Once the license is assign you user will be created in your CRM organization.
Alternatively, you could use the below code for existing users-
For currently logged in user use WhoAmIRequest message-
=========================
var userId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
var userRef = new EntityReference("systemuser", userId);
=========================
To retrieve a different user by username
=============================
QueryExpression userQuery = new QueryExpression
{
EntityName = "systemuser",
ColumnSet = new ColumnSet("systemuserid"),
Criteria = { Conditions = { new ConditionExpression("domainname", ConditionOperator.Equal, "username@testorg.onmicrosoft.com") } }
};
var userResults = service.RetrieveMultiple(userQuery);
var retrievedUser = userResults.Entities.FirstOrDefault();
var userRef = retrievedUser.ToEntityReference();
=============================
Once you get the user reference, you can then set the user referenc directly to your other code. E.g.
clinic.new_ClinicCoordinator = userRef ;
Hope this helps.
Hi,
Is this user already part of the Office 365 ?? because in case crm online users are synchronised from office 365 as soon as it is added and licence is configured. If not then you need to add this user to office 365 first, something like this: stackoverflow.com/.../create-user-accounts-in-office-365-using-c-net-code-dynamically
Or you can use the powershell like this: community.dynamics.com/.../104204
Hope it will help !!
Please look at my response below. Thanks
var clinic = new new_clinic(); clinic.new_name = "Medinet"; Account trust = new Account(); trust.Name = "bIRMINGHAM CITY hOSPITAL"; Guid trustID = (Guid)Service().Create(trust); CrmEntityReference clinic_trust = new CrmEntityReference(trust.LogicalName, trustID); clinic.new_Trust = clinic_trust; clinic.new_ClinicDate = DateTime.Now; new_hospital hospital = new new_hospital(); hospital.new_name = "Geek-guru"; Service().Create(hospital); Guid hospitalID = (Guid)hospital.Id; clinic.new_HostingHospital = new CrmEntityReference(hospital.LogicalName, hospitalID); clinic.new_ClinicDate = new DateTime(2018, 07, 28); new_serviceprovision ser_contract = new new_serviceprovision(); ser_contract.new_Trust = clinic_trust; ser_contract.new_Specialty = new OptionSetValue(100000018); Service().Create(ser_contract); Guid ser_con_id = (Guid)ser_contract.Id; clinic.new_ServiceContract = new CrmEntityReference(ser_contract.LogicalName, ser_con_id); // Account account = (Account)GetOrgService().Retrieve(trust.LogicalName, trustID, new Microsoft.Xrm.Sdk.Query.ColumnSet("name")); //retreive the default business unit needed to create the user QueryExpression businessUnitQuery = new QueryExpression { EntityName = BusinessUnit.EntityLogicalName, ColumnSet = new ColumnSet("businessunitid"), Criteria = { Conditions = { new ConditionExpression("parentbusinessunitid", ConditionOperator.Null) } } }; BusinessUnit businessUnit = Service().RetrieveMultiple(businessUnitQuery).Entities[0].ToEntity<BusinessUnit>(); //creating a user SystemUser systemUser = new SystemUser(); systemUser.DomainName = "" + "Chika"; systemUser.FirstName = "Olabajo"; systemUser.LastName = "Boss"; systemUser.InternalEMailAddress = "onyebuchi@gmail.com"; systemUser.BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, businessUnit.Id); Guid systemID = (Guid)Service().Create(systemUser); // systemUser = (SystemUser)Service().Retrieve(systemUser.LogicalName, systemID, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname")); //this field is a lookup field to User. Assigned to the user created above clinic.new_ClinicCoordinator = new CrmEntityReference(systemUser.LogicalName, systemID); clinic.new_BookedBy = new OptionSetValue(100000001); clinic.new_Type1 = new OptionSetValue(100000008); clinic.new_Type2 = new OptionSetValue(100000001); clinic.new_NumberofConsultants = 5; clinic.new_NumberofNursesSuppliedByTrust = 6; Service().Create(clinic); //getting the error here MessageBox.Show("Sucessfully added a clinic record");
The Above is all my code, at the final service(clinic), I am having the following error - System.ServiceModel.FaultException`1: 'new_serviceprovision With Id = 00000000-0000-0000-0000-000000000000 Does Not Exist'
Hi Mahender,
after adding this bit, i am having this error - System.ServiceModel.FaultException`1: 'new_serviceprovision With Id = 00000000-0000-0000-0000-000000000000 Does Not Exist'
//retreive the default business unit needed to create the user QueryExpression businessUnitQuery = new QueryExpression { EntityName = BusinessUnit.EntityLogicalName, ColumnSet = new ColumnSet("businessunitid"), Criteria = { Conditions = { new ConditionExpression("parentbusinessunitid", ConditionOperator.Null) } } }; BusinessUnit businessUnit = Service().RetrieveMultiple(businessUnitQuery).Entities[0].ToEntity<BusinessUnit>(); //creating a user SystemUser systemUser = new SystemUser(); systemUser.DomainName = "" + "VIC"; systemUser.FirstName = "Juwon Olabajo"; systemUser.LastName = "*******"; systemUser.InternalEMailAddress = "onyebuchiboss@gmail.com"; systemUser.BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, businessUnit.Id); Guid systemID = (Guid)Service().Create(systemUser);
Hi,
To create a user you also need to provide it's BU, please refer a sample here: msdn.microsoft.com/.../jj602984.aspx
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6