Example #1
0
        protected override void ProcessRecord()
        {
            var assembly     = System.Reflection.Assembly.LoadFile(Path);
            var assemblyName = assembly.GetName().Name;
            var version      = assembly.GetName().Version.ToString();

            WriteObject($"Reading Assembly: {Path}");
            WriteObject($"Assembly Name: {assemblyName}");
            WriteObject($"Assembly Version: {version}");

            using (var context = new CIContext(OrganizationService))
            {
                var pluginAssembly = context.PluginAssemblySet.Single(x => x.Name == assemblyName);
                var content        = Convert.ToBase64String(File.ReadAllBytes(Path));
                if (pluginAssembly.Version != version ||
                    pluginAssembly.Content.GetHashCode() != content.GetHashCode())
                {
                    WriteObject($"Updating Plugin Assembly: {assemblyName}");
                    pluginAssembly.Content = content;
                    context.UpdateObject(pluginAssembly);
                    context.SaveChanges();
                }
                else
                {
                    WriteObject($"Assembly version and content were not changed. Skipping update.");
                }
            }
        }
Example #2
0
        protected override void ProcessRecord()
        {
            var missedWebResourcesCount = 0;
            var resourceFiles           = new HashSet <string>();
            var patterns = (string.IsNullOrWhiteSpace(SearchPattern)) ? new string[1] {
                "*"
            } :
            SearchPattern.Split(',');

            foreach (var pattern in patterns)
            {
                WriteVerbose($"Processing pattern {pattern}...");

                Directory.GetFiles(Path, pattern.Trim(), SearchOption.AllDirectories).ToList()
                .ForEach((item) => resourceFiles.Add(item));
            }

            if (resourceFiles.Count == 0)
            {
                WriteVerbose($"There are no files in folder '{Path}' matching patterns {SearchPattern}");
                return;
            }
            else
            {
                WriteVerbose($"Found {resourceFiles.Count} resource files.");
            }

            using (var context = new CIContext(OrganizationService))
            {
                WriteVerbose($"Retrieving web resources from CRM...");
                var allWebResources = context.WebResourceSet.ToList();
                WriteVerbose($"Found {allWebResources.Count}");

                var importexportxml = new StringBuilder();
                var webResource     = default(WebResource);
                foreach (var resourceFile in resourceFiles)
                {
                    WriteVerbose($"Processing file: {System.IO.Path.GetFileName(resourceFile)}");
                    try
                    {
                        webResource = allWebResources.Single(CompareCondition(resourceFile));
                        WriteVerbose($"Found web resource: {webResource.Name}");
                    }
                    catch (Exception ex)
                    {
                        missedWebResourcesCount++;
                        WriteWarning($"Cannot process {resourceFile}: {ex.Message}");
                        continue;
                    }

                    // update in context
                    var fileContent = Convert.ToBase64String(File.ReadAllBytes(resourceFile));
                    if (webResource.Content?.GetHashCode() != fileContent.GetHashCode())
                    {
                        webResource.Content = fileContent;
                        context.UpdateObject(webResource);
                        // add id to publish xml
                        if (Publish)
                        {
                            importexportxml.Append($"<webresource>{webResource.Id}</webresource>");
                        }
                    }
                }

                // Update
                WriteVerbose("Saving changes...");
                context.SaveChanges();

                // Publish
                if (Publish)
                {
                    WriteVerbose("Publishing web resources...");
                    PublishXmlRequest req = new PublishXmlRequest()
                    {
                        ParameterXml = $"<importexportxml><webresources>{importexportxml.ToString()}</webresources></importexportxml>"
                    };
                    OrganizationService.Execute(req);
                }

                WriteObject($"{resourceFiles.Count - missedWebResourcesCount} out of {resourceFiles.Count} web resources were processed");

                if (FailIfWebResourceNotFound && missedWebResourcesCount > 0)
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new RuntimeException($"{missedWebResourcesCount} web resources were not found"),
                                              "", ErrorCategory.ObjectNotFound, null));
                }
            }
        }