locked
Programmatically create a user RRS feed

  • Question

  • I'm new to CRM, and have a book on developing for it, but have yet to find any examples there or online for programmatically creating a user in 4.0.    Does anyone have a code sample for this, or can you even point me toward the proper sdk class and/or methods?

    thanks,
    -stacy
    Wednesday, August 19, 2009 6:41 PM

Answers

  • systemuser newUser = new systemuser();

    newUser.domainname = "domainname\ali";
    newUser.firstname="Ali";
    newUser.lastname="Khan";
    newUser.internalemailaddress=test@hotmail.com;
    newUser.businessunitid = new Lookup();
    newUser.businessunitid.type = "businessunit";
    newUser.businessunitid.value = new Guid("BussinessUnitID");



    Guid id = crmService.Create(newUser);
    Wednesday, August 19, 2009 7:03 PM

All replies

  • systemuser newUser = new systemuser();

    newUser.domainname = "domainname\ali";
    newUser.firstname="Ali";
    newUser.lastname="Khan";
    newUser.internalemailaddress=test@hotmail.com;
    newUser.businessunitid = new Lookup();
    newUser.businessunitid.type = "businessunit";
    newUser.businessunitid.value = new Guid("BussinessUnitID");



    Guid id = crmService.Create(newUser);
    Wednesday, August 19, 2009 7:03 PM
  • After creating the user you must assign it some role. you can use the following code for this purpose

    Copy Code
    [C#]
    // Set up the CRM Service.
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0;
    token.OrganizationName = "AdventureWorksCycle";
     
    CrmService service = new CrmService();
    service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Create the request object.
    AssignUserRolesRoleRequest assign = new AssignUserRolesRoleRequest();

    // Set the properties of the request object.
    assign.UserId = user.UserId;
    // Set the ID of a role that is in the same business unit as the user.
    assign.RoleIds = new Guid[] {"B050F053-6968-DC11-BB3A-0003FFBAD37A"};

    // Execute the request.
    AssignUserRolesRoleResponse assigned = (AssignUserRolesRoleResponse) service.Execute(assign);

    The detailed code is here for assigning roles to a user.
    http://technet.microsoft.com/en-us/library/bb959373.aspx

    Wednesday, August 19, 2009 7:08 PM
  • Thanks so much!  I still had some trouble with the business unit id, so I gave it the executing user's business unit id in that org.  I was also told that I should try to give it all the fields that are required if you do it through forms, so I included some more email info, etc, in between the user instantiation and service call that you provided.

                    // Set business unit id
                    newUser.businessunitid = new Lookup();
                    systemuser me = new systemuser();  //Find my businessunitid and give it to this user.
                    WhoAmIRequest whoami = new WhoAmIRequest();
                    WhoAmIResponse resp = (WhoAmIResponse)service.Execute(whoami);
                    me = (systemuser)service.Retrieve("systemuser", resp.UserId, new AllColumns());
                    newUser.businessunitid = me.businessunitid;
                    newUser.businessunitid.type = CrmSdk.EntityName.businessunit.ToString();

                    newUser.setupuser = new CrmSdk.CrmBoolean();
                    newUser.setupuser.Value = false;

                    newUser.incomingemaildeliverymethod = new Picklist();
                    newUser.incomingemaildeliverymethod.Value = 2;

                    newUser.outgoingemaildeliverymethod = new Picklist();
                    newUser.outgoingemaildeliverymethod.Value = 2;

    But thanks also for the additional roles info. 

    Thanks again,
    -stacy
    Tuesday, September 15, 2009 4:25 PM
  • Here is the code snippet that worked for me in CRM 2011 to create and assign a Role to the User. Hope this helps.

     

    SystemUser newUser = new SystemUser();
                newUser.DomainName = "domainname\username";
                newUser.FirstName = "James";
                newUser.LastName = "Smith";
                newUser.EmployeeId = "12121212";
                newUser.JobTitle = "Title";
                newUser.NickName = "Jimmy";     

    // Business Unit is mandatory  or you will get an error saving the user      

                Guid bussUnitId = getBusinessUnitGuid(OrgService);// Get the guid for the business Unit for the BusinessUnit Entity
                newUser.BusinessUnitId = new EntityReference();
                newUser.BusinessUnitId.LogicalName = BusinessUnit.EntityLogicalName;
                newUser.BusinessUnitId.Id = bussUnitId;
                       
                try
                {
                    Guid userId = OrgService.Create(newUser);
                   


                    Guid secRoleId = getSecurityRoleGuid(OrgService); // Get Guid for security Role you want to assign from the ROLE Entity
                   
                    EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
                    relatedEntities.Add(new EntityReference(Role.EntityLogicalName, secRoleId));


                    // Create an object that defines the relationship between the SystemUser and Role.
                    Relationship relationship = new Relationship("systemuserroles_association");
                   
                    AssociateRequest req = new AssociateRequest();  // from the library "Microsoft.Xrm.Sdk.Messages";
                    req.Target = new EntityReference(SystemUser.EntityLogicalName, userId);
                    req.RelatedEntities = relatedEntities;
                    req.Relationship = relationship;


                    //Associate the SystenUser with the Security Role(s).
                    AssociateResponse response = (AssociateResponse)OrgService.Execute(req);

                    Console.WriteLine("The entities have been associated.");

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception creating user: " + ex.Message);
                    Console.WriteLine("StackTrace :" + ex.StackTrace);
                }

    Friday, July 15, 2011 1:56 PM