?
原創地址:http://www.cnblogs.com/jfzhu/archive/2013/02/15/2913077.html
轉載請注明出處
?
我在以前的文章中講過如何用JScript讀取web resource資源,我在本文中將要講解如何在C#中獲取web resource資源。
?
有時候可能有這樣的需求,你需要在一個插件中讀取某個xml web resource的內容,并將該xml文件作為附件創建一封E-mail。或者該xml文檔是插件的一個配置文件。這時,你就需要在C#中獲取web resource資源了。CRM中web resource不過是一個特殊的entity,在數據庫中你也可以看到web resource table。web resource的內容(content)以Base64編碼保存在數據庫中(參見Base 64 Encoding 編碼)。你只需要知道web resource的name,然后就可以用RetrieveMultiple方法獲取該web resource。下面的代碼演示了,如何獲取一個名為aw_testxml.xml的web resource,并將其內容作為附件發送給一封E-mail。
// Create an e-mail message. // Create the 'From:' activity party for the email ActivityParty fromParty = new ActivityParty { PartyId = new EntityReference(SystemUser.EntityLogicalName, new Guid("F6F5BB29-D519-E211-B109-B499BAFDBEDA")) };// Create the 'To:' activity party for the email ActivityParty toParty = new ActivityParty { PartyId = new EntityReference(SystemUser.EntityLogicalName, new Guid("F6F5BB29-D519-E211-B109-B499BAFDBEDA")) };Email email = new Email { To = new ActivityParty[] { toParty }, From = new ActivityParty[] { fromParty }, Subject = "SDK Sample e-mail", Description = "SDK Sample for SendEmail Message.", DirectionCode = true }; Guid _emailId = service.Create(email);QueryExpression mySavedQuery = new QueryExpression { ColumnSet = new ColumnSet(true), EntityName = WebResource.EntityLogicalName, Criteria = new FilterExpression() { Conditions = { new ConditionExpression { AttributeName = "name", Operator = ConditionOperator.Equal, Values = {"aw_testxml.xml"} } } } };EntityCollection ec = service.RetrieveMultiple(mySavedQuery); if (ec != null && ec.Entities != null && ec.Entities.Count > 0) { WebResource webresource = ec.Entities[0].ToEntity<WebResource>(); ActivityMimeAttachment _sampleAttachment = new ActivityMimeAttachment { ObjectId = new EntityReference(Email.EntityLogicalName, _emailId), ObjectTypeCode = Email.EntityLogicalName, Subject = "Sample Attachment", Body = webresource.Content, FileName = "ExampleAttachment.xml" };service.Create(_sampleAttachment); }
?