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

背景:

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

工具:

LinqPad 5

Microsoft Dynamics SDK 9.0

XrmToolBox

老规矩先上效果图:

目录包含的文件

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

批量创建web资源后,发布

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

解决方案添加现有资源

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

代码

 //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)并替换实体图标

实体修改图标最终效果图

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

更换站点地图底色后

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

移动端:

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

上一篇:linux find命令-print0和xargs中-0使用技巧


下一篇:视音频编解码学习工程:H.264分析器