private static void UploadDirectory(IOrganizationService service, string path, string prefix, string extension, Deploy.Merge merge, string subpath, string solution) { Console.WriteLine("Upload " + path); foreach (var file in Directory.GetFiles(path)) { var filename = Path.GetFileName(file); if (file.ToUpper().EndsWith(".MAP") || file.ToUpper().EndsWith(".TS")) { continue; } if (subpath == null) { filename = prefix + "_" + filename; } else { filename = prefix + "_" + "/" + subpath + "/" + filename; } filename = filename.Replace(@"\", "/"); filename = merge.DeployName(file, filename); var webResource = findWebresource(service, filename, extension); var type = filename.ToResourceType(); if (webResource != null) { var lastChange = ((DateTime)webResource["modifiedon"]).ToUniversalTime(); if (merge.IsChanged(file, lastChange)) { Console.WriteLine("Updating " + filename); webResource["content"] = Convert.ToBase64String(merge.GetMergedFileContent(file, type)); service.Update(webResource); var publishRequest = new PublishXmlRequest { ParameterXml = string.Format("<importexportxml><webresources><webresource>{0}</webresource></webresources></importexportxml>", webResource.Id) }; var res = service.Execute(publishRequest) as PublishXmlResponse; if (res != null) { foreach (var re in res.Results) { Console.WriteLine(re.Key + " " + re.Value); } } } } else { if (merge.MergeOnly(file)) { continue; } webResource = new Entity { Id = Guid.NewGuid(), LogicalName = "webresource" }; webResource["name"] = filename; webResource["content"] = Convert.ToBase64String(merge.GetMergedFileContent(file, type)); webResource["displayname"] = filename; webResource["description"] = "Auto Imported as part"; if (type == ResourceTypeEnum.Unknown) { Console.WriteLine("Warning : unable to map file to Dynamics 365 web resource type " + filename + ". The file was ignored"); continue; } webResource["webresourcetype"] = new Microsoft.Xrm.Sdk.OptionSetValue((int)type); service.Create(webResource); var publishRequest = new PublishXmlRequest { ParameterXml = string.Format("<importexportxml><webresources><webresource>{0}</webresource></webresources></importexportxml>", webResource.Id) }; service.Execute(publishRequest); if (!string.IsNullOrEmpty(solution)) { // attach new webResource to solution var request = new Microsoft.Crm.Sdk.Messages.AddSolutionComponentRequest { ComponentType = 61, // Web Resource, ComponentId = webResource.Id, SolutionUniqueName = solution }; service.Execute(request); } Console.WriteLine("Created " + filename); } } foreach (var dir in Directory.GetDirectories(path)) { var d = dir.Replace(path, ""); if (!string.IsNullOrEmpty(subpath)) { d = subpath + @"\" + d; } UploadDirectory(service, dir, prefix, extension, merge, d, solution); } }
public void Import(string dist, string name, string subPath, string solution, string prefix = null) { var path = dist; if (!string.IsNullOrEmpty(subPath)) { path += @"\" + subPath; } if (prefix == null) { prefix = findCustomizationPrefix(solution); } #region upload files foreach (var file in Directory.GetFiles(path)) { var filename = Path.GetFileName(file); var resourceName = prefix + "_/" + name + (!string.IsNullOrEmpty(subPath) ? "/" + subPath.Replace("\\", "/") : "") + "/" + filename; var webResource = findWebresource(resourceName); var data = Convert.ToBase64String(File.ReadAllBytes(file)); if (data.Length == 0) { data = Convert.ToBase64String(filename.DefaultContentForEmplyFile()); } if (webResource != null) { if (new FileInfo(file).LastWriteTimeUtc > ((DateTime)webResource["modifiedon"]).ToUniversalTime()) { Console.WriteLine("Updating " + resourceName); webResource["content"] = data; orgService.Update(webResource); var publishRequest = new PublishXmlRequest { ParameterXml = string.Format("<importexportxml><webresources><webresource>{0}</webresource></webresources></importexportxml>", webResource.Id) }; orgService.Execute(publishRequest); } } else { webResource = new Entity { Id = Guid.NewGuid(), LogicalName = "webresource" }; webResource["name"] = resourceName; webResource["content"] = data; webResource["displayname"] = name + ": " + resourceName; webResource["description"] = "Imported as part of the " + name + " application"; webResource["webresourcetype"] = new Microsoft.Xrm.Sdk.OptionSetValue((int)filename.ToResourceType()); orgService.Create(webResource); var publishRequest = new PublishXmlRequest { ParameterXml = string.Format("<importexportxml><webresources><webresource>{0}</webresource></webresources></importexportxml>", webResource.Id) }; orgService.Execute(publishRequest); // attach new webResource to solution var request = new Microsoft.Crm.Sdk.Messages.AddSolutionComponentRequest { ComponentType = 61, // Web Resource, ComponentId = webResource.Id, SolutionUniqueName = solution }; orgService.Execute(request); Console.WriteLine("Created " + resourceName); } } #endregion #region upload subs var sub = path; if (!string.IsNullOrEmpty(subPath)) { sub = path + @"\" + subPath; } foreach (var dir in Directory.GetDirectories(path)) { var dirname = Path.GetFileName(dir); if (dirname == "out-tsc") { continue; } if (!string.IsNullOrEmpty(subPath)) { dirname = subPath + @"\" + dirname; } this.Import(dist, name, dirname, solution, prefix); } #endregion }
private void UploadContent(string name, string solution, string resourceName, string file, string data, bool modifycheck) { var hasData = data != null; var filename = Path.GetFileName(file); var webResource = findWebresource(resourceName); if (data == null) { data = Convert.ToBase64String(File.ReadAllBytes(file)); } if (data.Length == 0) { data = Convert.ToBase64String(filename.DefaultContentForEmplyFile()); } if (webResource != null) { if (!modifycheck || new FileInfo(file).LastWriteTimeUtc > ((DateTime)webResource["modifiedon"]).ToUniversalTime()) { Console.WriteLine("Updating " + resourceName); webResource["content"] = data; orgService.Update(webResource); var publishRequest = new PublishXmlRequest { ParameterXml = string.Format("<importexportxml><webresources><webresource>{0}</webresource></webresources></importexportxml>", webResource.Id) }; orgService.Execute(publishRequest); } } else { webResource = new Entity { Id = Guid.NewGuid(), LogicalName = "webresource" }; webResource["name"] = resourceName; webResource["content"] = data; webResource["displayname"] = name + ": " + resourceName; webResource["description"] = "Imported as part of the " + name + " application"; var type = hasData ? ResourceTypeEnum.Html : filename.ToResourceType(); if (type == ResourceTypeEnum.Unknown) { Console.WriteLine("Warning : unable to map file to Dynamics 365 web resource type " + filename + ". The file was ignored"); return; } webResource["webresourcetype"] = new Microsoft.Xrm.Sdk.OptionSetValue((int)type); orgService.Create(webResource); var publishRequest = new PublishXmlRequest { ParameterXml = string.Format("<importexportxml><webresources><webresource>{0}</webresource></webresources></importexportxml>", webResource.Id) }; orgService.Execute(publishRequest); // attach new webResource to solution var request = new Microsoft.Crm.Sdk.Messages.AddSolutionComponentRequest { ComponentType = 61, // Web Resource, ComponentId = webResource.Id, SolutionUniqueName = solution }; orgService.Execute(request); Console.WriteLine("Created " + resourceName); } }