/// <summary>
 /// Method to Unregister an Assembly.
 /// </summary>
 /// <param name="xrmPluginAssembly">The Assembly to Unregister.</param>
 /// <returns>Result.</returns>
 internal bool UnregisterAssembly(string xrmServerDetails, XrmPluginAssembly xrmPluginAssembly, Collection <string> errors, SolutionComponentType solutionComponentType)
 {
     try
     {
         Result = RegistrationService.Unregister(xrmServerDetails, PluginAssembly.EntityLogicalName, xrmPluginAssembly.AssemblyId, errors, solutionComponentType);
         return(Result);
     }
     catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
     {
         throw;
     }
 }
 internal XrmPluginAssembly GetRegistrationAssembly(string xrmServerDetails, Registration registration)
 {
     try
     {
         XrmPluginAssembly xrmPluginAssembly = new XrmPluginAssembly();
         xrmPluginAssembly.Path          = registration.AssemblyPath;
         xrmPluginAssembly.IsolationMode = registration.IsolationMode;
         Assembly     assembly         = Assembly.LoadFrom(registration.AssemblyPath);
         AssemblyName assemblyName     = assembly.GetName();
         string       defaultGroupName = string.Format(CultureInfo.InvariantCulture, "{0} ({1})", assemblyName.Name, assemblyName.Version);
         ItemExists   assemblyExists   = GetRegistrationAssemblyId(xrmServerDetails, assemblyName);
         xrmPluginAssembly.Exists     = assemblyExists.Exists;
         xrmPluginAssembly.AssemblyId = assemblyExists.ItemId;
         xrmPluginAssembly.SourceType = registration.AssemblySourceType;
         FileInfo fileInfo = new FileInfo(registration.AssemblyPath);
         xrmPluginAssembly.ServerFileName = fileInfo.Name;
         if (xrmPluginAssembly.SourceType != XrmAssemblySourceType.Disk)
         {
             xrmPluginAssembly.ServerFileName = null;
         }
         string cultureLabel;
         if (assemblyName.CultureInfo.LCID == System.Globalization.CultureInfo.InvariantCulture.LCID)
         {
             cultureLabel = "neutral";
         }
         else
         {
             cultureLabel = assemblyName.CultureInfo.Name;
         }
         xrmPluginAssembly.Name    = assemblyName.Name;
         xrmPluginAssembly.Version = assemblyName.Version.ToString();
         xrmPluginAssembly.Culture = cultureLabel;
         byte[] tokenBytes = assemblyName.GetPublicKeyToken();
         if (null == tokenBytes || 0 == tokenBytes.Length)
         {
             xrmPluginAssembly.PublicKeyToken = null;
         }
         else
         {
             xrmPluginAssembly.PublicKeyToken = string.Join(string.Empty, tokenBytes.Select(b => b.ToString("X2", CultureInfo.InvariantCulture)));
         }
         xrmPluginAssembly = PluginHelper.GetRegistrationPlugins(xrmServerDetails, registration, xrmPluginAssembly, assembly, defaultGroupName);
         return(xrmPluginAssembly);
     }
     catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
     {
         throw;
     }
 }
 /// <summary>
 /// Method to Register an Assembly.
 /// </summary>
 /// <param name="xrmPluginAssembly">The Assembly to Register.</param>
 /// <returns>The Identifier of the newly Registered Assembly.</returns>
 internal Guid RegisterAssembly(string xrmServerDetails, XrmPluginAssembly xrmPluginAssembly)
 {
     try
     {
         using (xrmService = RegistrationService.GetService(xrmServerDetails))
         {
             PluginAssembly pluginAssembly = GetPluginAssembly(xrmPluginAssembly);
             return(xrmService.Create(pluginAssembly));
         }
     }
     catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
     {
         throw;
     }
 }
 /// <summary>
 /// Method to Update an Assembly Registration.
 /// </summary>
 /// <param name="xrmPluginAssembly">The Assembly to Update.</param>
 /// <returns>Result.</returns>
 internal bool UpdateAssembly(string xrmServerDetails, XrmPluginAssembly xrmPluginAssembly)
 {
     try
     {
         using (xrmService = RegistrationService.GetService(xrmServerDetails))
         {
             PluginAssembly pluginAssembly = GetPluginAssembly(xrmPluginAssembly);
             xrmService.Update(pluginAssembly);
         }
         Result = true;
         return(Result);
     }
     catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
     {
         throw;
     }
 }
        /// <summary>
        /// Method to retrieve the Assembly.
        /// </summary>
        /// <param name="xrmPluginAssembly">The Assembly to retrieve.</param>
        /// <returns>The retrieved Assembly.</returns>
        private PluginAssembly GetPluginAssembly(XrmPluginAssembly xrmPluginAssembly)
        {
            try
            {
                PluginAssembly = new PluginAssembly()
                {
                    PluginAssemblyId = xrmPluginAssembly.AssemblyId,
                    Name             = xrmPluginAssembly.Name,
                    SourceType       = new OptionSetValue()
                    {
                        Value = (int)xrmPluginAssembly.SourceType
                    },
                    IsolationMode = new OptionSetValue()
                    {
                        Value = (int)xrmPluginAssembly.IsolationMode
                    },
                    Culture        = xrmPluginAssembly.Culture,
                    PublicKeyToken = xrmPluginAssembly.PublicKeyToken,
                    Version        = xrmPluginAssembly.Version,
                    Description    = xrmPluginAssembly.Description
                };
                if (xrmPluginAssembly.SourceType == XrmAssemblySourceType.Database)
                {
                    PluginAssembly.Content = Convert.ToBase64String(File.ReadAllBytes(xrmPluginAssembly.Path));
                }
                switch (xrmPluginAssembly.SourceType)
                {
                case XrmAssemblySourceType.Disk:
                    PluginAssembly.Path = xrmPluginAssembly.ServerFileName;
                    break;

                case XrmAssemblySourceType.Database:
                    break;

                case XrmAssemblySourceType.GAC:
                    break;
                }
                ;
                return(PluginAssembly);
            }
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                throw;
            }
        }
