Microsoft Dynamics CRM 批量上传web资源(非官方WebResourceUtility)并替换实体图标
程序员文章站
2024-03-27 22:01:58
背景: 去年以前可以按照目录WebResourceUtility批量上传web资源,昨天发现用不了了,拿到WebResourceUtility源码改了一下都不是很方便,感觉官方写的太冗余,太长了,跟我喜欢的简单粗暴思想不太符合,刚好无意阅览了一个上传资源的代码,干脆自己手写一个根据目录去上传web资 ......
背景:
去年以前可以按照目录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($"{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插件
实体修改图标最终效果图