Checking if a solution exists and upload it to CRM using the SDK

Checking if a solution exists and upload it to CRM using the SDK

Many times we encounter an external application that requires a solution to be installed in your CRM environment, but it might not be there or we might require to upgrade an existing solution.

The first thing that we want to check is if the solution that we are looking for already exists in CRM. The following function checks for that exactly.

Many times we encounter an external application that requires a solution to be installed in your CRM environment, but it might not be there or we might require to upgrade an existing solution.

The first thing that we want to check is if the solution that we are looking for already exists in CRM. The following function checks for that exactly.

        public EntityCollection RetrieveSolutions(string solutionName)
        {
            QueryExpression query = new QueryExpression
            {
                EntityName = "solution",
                ColumnSet = new ColumnSet(true),
                Criteria =
                {
                    Conditions =
                    {
                        new ConditionExpression("uniquename", ConditionOperator.Equal, solutionName)
                    },
                }
            };

            RetrieveMultipleRequest request = new RetrieveMultipleRequest();
            request.Query = query;

            try
            {
                RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);
                EntityCollection results = response.EntityCollection;
                return results;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

Just pass the name of the solution to the function, and it will return all solutions that match that solution name. Should be either 0 or 1 values returned. I can use the following code to check for additional information about the solution (such as version number):

                if (results.Entities.Count > 0)
                {
                    string friendlyName = results.Entities[0].Attributes["friendlyname"].ToString();
                    string versionNumber = results.Entities[0].Attributes["version"].ToString();
                }

The next phase is to import the solution if none exists, or if you had a newer version that you would like to import, the same codeset will work. We can call the following function, and pass the full path of the solution file (zip file):

        
public static bool ImportSolution(string fileName)
        {
            byte[] fileBytes = File.ReadAllBytes(fileName);
            ImportSolutionRequest request = new ImportSolutionRequest()
            {
                CustomizationFile = fileBytes
            };

            try
            {
                ImportSolutionResponse response = (ImportSolutionResponse)service.Execute(request);
                return true;
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw ex;
            }
        }

That is basically the entire process. If the import fails, you will receive a Fault Exception and can check the data returned in the catch block.