Friday, October 19, 2012

Microsoft CRM 2011 Solution backups with help of Plugins.

     This small plugin can save a lot of time and put things under control. Each time somebody press on Publish All button, it creates a backup of all user solutions (system solution is ignored) and creates a task in the system with attached backups. So you can easily find a right task with right backups of solutions.


using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using System.Linq;
using Microsoft.Crm.Sdk.Messages;
namespace VolovikFramework.Plugins
{
   
    /// <summary>
    /// PreSolutionBackup Plugin.
    /// </summary>   
    public class PreSolutionBackup : PluginToContext
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PostSolution"/> class.
        /// </summary>
        public PreSolutionBackup(): base(typeof(PreSolutionBackup))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "PublishAll", "", new Action<LocalPluginContext>(ExecutePreSolutionBackup)));
        }
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        protected void ExecutePreSolutionBackup(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }
            this.MainExecute(localContext);
        }
        protected override void InternalExecute()
        {
            var solutions = from solutionsSet in this.OrgContext.SolutionSet select solutionsSet;
            Task task = new Task {Subject = string.Format("BackUp of solutions - {0}.", DateTime.Now)};
            var taskId = Guid.Empty;
            foreach (Solution solution in solutions)
            {
                //We need only user solution and unmanaged type
                if (solution.IsVisible == true && solution.IsManaged == false)
                {
                    var exportXml = ExportSolution(solution.UniqueName);
                    if (exportXml != null)
                    {
                        //Create a Task
                        if (taskId == Guid.Empty)
                            taskId = this.OrganizationService.Create(task);
                        //Create Annotation and attach it to Task
                        Annotation ann = new Annotation();
                        ann.Subject = String.Format("BackUp of solution {0}.",solution.UniqueName);
                        ann.NoteText = String.Format("BackUp created: {0}." + DateTime.Now);
                        ann.ObjectId = new EntityReference(Task.EntityLogicalName, taskId);
                        ann.IsDocument = false;
                        ann.FileName = String.Format("{0}_{1}.zip", solutionName, DateTime.Now);
                        ann.MimeType = "application/zip";
                        ann.DocumentBody = Convert.ToBase64String(exportXml);
                        this.OrganizationService.Create(ann);
                    }
                }
            }
        }
        /// <summary>
        /// Methods that send ExportSolution request
        /// </summary>
        /// <param name="solutionName">string name of Target solution</param>
        /// <returns>A byte array of zip file</returns>
        private byte[] ExportSolution(string solutionName)
        {
            try
            {
                ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest();
                //Export solution as managed? - No..
                exportSolutionRequest.Managed = false;
                //Specify the unique name of your solution
                exportSolutionRequest.SolutionName = solutionName;
                ExportSolutionResponse exportSolutionResponse =
                    (ExportSolutionResponse)this.OrganizationService.Execute(exportSolutionRequest);
                byte[] exportXml = exportSolutionResponse.ExportSolutionFile;
                return exportXml;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}

1 comment: