【发布时间】:2014-05-06 14:37:46
【问题描述】:
我正在实现一个插件(POST Quote Create、Synchronous、Sandbox),以便在修改报价时将注释复制到新记录中。
我的插件归结为这个(sn-p):
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var Service = serviceFactory.CreateOrganizationService(context.UserId);
var notesQuery = new QueryExpression("annotation");
notesQuery.ColumnSet = new ColumnSet(true);
notesQuery.Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression("objecttypecode", ConditionOperator.Equal, "quote"),
new ConditionExpression("objectid", ConditionOperator.Equal, revisedQuoteId)
}
};
var notes = Service.RetrieveMultiple(notesQuery).Entities;
foreach (var n in notes)
{
var newNote = new Entity("annotation");
newNote.Attributes.Add("ownerid", n.GetAttributeValue<EntityReference>("ownerid"));
newNote.Attributes.Add("objectid", new EntityReference("quote", sourceEntity.Id));
newNote.Attributes.Add("objecttypecode", "quote");
newNote.Attributes.Add("subject", n.GetAttributeValue<string>("subject"));
newNote.Attributes.Add("notetext", n.GetAttributeValue<string>("notetext"));
newNote.Attributes.Add("isdocument", n.GetAttributeValue<bool>("isdocument"));
if (n.GetAttributeValue<bool>("isdocument"))
{
newNote.Attributes.Add("filesize", n.GetAttributeValue<int>("filesize"));
newNote.Attributes.Add("documentbody", n.GetAttributeValue<string>("documentbody"));
newNote.Attributes.Add("filename", n.GetAttributeValue<string>("filename"));
newNote.Attributes.Add("mimetype", n.GetAttributeValue<string>("mimetype"));
}
Service.Create(newNote);
}
基本上,我会复制所有内容,包括最终的附件。一切似乎都很好,新版本正确地显示了字段、详细记录和注释……除了注释的附件之外的所有内容。
如果我有一个笔记,附上test.txt,内容是:
测试附件
OrganizationData 服务内容如下:
<d:FileName>test.txt</d:FileName>
<d:FileSize m:type="Edm.Int32">39</d:FileSize>
<d:DocumentBody>H4sIAAAMaVMA/wtJLS5RSCwpSUzOyE3NK+HlAgCLmj1zEQAAAA==</d:DocumentBody>
它的“克隆”具有正确的主题和文本,并且还显示了一个test.txt 附加的内容是什么
‹ iS ÿI-.QH,)ILÎÈMÍ+áå ‹š=s
mimetype 和(在检查odata时,我注意到filesizefilesize实际上不正确!)似乎是正确的(又名:相同作为我试图复制的原始注释),但 OData 似乎确认了某些东西已经关闭(它不同!):
<d:FileName>test.txt</d:FileName>
<d:FileSize m:type="Edm.Int32">60</d:FileSize
<d:DocumentBody>H4sIAED6aVMA/5Pv5mBg4MkMZvjP7amrF+iho+npc+6E71nth0+ZGLpn2RYLMjAwAABXqCwTJQAAAA==</d:DocumentBody>
test.txt 文件是从命令提示符创建的(COPY CON test.txt,键入,CTRL+Z)。
我尝试更改文件,并通过 PDFCreator 创建了一个test.pdf:AcroRead 反过来抱怨说文档已损坏(因此问题似乎与 mimetype 无关)。
我还尝试通过早期绑定(通过 CRMSVCUTIL 生成的类)重新实现相同的代码,但它会产生完全相同的结果(垃圾而不是附件内容)。
我尝试像这样手工制作documentbody:
// "VGVzdCBhdHRhY2htZW50" is Base64 for "Test attachment"
newNote.Attributes.Add("documentbody", "VGVzdCBhdHRhY2htZW50");
而且创建的文件是正确的。
我不知道发生了什么:据我所知,documentbody 应该是一个 Base64 编码的字符串(同样,据我所知)复制时应该没有任何不同.我错过了什么?
作为参考,CRM 已更新为 UR13,但我在 UR16 环境中重新制作了它。
【问题讨论】:
-
你检查过
n.GetAttributeValue<string>("documentbody")的值,并尝试解码检查内容吗? -
我在问题中整合了我以前的 cmets。最新添加:早期绑定的代码做同样的事情会产生同样的结果
标签: c# plugins encoding dynamics-crm-2011