Customizations Utility

The Microsoft Dynamics CRM customizations file can encapsulate the application’s configuration and schema, workflow rules, and security roles. As mentioned in Chapter 2, during the development process you should plan to have regular software builds as well as copies of the customizations file available for rollback purposes. This utility will automatically export all of the customizations for an organization and save the information to a file. You can then save this file in your source control system for backups, restores, and auditing. By setting up this utility to run automatically, you can create scheduled backups of the organization’s customizations in case you ever need to go back and retrieve an older version. Technically, you could also back up the _mscrm database to provide a complete snapshot of the customizations, but we think you’ll find it more useful to have backups of the full customizations XML available.

The code in Example 15-4 provides a simple application to extract the entire customizations file from a system and store it on the file system.

More Information

More Information

See Chapter 5, for an example of how to use a similar technique with a plug-in to save the customizations each time someone publishes changes.

Example 15-4. The customizations utility

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;

namespace ExportCustomizations
{
  class Program
  {
   static void Main(string[] args)
   {
     if (args.Length != 2)
     {
       Console.Error.WriteLine(
         "usage: ExportCustomizationsXml <crmServerUrl> <organizationName>");
       Environment.Exit(1);
     }

     try
     {
       string crmServerUrl = args[0];
       string orgName = args[1];
       string filename = string.Format("customizations_{0}_{1}.zip",
           orgName, DateTime.Today.ToString("yyyyMMddhhmmss"));

       CrmService service = GetCrmService(crmServerUrl, orgName);

       ExportCompressedAllXmlRequest request = new ExportCompressedAllXmlRequest();
       request.EmbeddedFileName = "customizations.xml";
       ExportCompressedAllXmlResponse response =
         (ExportCompressedAllXmlResponse)service.Execute(request);

       System.IO.File.WriteAllBytes(filename, response.ExportCompressedXml);
     }
     catch (System.Web.Services.Protocols.SoapException ex)
     {
       Console.Error.WriteLine(ex.Detail.InnerText);
       Environment.Exit(-1);
     }
     catch (Exception e)
     {
       Console.Error.WriteLine(e.Message);
       Environment.Exit(-1);
     }
   }

   /// <summary>
   /// Creates an instance of a CrmService.
   /// </summary>
   /// <param name="crmServerUrl">The URL of the CRM server.
   /// Path does not need to be included.</param>
   /// <param name="organizationName">The name of the CRM organization.</param>
   /// <returns>A new CrmService instance.</returns>
   public static CrmService GetCrmService(string crmServerUrl,
     string organizationName)
   {
     CrmAuthenticationToken token = new CrmAuthenticationToken();
     token.OrganizationName = organizationName;
   
     CrmService service = new CrmService();
   
     if (!String.IsNullOrEmpty(crmServerUrl))
     {
       UriBuilder builder = new UriBuilder(crmServerUrl);
        builder.Path = "/MSCRMServices/2007/CrmService.asmx";
       service.Url = builder.Uri.ToString();
     }
   
     service.UseDefaultCredentials = true;
     service.CrmAuthenticationTokenValue = token;
 
     return service;
    }
  }
}

The following example demonstrates how to create a simple utility application to export customizations from a Microsoft Dynamics CRM system.

Creating a new console project

  1. Open Visual Studio 2008.

  2. On the File menu, click New, and then click Project.

  3. Under Project Types, select Visual C# Projects, and then click Console Application under Templates.

  4. In the Name box, type ExportCustomizations and click OK.

  5. Using the techniques discussed in Chapter 3, add references to the Microsoft.Crm.Sdk and Microsoft.Crm.SdkTypeProxy assemblies.

  6. Add a reference to Microsoft.Web.Services.

  7. Replace the contents of the Program.cs file with the code in Example 15-4.

  8. Save and then build the application.

  9. Open a command window and navigate to the proper output folder to find the application.

  10. Enter the following statement:

    exportcustomizations "http://<crmserver>" "<organizationname>"

    Important

    Important

    You must run the application as a valid Microsoft Dynamics CRM user who has the ability to export customizations and has permission to write to the file system.

  11. The following output zip file will then appear with an xml file with all of your customizations.

Important
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset