Skip to content

Export a solution via Javascript

Posted in Dynamics 365, Power Platform, and Revive

Last week I got an error during a solution export, but the error message wasn’t very helpfull. I then had the idea to export the solution via Javascript with the hope to get an more meanfull error.
Fortunately it was so and by the way I’ve learned how to export a solution via Javascript.

//Just to make the selection oh the target version easier
var targetVersions = {
    v_7_0 : "7.0.0.0",
    v_7_1 : "7.1.0.0",
    v_8_0 : "8.0.0.0",
    v_8_1 : "8.1.0.0",
    v_8_2 : "8.2.0.0",
};

function exportSolution(strSolutionName, asManaged, strTargetVersion, objParameter)
{
    var parameters = {
        SolutionName: strSolutionName,
        Managed: asManaged,
        TargetVersion: strTargetVersion,
        ExportAutoNumberingSettings: objParameter.ExportAutoNumberingSettings || false,
        ExportCalendarSettings: objParameter.ExportCalendarSettings || false,
        ExportCustomizationSettings: objParameter.ExportCustomizationSettings || false,
        ExportEmailTrackingSettings: objParameter.ExportEmailTrackingSettings || false,
        ExportGeneralSettings: objParameter.ExportGeneralSettings || false,
        ExportMarketingSettings: objParameter.ExportMarketingSettings || false,
        ExportOutlookSynchronizationSettings: objParameter.ExportOutlookSynchronizationSettings || false,
        ExportRelationshipRoles: objParameter.ExportRelationshipRoles || false,
        ExportIsvConfig: objParameter.ExportIsvConfig || false,
        ExportSales: objParameter.ExportSales || false,
        ExportExternalApplications: objParameter.ExportExternalApplications || false,
    };

	var req = new XMLHttpRequest();
	req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/ExportSolution", true);
	req.setRequestHeader("OData-Version", "4.0");
	req.setRequestHeader("OData-MaxVersion", "4.0");
	req.setRequestHeader("Accept", "application/json");
	req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
	req.onreadystatechange = function ()
	{
		if (this.readyState === 4)
		{
			req.onreadystatechange = null;
			if (this.status === 200)
			{
                //you will get a download notification
			}
			else
			{
				Xrm.Utility.alertDialog(this.statusText);
			}
		}
	};
	req.send(JSON.stringify(parameters));
}

One Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.