Microsoft Dynamics CRM 批量上傳web資源(非官方WebResourceUtility)並替換實體圖標

背景:web

去年之前能夠按照目錄WebResourceUtility批量上傳web資源,昨天發現用不了了,拿到WebResourceUtility源碼改了一下都不是很方便,感受官方寫的太冗餘,太長了,跟我喜歡的簡單粗暴思想不太符合,恰好無心閱覽了一個上傳資源的代碼,乾脆本身手寫一個根據目錄去上傳web資源的工具。工具

工具:ui

LinqPad 5spa

Microsoft Dynamics SDK 9.0pwa

XrmToolBox插件

老規矩先上效果圖:code

目錄包含的文件component

 

批量建立web資源後,發佈server

 

 

解決方案添加現有資源xml

 

 

 代碼

  1 //Microsoft Dynamics CRM 批量上傳web資源(非官方WebResourceUtility)替換圖標
  2 //對應web資源在mscrm的文件類型
  3 enum FileTypes
  4 {
  5     HTML = 1,
  6     CSS = 2,
  7     JS = 3,
  8     XML = 4,
  9     PNG = 5,
 10     JPG = 6,
 11     GIF = 7,
 12     XAP = 8,
 13     XSL = 9,
 14     ICO = 10,
 15     SVG = 11,
 16     RESX = 12
 17 }
 18 //根據目錄獲取目錄下全部的文件
 19 Dictionary<string, int> GetFilesWithDir(string localPath)
 20 {
 21     Dictionary<string, int> dict = new Dictionary<string, int>();
 22     var typelist = Enum.GetNames(typeof(FileTypes));
 23     var dirs = Directory.GetDirectories(localPath);
 24     //dirs.Dump();
 25     foreach (var dir in dirs)
 26     {
 27         var files = Directory.GetFiles(dir);
 28         //files.Dump();
 29         foreach (var file in files)
 30         {
 31             var index = file.LastIndexOf(".");//.Dump();
 32             if (index == -1) continue;
 33             var filetype = file.Substring(index + 1).ToUpper();
 34             if (typelist.Contains(filetype))
 35             {
 36                 dict.Add(file,
 37                 Enum.Parse(typeof(FileTypes), filetype).GetHashCode()
 38                 );
 39             }
 40 
 41         }
 42     }
 43     return dict;
 44 }
 45 
 46 //建立或更新web資源
 47 Guid CreateOrUpateFile2WebResoulse(IOrganizationService service, string filePath, FileTypes type, string rootPath, string serverPath = "new_/icons/")
 48 {
 49     Stopwatch sw = new Stopwatch();
 50     sw.Start();
 51 
 52     string fileName = filePath.Replace(rootPath, serverPath).Replace("\\", "/");
 53 
 54     var fileContent = File.ReadAllText(filePath);
 55 
 56     fileName = Regex.Replace(fileName, @"[\u4e00-\u9fa5]", "").Replace("//", "/");
 57 
 58     //常規文本文件
 59     var customTypes = new int[] { 1, 2, 3, 4, 11, 12 };
 60 
 61     QueryExpression query = new QueryExpression("webresource")
 62     {
 63         ColumnSet = new ColumnSet(new string[] { "webresourceid" }),
 64         Criteria = new FilterExpression(LogicalOperator.And)
 65     };
 66     query.Criteria.AddCondition("name", ConditionOperator.Equal, new object[] { fileName });
 67     EntityCollection entitys = service.RetrieveMultiple(query);
 68 
 69     Guid entityId;
 70 
 71     Entity entity = new Entity("webresource");
 72     entity["content"] = customTypes.Contains(type.GetHashCode()) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContent.ToString())) : ImgToBase64String(filePath);
 73 
 74     if (entitys.Entities.Count == 0)
 75     {
 76         entity["webresourcetype"] = new OptionSetValue(type.GetHashCode());
 77         entity["displayname"] = fileName;
 78         entity["name"] = fileName;
 79         entity["componentstate"] = new OptionSetValue(0);
 80         entityId = service.Create(entity);
 81     }
 82     else
 83     {
 84         entity = entitys.Entities[0];
 85         service.Update(entity);
 86         entityId = entity.Id;
 87     }
 88     sw.Stop();
 89     Console.WriteLine($"{fileName} 建立/更新成功!耗時:{sw.ElapsedMilliseconds} 毫秒。");
 90     return entityId;
 91 }
 92 
 93 //發佈web資源
 94 void publishWebResources(List<Guid> ids,IOrganizationService service)
 95 {
 96     Stopwatch sw=new Stopwatch();
 97     sw.Start();
 98     
 99     var sb=new StringBuilder();
100     
101     foreach (var id in ids)
102     {
103         sb.AppendLine($"\r\n<webresource>{id.ToString().ToUpper()}</webresource>\r\n");
104     }
105     XElement element = XElement.Parse("<importexportxml>\r\n<webresources>"+sb.ToString()+"</webresources>\r\n</importexportxml>");
106     PublishXmlRequest request = new PublishXmlRequest();
107     request.ParameterXml = element.ToString();
108     service.Execute(request);
109     sw.Stop();
110     Console.WriteLine($"批量發佈!耗時:{sw.ElapsedMilliseconds} 毫秒。");
111 
112 }
113 void Main()
114 {    
115     var service = Dynamic365.GetService(Envs.dev);
116     
117     var rootPath = @"D:\Desktop\圖標20191123\圖標20191123\";
118     var targetPath = @"new_/dyicon/";
119     var dict=GetFilesWithDir(rootPath).Dump("目錄包含的文件");
120     
121     var ids=new List<Guid>();
122     
123     foreach (var kv in dict)
124     {
125         Guid id;
126         try
127         {
128             id=CreateOrUpateFile2WebResoulse(service, kv.Key, (FileTypes)kv.Value, rootPath, targetPath);
129 
130         }
131         catch(Exception ex)
132         {
133             ex.Dump();
134             
135             //報錯從新執行一次
136             id=CreateOrUpateFile2WebResoulse(service, kv.Key, (FileTypes)kv.Value, rootPath, targetPath);
137         }
138         ids.Add(id);
139     }
140     
141     publishWebResources(ids,service);
142 }

問題延伸:

web資源批量上傳後,可是仍是須要手動選擇web資源替換實體圖標,這裏在xrmtoolbox的插件市場找到iconator插件

 

 實體修改圖標最終效果圖

相關文章
相關標籤/搜索