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($"{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插件

 

 实体修改图标最终效果图

 更换站点地图底色后

 移动端:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的有限差分法实验报告用MATLAB中的有限差分法计算槽内电位;对比解析法和数值法的异同点;选取一点,绘制收敛曲线;总的三维电位图+使用说明文档 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值