Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 13977

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

$
0
0
Body:

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

publicEntityCollection RetrieveSolutions(string solutionName)

{

QueryExpression query = newQueryExpression

{

EntityName = "solution",

ColumnSet = newColumnSet(true),

Criteria =

{

Conditions =

{

newConditionExpression("uniquename", ConditionOperator.Equal, solutionName)

},

}

};

RetrieveMultipleRequest request = newRetrieveMultipleRequest();

request.Query = query;

try

{

RetrieveMultipleResponse response = (RetrieveMultipleResponse)CRMHelper.xrm5.Execute(request);

EntityCollection results = response.EntityCollection;

return results;

}

catch (System.Exception ex)

{

returnnull;

}

}

 

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 (solutions.Entities.Count > 0)

{

string friendlyName = solutions.Entities[0].Attributes["friendlyname"].ToString();

string versionNumber = solutions.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):

publicstaticbool ImportSolution(string fileName)

{

byte[] fileBytes = File.ReadAllBytes(fileName);

ImportSolutionRequest request = newImportSolutionRequest()

{

CustomizationFile = fileBytes

};

try

{

ImportSolutionResponse response = (ImportSolutionResponse)context.Execute(request);

returntrue;

}

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.

Published: 8/20/2015 8:07 PM

Viewing all articles
Browse latest Browse all 13977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>