2012-03-14 94 views
1

嗨,大家好我想弄清楚如何通过WCF为我的CRM 2011项目执行附件。使用WCF的Dynamics CRM 2011附件

所以目前我有MVC的形式,允许用户上传PDF文件到我的服务器。现在我想有一个WCF服务,查看上传的文件并将它们附加到相关的实体/表单。

我能够通过引用CRM的WCF服务对实体执行基本的CRUD操作,但不确定将文件附加到该实体的方法。有人能指点我正确的方向吗?

回答

3

您可以使用类似于下面的代码来读取适当的文件,对数据进行编码,然后创建一个附加到适当实体的新注释。如果您因任何原因使用了早期绑定,我在这里使用了后期绑定。

FileStream stream = File.OpenRead("pathToFile"); 
byte[] byteData = new byte[stream.Length]; 
stream.Read(byteData, 0, byteData.Length); 
stream.Close();  

string encodedData = System.Convert.ToBase64String(byteData); 

Entity annotation = new Entity("annotation"); 
annotation.Attributes["subject"] = "My subject"; 
annotation.Attributes["notetext"] = "My note text"; 

EntityReference noteRef = new EntityReference(); 
noteRef.LogicalName = "myEntity"; 
noteRef.Id = myEntity.Id; 
annotation.documentbody = encodedData; 
annotation.filename = "myFile.doc"; 
annotation.mimetype = @"application\ms-word"; 
annotation.Attributes.Add("objectid", noteRef); 
annotation.Attributes.Add("objecttypecode", "myEntity"); 

service.Create(annotation); 

让我知道你上车,

感谢。

+0

嗨我遇到错误访问注释类的documentbody,文件名和mimetype属性。 – skub 2012-03-19 06:06:52

+0

也许尝试通过Attributes集合访问它们,例如annotation.Attributes [ “documentbody”]。他们当然应该在那里。本文确认有关属性的设置:http://msdn.microsoft.com/en-us/library/gg328429.aspx。让我知道你是怎么办的。 – 2012-03-19 08:28:48