Create a User

In order to create a user, the user name must have already been added to Windows Active Directory. This can either be done by hand or programmatically.

For more information about Active Directory, see the documentation on MSDN (www.microsoft.com/windows2000/technologies/directory/ad/default.asp).

For more information about the Active Directory SDK, see the documentation on MSDN (msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/ad/active_directory.asp).

Class Reference

Schema Reference

  • systemuser.xsd

Example

[C#]

// The precondition for this code to work is that there needs to be
// an organization, a business unit within the organization, and a user
// within the business unit whose domainname corresponds to the 
// Active Directory name of the currently logged on user

public void CreateUser()
{
   // strServer should be set with the name of the platform Web server
   String strServer = "MyServerName";

   // strVirtualDirectory should be set with the name of the Microsoft CRM
   // virtual directory on the platform Web server
   String strVirtualDirectory = "mscrmservices";
   String strDir = String.Concat("https://", strServer, "/",
                                  strVirtualDirectory, "/");

   // BizUser proxy object
   Microsoft.Crm.Platform.Proxy.BizUser bizUser 
               = new Microsoft.Crm.Platform.Proxy.BizUser ();
   bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
   bizUser.Url = String.Concat(strDir, "BizUser.srf");

   String strErrorMsg;
   String strUserName;
   try
   {
      Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();

      // The 'domainname' field must map to an existing Active Directory
      // user's name. At the same time, if that name is already used to
      // create a user, it cannot be reused

      strUserName = "Reed_Koch";

      // For this sample code to succeed, 'Reed_Koch' must be a valid user
      // in the domain where the code is run, and none of the existing
      // Microsoft CRM users already map to it
      StringBuilder userXml 
           = new StringBuilder("<systemuser><firstname>Reed</firstname>");
      userXml.Append("<lastname>Koch</lastname>");
      userXml.AppendFormat("<domainname>{0}</domainname>", userName);
      userXml.Append("<businessunitid>");
      userXml.Append(userAuth.MerchantId.ToString());
      userXml.Append("</businessunitid></systemuser>");
      
      // Create the user
      String strUserId = bizUser.Create(userAuth, userXml.ToString());
   }
   catch (System.Web.Services.Protocols.SoapException err)
   {
      // Process the platform error here
      strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
                      err.Detail.OuterXml, " Source: ", err.Source);
   }
}

© 2005 Microsoft Corporation. All rights reserved.