Exemple #6
0
 /// <summary>
 /// Method to populate the Plugins according to the Registration Object.
 /// </summary>
 /// <param name="registration">The Registration Object indicating which Plugins to retrieve.</param>
 /// <param name="xrmPluginAssembly">The Assembly object to populate.</param>
 /// <param name="assembly">The Assembly from whence to extract Assembly Details.</param>
 /// <param name="defaultGroupName">The Default Group Name String Property.</param>
 internal XrmPluginAssembly GetRegistrationPlugins(string xrmServerDetails, Registration registration, XrmPluginAssembly xrmPluginAssembly, Assembly assembly, string defaultGroupName)
 {
     try
     {
         foreach (Type type in assembly.GetExportedTypes())
         {
             if (type.IsAbstract || !type.IsClass)
             {
                 continue;
             }
             foreach (Plugin plugin in registration.Plugins)
             {
                 if (type.FullName == plugin.PluginName)
                 {
                     XrmPluginType       xrmPluginType       = XrmPluginType.Plugin;
                     XrmPluginIsolatable xrmPluginIsolatable = XrmPluginIsolatable.Unknown;
                     string  workflowGroupName = defaultGroupName;
                     string  pluginName        = type.FullName;
                     Version sdkVersion        = GetSdkVersion(type, ref xrmPluginType, ref xrmPluginIsolatable, ref workflowGroupName, ref pluginName);
                     if (sdkVersion != null)
                     {
                         xrmPluginAssembly.SdkVersion = new Version(sdkVersion.Major, sdkVersion.Minor);
                         XrmPlugin xrmPlugin = new XrmPlugin();
                         xrmPlugin.TypeName   = type.FullName;
                         xrmPlugin.Name       = pluginName;
                         xrmPlugin.PluginType = xrmPluginType;
                         ItemExists pluginExists = GetRegistrationPluginId(pluginName, xrmServerDetails);
                         xrmPlugin.PluginId     = pluginExists.ItemId;
                         xrmPlugin.FriendlyName = pluginExists.ItemId.ToString();
                         xrmPlugin.Exists       = pluginExists.Exists;
                         xrmPlugin.AssemblyId   = xrmPluginAssembly.AssemblyId;
                         xrmPlugin.AssemblyName = xrmPluginAssembly.Name;
                         xrmPlugin.Isolatable   = xrmPluginIsolatable;
                         if (xrmPluginType == XrmPluginType.WorkflowActivity && !string.IsNullOrWhiteSpace(workflowGroupName))
                         {
                             xrmPlugin.WorkflowActivityGroupName = workflowGroupName;
                         }
                         xrmPlugin = StepHelper.GetPluginSteps(xrmServerDetails, plugin, xrmPlugin, pluginName, pluginExists);
                         xrmPluginAssembly.Plugins.Add(xrmPlugin);
                     }
                 }
             }
         }
         return(xrmPluginAssembly);
     }
     catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
     {
         throw;
     }
 }
