Microsoft Dynamics CRM 批量上传web资源(非官方WebResourceUtility)并替换实体图标

背景:

去年以前可以按照目录WebResourceUtility批量上传web资源,昨天发现用不了了,拿到WebResourceUtility源码改了一下都不是很方便,感觉官方写的太冗余,太长了,跟我喜欢的简单粗暴思想不太符合,刚好无意阅览了一个上传资源的代码,干脆自己手写一个根据目录去上传web资源的工具。

工具:

LinqPad 5

Microsoft Dynamics SDK 9.0

XrmToolBox

老规矩先上效果图:

目录包含的文件
在这里插入图片描述

批量创建web资源后,发布

在这里插入图片描述

解决方案添加现有资源

在这里插入图片描述

代码

复制代码
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(KaTeX parse error: Expected 'EOF', got '}' at position 85: … entityId; 91 }̲ 92 93 //发布w…"\r\n{id.ToString().ToUpper()}\r\n");
104 }
105 XElement element = XElement.Parse("\r\n"+sb.ToString()+"\r\n");
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();
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插件

在这里插入图片描述

实体修改图标最终效果图

更换站点地图底色后

移动端:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值