2012-03-14 99 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