Creating Business Units and Teams Programmatically

Assumed Background Information:

Introduction to Entities in Microsoft Dynamics CRM

http://msdn.microsoft.com/en-us/library/gg309396.aspx

You may find yourself working on a complex CRM implementation where you need to create a number of Business Units and Teams.

You may find it helpful to create a utility to do this activity on your behalf.

This following code reads the list of Business Units and Teams from an XML file and calls the methods below to do the heavy lifting.

Creating a Business Unit

/// <summary>
/// Creates the BusinessUnit with the given name
/// </summary>
/// <param name="strParentName">ParentBusiness Unit Name</param>
/// <param name="strName">Name of Business Unit needs to be created</param>
private void CreateBusinessUnit(string strParentName, string strName)
{
    try
    {
        Entity e = new Entity("businessunit");
        e.Attributes["name"] = strName;

        //Get the Parent BusinessUnit Guid from the Parent BusinessUnit Name
        string strParentGuid = strParentName;

        //Check for the valid Guid. If it is not a guid retrieve the guid based on the name of the business unit
        if (!IsGUID(strParentGuid))
        {
            strParentGuid = GetParentUnitId(strParentName);
        }


        //If Guid Exists then place it into the ParentBusinessUnitId lookup field of BusinessUnit entity
        if (strParentGuid.Length > 0)
        {
            EntityReference erParentBusinessUnit = new EntityReference("businessunit", new Guid(strParentGuid));
            e.Attributes["parentbusinessunitid"] = erParentBusinessUnit;
        }

        //Create the BusinessUnit
        _service.Create(e);
        Console.WriteLine("Created BusinessUnit : " + strName + " Successfully");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error While Creating a Business Unit :  " + strName + ". Error Message: " + ex.Message);
    }

}

 

 

Creating a Team

private void CreateTeam(string strBUName, string strTeamName)
{
    try
    {
        Entity e = new Entity("team");
        e.Attributes["name"] = strTeamName;

        //Get the Business Unit Name which the team belongs to
        string strBU = GetParentUnitId(strBUName);

        //If Guid Exists then place it into the BusinessUnit lookup field of Team entity
        if (strBU.Length > 0)
        {
            EntityReference erBusinessUnit = new EntityReference("businessunit", new Guid(strBU));
            e.Attributes["businessunitid"] = erBusinessUnit;
        }

        //Create the Team
        _service.Create(e);
        Console.WriteLine("Created Team : " + strTeamName + " Successfully under Business Unit: " + strBUName);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error While Creating a Team:  " + strTeamName + ". Error Message: " + ex.Message);
    }

}

 

Getting the ID of the Business Unit

/// <summary>
///  Gets the BusinessUnitID when the Businessunit name is passed
/// </summary>
/// <param name="BUName">BusinessUnit Name</param>
/// <returns>Business Unit Guid</returns>
private string GetParentUnitId(string BUName)
{
    try
    {
        //Checks whether the BUName is empty or null and then proceed into the query 
        if (BUName != null && BUName.Length > 0)
        {
            QueryByAttribute qba = new QueryByAttribute("businessunit");
            //Condition parameters is Businessunit Name
            qba.AddAttributeValue("name", BUName);
            //Values need to be retrieved is the Guid of Business Unit
            qba.ColumnSet = new ColumnSet("businessunitid", "createdon");


            DataCollection<Entity> dcBUDetails;

            //Retrieve the value based on the condition given
            dcBUDetails = _serviceProxy.RetrieveMultiple(qba).Entities;

            //Return the Guid of the BusinessUnit
            if (dcBUDetails.Count == 1)
            {
                if (dcBUDetails[0] != null)
                {
                    if (dcBUDetails[0].Id != null)
                    {
                        return dcBUDetails[0].Id.ToString();
                    }
                }
            }
            else
            {

                //Where there are more than one BusinessUnit with the same name take the latest created business unit to create the team or business  unit
                string id = string.Empty;
                DateTime dtMax = new DateTime();
                //Get the date of creation of the first BU
                DateTime.TryParse(dcBUDetails[0].Attributes["createdon"].ToString(), out dtMax);
                for (int i = 1; i < dcBUDetails.Count; i++)
                {
                    //Compare all the BU with the same name and get the latest Business unit created to create a team
                    DateTime dt;
                    DateTime.TryParse(dcBUDetails[i].Attributes["createdon"].ToString(), out dt);
                    if (dt > dtMax)
                    {
                        dtMax = dt;
                        id = dcBUDetails[i].Id.ToString();
                    }

                }
                return id;
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error While retrieving Guid of Business Unit " + ex.Message);
    }

    return "";

}
This entry was posted in Development and tagged . Bookmark the permalink.

One Response to Creating Business Units and Teams Programmatically

  1. Rizwan says:

    Can you please let me know what service reference you used? and is this code for CRM 2011?

Leave a Reply

Your email address will not be published. Required fields are marked *