Retrieve Resources from Resource Group and Service entity programmatically

By | April 6, 2016

Introduction:

In order to deliver a service, resources which represent tools, rooms, people or pieces of equipment are used. These resources are collected into resource groups which have similar attributes. In MS Dynamics CRM, the resources are categorized as Facilities / Equipment or Users.

Recently, we had to retrieve Resources from the Resource Group and Service Entity. Initially, we thought that there was a direct relationship between Resources and Resource Group / Service entity. But we could not find the relationship. So we had to do the task through a program.

Let’s take a look how the resource is retrieved from the resource group.

In MS Dynamics CRM, the resources of Resource Group are stored in the ‘constraints’ field in the form of XML format in Resource Group entity. So, with the help of the code as below we can get the resources from the ‘constraints’ field.

            //retrieve the resource group
            Entity resourceGroup = service.Retrieve("constraintbasedgroup", new Guid("26017ff5-8bf1-e511-80e3-5065f38b81c1"), new ColumnSet("constraints"));

            //read the constraints field which consist guids of resources.
            if (resourceGroup != null && resourceGroup.Attributes.Contains("constraints") && resourceGroup.Attributes["constraints"] != null)
            {

                string constraints = (string)resourceGroup.Attributes["constraints"];
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(constraints);

                string resourcesData = xmlDoc.SelectSingleNode(".//Body").InnerText;

                //check resources are available or not the Resource group. 
                //false means there are no resources in the Resources group
                if (resourcesData.ToString().ToLower().Trim() != "false")
                {
                    List<Guid> resources = new List<Guid>();

                    string[] splitEachResources = resourcesData.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);

                    if (splitEachResources.Count() > 0)
                    {
                        //loop through all existing resources
                        foreach (var resource in splitEachResources)
                        {
                            string[] splitData = resource.Split(new string[] { "==" }, StringSplitOptions.RemoveEmptyEntries);

                            if (splitData.Count() > 0)
                            {
                                //add resources in the list
                                resources.Add(new Guid(splitData[1]));
                            }
                        }//end loop
                    }//check number of resources
                    else
                    {
                        //only one resource is available
                        string[] splitData = resourcesData.Split(new string[] { "==" }, StringSplitOptions.RemoveEmptyEntries);

                        if (splitData.Count() > 0)
                        {
                            //add resources in the list
                            resources.Add(new Guid(splitData[1]));
                        }
                    }
                }//end if resourcesData

            }//end if contstrains

Now, let’s take a look how to retrieve the resource from the service entity.

When we create a service record in the CRM, the MSCRM automatically creates ‘resourcespec’ entity record which has 1:N relationship between the ‘ResourceSpec’ and Service entity. The Dynamics CRM also creates Resource Group entity record which has 1:N relationship between Resource Group and ResourceSpec entity.

So, using the Resource Group entity record we can get the Resources of the particular service.

The code as below is written for retrieving the Resource Group. Next, follow the code as mentioned above to retrieve the resource from the resource group.

C#:

            //retrieve the Service 
            Entity serviceEntity = service.Retrieve("service", new Guid("EBE19C74-8DF1-E511-80E3-5065F38B81C1"), new ColumnSet("resourcespecid"));

            if (serviceEntity != null && serviceEntity.Contains("resourcespecid") && serviceEntity.Attributes["resourcespecid"] != null)
            {
                //now using the resourcespecid we need to retrieve the 
                //groupobjectid from the resourcespec table.

                Guid resourcespecid = ((EntityReference)serviceEntity.Attributes["resourcespecid"]).Id;

                //retrieve resourcespec table
                Entity resourcespec = service.Retrieve("resourcespec", resourcespecid, new ColumnSet("groupobjectid"));

                //checked groupobject id
                if (resourcespec != null && resourcespec.Contains("groupobjectid") && resourcespec.Attributes["groupobjectid"] != null)
                {
                    //important
                    //groupobjectid is nothing but the resourcegroup(entity) id
                    //now using resource group id we can retrieve the resources from the resource group as we have done in the above code.

                    Entity resourceGroupTest = service.Retrieve("constraintbasedgroup", new Guid(resourcespec.Attributes["groupobjectid"].ToString()), new ColumnSet("constraints"));

                }
            } //end main if

Conclusion:

Before we end this blog, we hope that the code snippets would help you retrieve the resources.

You may also like to see : Click2Clone – a productivity add-on for Microsoft Dynamics CRM.

Leave a Reply

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