Create a Business Unit Hierarchy

CRM 1.0

This sample code creates the following business unit hierarchy:

Under the root business, Adventure Works, you create two child business units: Alpine Ski House and Blue Yonder Airlines. Under Blue Yonder Airlines you also create a child business: Markie’s Travel. For each business you create two users.

The following diagram illustrates the organization hierarchy.

Business Unit Hierarchy

Class Reference

Schema Reference

  • businessunit.xsd
  • organization.xsd
  • systemuser.xsd

Example

Note   The following example contains sample data for the administrator logon name and password. This is for illustration purposes only. User credentials should never be hard coded. For more information, see Building Secure ASP.NET Applications, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetch12.asp.

[C#]

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

   // 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("http://", 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");

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

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

   String strErrorMsg;

   // See Note above regarding the use of hard-coded passwords
   String strDomainAdminName = "Administrator";
   String strDomainAdminPass = "Admin";
   String strDomainName = "MyDomain";

   try
   {
      Microsoft.Crm.Platform.Proxy.CUserAuth userAuth 
               = new Microsoft.Crm.Platform.Proxy.CUserAuth();
      // License does a check for the Domain Admin,
      // but we must provide a valid format GUID for the caller
      // In this case, the caller can have empty GUIDs
      Microsoft.Crm.Platform.Proxy.CUserAuth strDomainAdminCaller 
               = new Microsoft.Crm.Platform.Proxy.CUserAuth();
      strDomainAdminCaller.MerchantId = System.Guid.Empty.ToString("b");
      strDomainAdminCaller.UserId = System.Guid.Empty.ToString("b");

      String strOrgName = "Contoso, Ltd";
      String strOrgID = String.Empty;

      // First create the organization OU in Active Directory, and then
      // get the object GUID as ID for the Microsoft CRM organization

      // CreateOrgOU is a DUMMY API
      // You need to write a method to create the OU in Active Directory
      strOrgID = CreateOrgOU(strOrgName);

      // The XML string for the organization
      String strOrgXml = String.Concat("<organization>", 
                 "<name>", strOrgName, "</name>",
                 "</organization>");

      // Set the object credentials to a user with domain administrator rights
      organization.Credentials 
               = new System.Net.NetworkCredential(strDomainAdminName, 
                                      strDomainAdminPass, strDomainName);
      license.Credentials 
               = new System.Net.NetworkCredential(strDomainAdminName,
                                      strDomainAdminPass, strDomainName);

      // Create the Microsoft CRM organization object
      organization.Create(strOrgXml, strOrgID);

      String strLicenseKey = "{QFL6W8JZ-TU8U-0NU6-1JKH-807F897C5027}";
      // Microsoft CRM Suite Professional
      String strLicenseType = "{59F6D48E-F7E8-46F3-806C-3CA992D21915}";

      // Grant the Professional Suite license to deployment
      license.AddDeploymentLicense(strDomainAdminCaller, strOrgID,
                                     strLicenseType, strLicenseKey);

      // Set up the XML string for the root business
      // The name must be unique
      String strRootBizXML = String.Concat("<businessunit>",
         "<name>Adventure Works</name>", 
         "</businessunit>");
   
      // See Note above regarding the use of hard-coded passwords
      String strFirstUserName = "Keith_Harris";
      String strFirstUserPass = "Keith_Harris";
      
      String strUserXML = String.Concat("<systemuser>",
         "<firstname>Keith</firstname>",
         "<lastname>Harris</lastname>",
         "<domainname>", strFirstUserName, "</domainname>",
         "</systemuser>");

      // Create a root business
      Microsoft.Crm.Platform.Proxy.CBusinessResult BizResult 
               = new Microsoft.Crm.Platform.Proxy.CBusinessResult ();
      BizResult = organization.CreateRootBusiness(strOrgID,
                                          strRootBizXML, strUserXML);

      // Grant the Professional Suite license to the first user
      license.GrantUserLicense(strDomainAdminCaller, userAuth.UserId,
                                "{59f6d48e-f7e8-46f3-806c-3ca992d21915}");

      // Set the user authorization for the first user
      userAuth.UserId = BizResult.UserId;
      userAuth.MerchantId = BizResult.BusinessId;

      // Set credentials for these objects for first user
      merchant.Credentials 
               = new System.Net.NetworkCredential(strFirstUserName,
                                strFirstUserPass, strDomainName );
      bizUser.Credentials 
               = new System.Net.NetworkCredential(strFirstUserName,
                                strFirstUserPass, strDomainName );
      license.Credentials 
               = new System.Net.NetworkCredential(strFirstUserName,
                                strFirstUserPass, strDomainName );

      // Create a business unit
      String strSubBusiness1 = "<businessunit>";
      strSubBusiness1 += "<parentbusinessunitid>" + userAuth;
      strSubBusiness1 += "</parentbusinessunitid>";
      strSubBusiness1 += "<name>Alpine Ski House</name>";
      strSubBusiness1 += "</businessunit>";

      // When the last parameter is provided as empty, no user will 
      // be created while creating a business unit
      Microsoft.Crm.Platform.Proxy.CBusinessResult result
                = merchant.Create(userAuth, strSubBusiness1, "");
      String strBusiness1_Id = result.BusinessId;
      String strUserName = null;

      // Create a user
      strUserName = "Nicole_Caron";
      String strUserA_1 = "<systemuser><firstname>Nicole</firstname>";
      strUserA_1 += "<lastname>Caron</lastname>";
      strUserA_1 += "<domainname>" + strUserName + "</domainname>";
      strUserA_1 += "<businessunitid>" + strBusiness1_Id;
      strUserA_1 += "</businessunitid></systemuser>";
    
      // The BizUser.Create method will fail if the user has not 
      // already been added to Active Directory
      String strUserA_1Id = bizUser.Create(userAuth, strUserA_1);

      // Grant Professional Suite license to the user
      license.GrantUserLicense(userAuth, strUserA_1Id,
                                "{59f6d48e-f7e8-46f3-806c-3ca992d21915}");

      // Create another user
      strUserName = "Ajay_Solanki";
      String strUserA_2 = "<systemuser><firstname>Ajay</firstname>";
      strUserA_2 += "<lastname>Solanki</lastname>";
      strUserA_2 += "<domainname>" + strUserName + "</domainname>";
      strUserA_2 += "<businessunitid>" + strBusiness1_Id;  
      strUserA_2 += "</businessunitid></systemuser>";
    
      // The BizUser.Create method will fail if the user has not 
      // already been added to Active Directory
      String strUserA_2Id = bizUser.Create(userAuth, strUserA_2);

      // Grant Professional Suite license to the user
      license.GrantUserLicense(userAuth, strUserA_1Id,
                               "{59f6d48e-f7e8-46f3-806c-3ca992d21915}");

      // Create another business unit
      String strSubBusiness2 = "<businessunit>";
      strSubBusiness2 += "<parentbusinessunitid>";
      strSubBusiness2 += userAuth.MerchantId;
      strSubBusiness2 += "</parentbusinessunitid>";
      strSubBusiness2 += "<name>Blue Yonder Airlines</name>";
      strSubBusiness2 += "</businessunit>";

      // When the last parameter is provided as empty, no user will 
      // be created while creating a business unit
      result= merchant.Create(userAuth, strSubBusiness2, "");
      String strBusinessB_Id = result.BusinessId;

      // Create a user
      strUserName = "Debra_E_Keiser";
      String strUserB_1 = "<systemuser><firstname>Debra</firstname>";
      strUserB_1 += "<lastname>Keiser</lastname>";
      strUserB_1 += "<domainname>" + strUserName + "</domainname>";
      strUserB_1 += "<businessunitid>" + strBusinessB_Id;
      strUserB_1 += "</businessunitid></systemuser>";

      // The BizUser.Create method will fail if the user has not 
      // already been added to Active Directory
      String strUserB_1Id = bizUser.Create(userAuth, strUserB_1);

      // Grant Professional Suite license to the user
      license.GrantUserLicense(userAuth, strUserA_1Id,
                                "{59f6d48e-f7e8-46f3-806c-3ca992d21915}");

      // Create another user
      strUserName = "Imtiaz_Khan";
      String strUserB_2 = "<systemuser><firstname>Imtiaz</firstname>";
      strUserB_2 += "<lastname>Khan</lastname>";
      strUserB_2 += "<domainname>" + strUserName + "</domainname>";
      strUserB_2 += "<businessunitid>" + strBusinessB_Id;
      strUserB_2 += "</businessunitid></systemuser>";
    
      // The BizUser.Create method will fail if the user has not 
      // already been added to Active Directory
      String strUserB_2Id = bizUser.Create(userAuth, strUserB_2);

      // Create a child business of business B
      String strSubBusinessB1 = "<businessunit><parentbusinessunitid>";
      strSubBusinessB1 += strBusinessB_Id;
      strSubBusinessB1 += "</parentbusinessunitid>";
      strSubBusinessB1 += "<name>Margie's Travel</name>";
      strSubBusinessB1 += "</businessunit>";

      // When the last parameter is provided as empty, no user will 
      // be created while creating a business unit
      result= merchant.Create(userAuth, strSubBusinessB1, "");
      String strBusinessB1_Id = result.BusinessId;

      // Create a user
      strUserName = "Min_Su";
      String strUserB1_1 = "<systemuser><firstname>Min</firstname>";
      strUserB1_1 += "<lastname>Su</lastname>";
      strUserB1_1 += "<domainname>" + strUserName + "</domainname>";
      strUserB1_1 += "<businessunitid>" + strBusinessB1_Id;
      strUserB1_1 += "</businessunitid></systemuser>";
    
      // The BizUser.Create method will fail if the user has not 
      // already been added to Active Directory
      String strUserB1_1Id = bizUser.Create(userAuth, strUserB1_1);

      // Grant Professional Suite license to the user
      license.GrantUserLicense(userAuth, strUserA_1Id,
                                "{59f6d48e-f7e8-46f3-806c-3ca992d21915}");

      // Create another user
      strUserName = "Yale_Li";
      String strUserB1_2 = "<systemuser><firstname>Yale</firstname>";
      strUserB1_2 += "<lastname>Li</lastname>";
      strUserB1_2 += "<domainname>" + strUserName + "</domainname>";
      strUserB1_2 += "<businessunitid>" + strBusinessB1_Id;
      strUserB1_2 += "</businessunitid></systemuser>";
    
      // The BizUser.Create method will fail if the user has not 
      // already been added to Active Directory
      String strUserB1_2Id = bizUser.Create(userAuth, strUserB1_2);

      // Grant Professional Suite license to the user
      license.GrantUserLicense(userAuth, strUserB1_2Id,
                                "{59f6d48e-f7e8-46f3-806c-3ca992d21915}");
   
   }
   catch (System.Web.Services.Protocols.SoapException err)
   {
      // Process the platform error here
      strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
                      err.Detail.OuterXml, " Source: ", err.Source);
   }
   finally
   {
      // Set back the credentials to default
      organization.Credentials = System.Net.CredentialCache.DefaultCredentials;
      merchant.Credentials = System.Net.CredentialCache.DefaultCredentials;
      bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
      license.Credentials = System.Net.CredentialCache.DefaultCredentials;
   }
}

© 2005 Microsoft Corporation. All rights reserved.


Show: