背景:

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

工具:

LinqPad 5

Microsoft Dynamics SDK 9.0

XrmToolBox

老规矩先上效果图:

目录包含的文件

批量创建web资源后,发布

解决方案添加现有资源

代码

 //Microsoft Dynamics CRM 批量上传web资源(非官方WebResourceUtility)替换图标
//对应web资源在mscrm的文件类型
enum FileTypes
{
HTML = ,
CSS = ,
JS = ,
XML = ,
PNG = ,
JPG = ,
GIF = ,
XAP = ,
XSL = ,
ICO = ,
SVG = ,
RESX =
}
//根据目录获取目录下所有的文件
Dictionary<string, int> GetFilesWithDir(string localPath)
{
Dictionary<string, int> dict = new Dictionary<string, int>();
var typelist = Enum.GetNames(typeof(FileTypes));
var dirs = Directory.GetDirectories(localPath);
//dirs.Dump();
foreach (var dir in dirs)
{
var files = Directory.GetFiles(dir);
//files.Dump();
foreach (var file in files)
{
var index = file.LastIndexOf(".");//.Dump();
if (index == -) continue;
var filetype = file.Substring(index + ).ToUpper();
if (typelist.Contains(filetype))
{
dict.Add(file,
Enum.Parse(typeof(FileTypes), filetype).GetHashCode()
);
} }
}
return dict;
} //创建或更新web资源
Guid CreateOrUpateFile2WebResoulse(IOrganizationService service, string filePath, FileTypes type, string rootPath, string serverPath = "new_/icons/")
{
Stopwatch sw = new Stopwatch();
sw.Start(); string fileName = filePath.Replace(rootPath, serverPath).Replace("\\", "/"); var fileContent = File.ReadAllText(filePath); fileName = Regex.Replace(fileName, @"[\u4e00-\u9fa5]", "").Replace("//", "/"); //常规文本文件
var customTypes = new int[] { , , , , , }; QueryExpression query = new QueryExpression("webresource")
{
ColumnSet = new ColumnSet(new string[] { "webresourceid" }),
Criteria = new FilterExpression(LogicalOperator.And)
};
query.Criteria.AddCondition("name", ConditionOperator.Equal, new object[] { fileName });
EntityCollection entitys = service.RetrieveMultiple(query); Guid entityId; Entity entity = new Entity("webresource");
entity["content"] = customTypes.Contains(type.GetHashCode()) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContent.ToString())) : ImgToBase64String(filePath); if (entitys.Entities.Count == )
{
entity["webresourcetype"] = new OptionSetValue(type.GetHashCode());
entity["displayname"] = fileName;
entity["name"] = fileName;
entity["componentstate"] = new OptionSetValue();
entityId = service.Create(entity);
}
else
{
entity = entitys.Entities[];
service.Update(entity);
entityId = entity.Id;
}
sw.Stop();
Console.WriteLine($"{fileName} 创建/更新成功!耗时:{sw.ElapsedMilliseconds} 毫秒。");
return entityId;
} //发布web资源
void publishWebResources(List<Guid> ids,IOrganizationService service)
{
Stopwatch sw=new Stopwatch();
sw.Start(); var sb=new StringBuilder(); foreach (var id in ids)
{
sb.AppendLine($"\r\n<webresource>{id.ToString().ToUpper()}</webresource>\r\n");
}
XElement element = XElement.Parse("<importexportxml>\r\n<webresources>"+sb.ToString()+"</webresources>\r\n</importexportxml>");
PublishXmlRequest request = new PublishXmlRequest();
request.ParameterXml = element.ToString();
service.Execute(request);
sw.Stop();
Console.WriteLine($"批量发布!耗时:{sw.ElapsedMilliseconds} 毫秒。"); }
void Main()
{
var service = Dynamic365.GetService(Envs.dev); var rootPath = @"D:\Desktop\图标20191123\图标20191123\";
var targetPath = @"new_/dyicon/";
var dict=GetFilesWithDir(rootPath).Dump("目录包含的文件"); var ids=new List<Guid>(); foreach (var kv in dict)
{
Guid id;
try
{
id=CreateOrUpateFile2WebResoulse(service, kv.Key, (FileTypes)kv.Value, rootPath, targetPath); }
catch(Exception ex)
{
ex.Dump(); //报错重新执行一次
id=CreateOrUpateFile2WebResoulse(service, kv.Key, (FileTypes)kv.Value, rootPath, targetPath);
}
ids.Add(id);
} publishWebResources(ids,service);
}

问题延伸:

web资源批量上传后,但是还是需要手动选择web资源替换实体图标,这里在xrmtoolbox的插件市场找到iconator插件

实体修改图标最终效果图

更换站点地图底色后

移动端:

