Share via


Create a Child Business Unit

This sample creates a child business unit with one new user.

Class Reference

Schema Reference

  • businessunit.xsd
  • 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 CreateSubBusiness()
{
   // 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");

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

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

      // Microsoft CRM business hierarchy directly maps to the Organizational
      // hierarchy in Active Directory. One constraint is that no two
      // child business units under the same location can have the same name

      // For this sample code to succeed, there should not be a
      // child business units named SubBusiness2 already under the same place
      StringBuilder subBusiness = new StringBuilder("<businessunit>");
      subBusiness.Append("<parentbusinessunitid>");
      subBusiness.Append(userAuth.MerchantId.ToString());
      subBusiness.Append("</parentbusinessunitid>");
      subBusiness.Append("<name>SubBusiness2</name>");
      subBusiness.Append("</businessunit>");

      String strUserName = "Frank_Lee";

      // 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
      String strUserXml = String.Concat("<systemuser><domainname>" ,
                              strUserName, "</domainname></systemuser>");

      Microsoft.Crm.Platform.Proxy.CBusinessResult result 
               = merchant.Create(userAuth, subBusiness.ToString(),
                                 strUserXml);
      String strSubBusinessId = result.BusinessId;
      String strUserId = result.UserId;

   }
   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.