Exemple #7
0
 /// <summary>
 /// Method to Unregister a Registration Collection.
 /// </summary>
 /// <param name="registrationCollection">The Registration Collection to Unregister.</param>
 /// <returns>Result.</returns>
 public bool Unregister(AssemblyRegistration registrationCollection)
 {
     try
     {
         this.errors = new Collection <string>();
         Result      = false;
         if (registrationCollection != null)
         {
             foreach (Registration registration in registrationCollection.Registrations)
             {
                 XrmPluginAssembly assembly = AssemblyHelper.GetRegistrationAssembly(registrationCollection.ConnectionString, registration);
                 if (assembly.Plugins.Count != 0)
                 {
                     foreach (XrmPlugin plugin in assembly.Plugins)
                     {
                         if (plugin.Steps.Count != 0)
                         {
                             foreach (XrmPluginStep step in plugin.Steps)
                             {
                                 if (step.Images.Count != 0)
                                 {
                                     foreach (XrmPluginImage image in step.Images)
                                     {
                                         Result = ImageHelper.UnregisterImage(registrationCollection.ConnectionString, image, errors, SolutionComponentType.SDKMessageProcessingStepImage);
                                         if (!Result)
                                         {
                                             throw new ArgumentException("Unregister image failed!");
                                         }
                                     }
                                 }
                                 Result = StepHelper.UnregisterStep(registrationCollection.ConnectionString, step, errors, SolutionComponentType.SDKMessageProcessingStep);
                                 if (!Result)
                                 {
                                     throw new ArgumentException("Unregister step failed!");
                                 }
                                 if (step.SecureConfiguration != null && step.SecureConfiguration.SecureConfigurationId.HasValue)
                                 {
                                     Result = StepHelper.UnregisterSecureConfiguration(registrationCollection.ConnectionString, step.SecureConfiguration, errors, SolutionComponentType.SDKMessageProcessingStepSecureConfig);
                                     if (!Result)
                                     {
                                         throw new ArgumentException("Unregister secure configuration failed!");
                                     }
                                 }
                             }
                         }
                         Result = PluginHelper.UnregisterPlugin(registrationCollection.ConnectionString, plugin, errors, SolutionComponentType.PluginType);
                         if (!Result)
                         {
                             throw new ArgumentException("Unregister plugin failed!");
                         }
                     }
                 }
                 Result = AssemblyHelper.UnregisterAssembly(registrationCollection.ConnectionString, assembly, errors, SolutionComponentType.PluginAssembly);
                 if (!Result)
                 {
                     throw new ArgumentException("Unregister assembly failed!");
                 }
             }
         }
         return(Result);
     }
     catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
     {
         throw;
     }
 }
Exemple #8
0
 /// <summary>
 /// Method to Register a Registration Collection.
 /// </summary>
 /// <param name="registrationCollection">The Registration Collection to Register.</param>
 /// <returns>Result.</returns>
 public bool Register(AssemblyRegistration registrationCollection)
 {
     try
     {
         this.errors = new Collection <string>();
         Result      = false;
         if (registrationCollection != null && registrationCollection.Registrations.Count != 0)
         {
             foreach (Registration registration in registrationCollection.Registrations)
             {
                 XrmPluginAssembly assembly = AssemblyHelper.GetRegistrationAssembly(registrationCollection.ConnectionString, registration);
                 if (assembly.Exists)
                 {
                     Result = AssemblyHelper.UpdateAssembly(registrationCollection.ConnectionString, assembly);
                     AssemblyHelper.AddAssemblyToSolution(registrationCollection, registration, assembly);
                 }
                 else
                 {
                     assembly.AssemblyId = AssemblyHelper.RegisterAssembly(registrationCollection.ConnectionString, assembly);
                     AssemblyHelper.AddAssemblyToSolution(registrationCollection, registration, assembly);
                     Result = true;
                 }
                 if (assembly.Plugins.Count != 0)
                 {
                     foreach (XrmPlugin plugin in assembly.Plugins)
                     {
                         if (plugin.Exists)
                         {
                             Result = PluginHelper.UpdatePlugin(registrationCollection.ConnectionString, plugin);
                         }
                         else
                         {
                             plugin.PluginId = PluginHelper.RegisterPlugin(registrationCollection.ConnectionString, plugin, errors);
                             Result          = true;
                         }
                         if (plugin.Steps.Count != 0)
                         {
                             foreach (XrmPluginStep step in plugin.Steps)
                             {
                                 if (step.Exists)
                                 {
                                     Result = StepHelper.UpdateStep(registrationCollection.ConnectionString, step);
                                     StepHelper.AddStepToSolution(registrationCollection, registration, step);
                                 }
                                 else
                                 {
                                     step.StepId = StepHelper.RegisterStep(registrationCollection.ConnectionString, step);
                                     StepHelper.AddStepToSolution(registrationCollection, registration, step);
                                     Result = true;
                                 }
                                 if (step.Images.Count != 0)
                                 {
                                     foreach (XrmPluginImage image in step.Images)
                                     {
                                         if (image.Exists)
                                         {
                                             Result = ImageHelper.UpdateImage(registrationCollection.ConnectionString, image);
                                         }
                                         else
                                         {
                                             image.ImageId = ImageHelper.RegisterImage(registrationCollection.ConnectionString, image);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         return(Result);
     }
     catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
     {
         throw;
     }
 }
 internal void AddAssemblyToSolution(AssemblyRegistration registrationCollection, Registration registration, XrmPluginAssembly assembly)
 {
     if (!string.IsNullOrEmpty(registration.SolutionUniqueName))
     {
         XrmSolution = new XrmSolution(registrationCollection.ConnectionString);
         XrmSolution.AddComponentToSolution(SolutionComponentType.PluginAssembly, assembly.AssemblyId, registration.SolutionUniqueName);
     }
 }