Microsoft Dynamics CRM 批量上传web资源(非官方WebResourceUtility)并替换实体图标的更多相关文章

  1. Dynamics CRM2016 升级老版本报“JavaScript Web 资源包含对 Microsoft Dynamics CRM 4.0 (2007) Web 服务终结点的引用”问题的解决办法

    今天在新的服务器上部署了CRM2016 on-premises,并将CRM2015的数据库拷贝过来准备附加后升级,但在升级过程中遇到了如下错误,向导检测到了我的JavaScript Web 资源中包含 ...

  2. Dynamics CRM2016 升级老版本号报“JavaScript Web 资源包括对 Microsoft Dynamics CRM 4.0 (2007) Web 服务终结点的引用”问题的解决的方法

    今天在新的server上部署了CRM2016 on-premises,并将CRM2015的数据库拷贝过来准备附加后升级,但在升级过程中遇到了例如以下错误.向导检測到了我的JavaScript Web ...

  3. Dynamics CRM 自定义上传附件的图片悬浮层显示

    CRM中的附件是以流的形式保存在了数据库中,这样做的一个坏处是一旦系统运行时间久,附件上传的多了势必会导致数据库极速扩大,即影响系统的运行效率也对后期的迁移维护带来了不必要的麻烦.所以很多的客户都会要 ...

  4. 一、Microsoft Dynamics CRM 4.0 SDK概述

    Chapter 1. Microsoft Dynamics CRM 4.0 SDK Overview(SDK概述) You are probably reading this book because ...

  5. WEB版一次选择多个文件进行批量上传(WebUploader)的解决方案

    本人在2010年时使用swfupload为核心进行文件的批量上传的解决方案.见文章:WEB版一次选择多个文件进行批量上传(swfupload)的解决方案. 本人在2013年时使用plupload为核心 ...

  6. WEB版一次选择多个文件进行批量上传(Plupload)的解决方案

    WEB版一次选择多个文件进行批量上传(Plupload)的解决方案  转载自http://www.cnblogs.com/chillsrc/archive/2013/01/30/2883648.htm ...

  7. WEB版一次选择多个图片进行批量上传(WebUploader)的解决方案

    最近在学习百度的开源上传组件WebUploader,上一篇文章,学习了批量文件上传,今天学习一下批量图片上传,实际上与文件上传很类似,只是添加了图片显示功能,这个功能WebUploader组件中已经提 ...

  8. Microsoft Dynamics CRM 2011 当您在 大型数据集上执行 RetrieveMultiple 查询很慢的解决方法

    症状 当您在 Microsoft Dynamics CRM 2011 年大型数据集上执行 RetrieveMultiple 查询时,您会比较慢. 原因 发生此问题是因为大型数据集缓存 Retrieve ...

  9. WEB版一次选择多个文件进行批量上传(swfupload)的解决方案

    说明:功能完全支持ie和firefox浏览器! 一般的WEB方式文件上传只能使用FileUpload控件进行一个文件一个文件的进行上传,就算是批量上传,也要把文件一个一个的添加到页面,无法如windo ...

随机推荐

  1. Vulnhub靶场渗透练习(一) Breach1.0

    打开靶场 固定ip需要更改虚拟机为仅主机模式 192.168.110.140 打开网页http://192.168.110.140/index.html 查看源代码发现可以加密字符串 猜测base64 ...

  2. RIDE的Edit界面

    有四种类型的Edit界面(注:测试套件主要是存放测试案例,资源文件主要是存放用户关键字) 1.测试套件(file类型)的Edit界面 首先展开Setting: 对右侧红框按钮简单说明: Library ...

  3. springboot 打jar包时分离配置文件

    修改pom.xml文件 <build> <resources> <resource> <directory>src/main/resources< ...

  4. Centos7 安装需要的软件环境

    Mysql 安装 下载安装 下载并安装MySQL官方的 Yum Repository wget -i -c http://dev.mysql.com/get/mysql57-community-rel ...

  5. pycharm的使用(day03复习整理)

    pycharm的使用 file --> settings --> editor -->general --> change font size .... file --> ...

  6. Spring MVC(2)Spring MVC 组件开发

    一.控制器接收各类请求参数 代码测试环境: 接收各类参数的控制器--ParamsController package com.ssm.chapter15.controller; @Controller ...

  7. Unity常用协程功能封装

    # 1.前言unity开发过程中,经常用到一些特定的协程功能,比如延时触发.等待触发.重复操作等.unity自带Invoke以及InvokeRepeating方法,但这些方法均采用反射机制,性能消耗, ...

  8. SpringBoot2.1.9+dubbo2.7.3+Nacos1.1.4构建你的微服务体系

    简单几步使用最新版本的DUBBO构建你的微服务体系 NACOS注册中心 从github下载最新版本的nacos 上传至服务器并解压 单机启动sh startup.sh -m standalone na ...

  9. 初始bat命令

    任务:manven打包的jar包以及相关的bat文件,要将其设置为开机自启动. bat :批处理文件,通过简单的命令行方式对windows进行操作. 简单的bat命令: 1echo 显示命令,@带着个 ...

  10. Ambari 集群时间同步配置教程

    本文原始地址:https://sitoi.cn/posts/27560.html 步骤 在时间同步主节点创建 ntp.conf 文件 在时间同步从节点上创建 ntp.conf 文件 修改所有节点时区 ...