MS CRM 2011 C#中获取Web Resource
?
原創(chuàng)地址:http://www.cnblogs.com/jfzhu/archive/2013/02/15/2913077.html
轉(zhuǎn)載請(qǐng)注明出處
?
我在以前的文章中講過(guò)如何用JScript讀取web resource資源,我在本文中將要講解如何在C#中獲取web resource資源。
?
有時(shí)候可能有這樣的需求,你需要在一個(gè)插件中讀取某個(gè)xml web resource的內(nèi)容,并將該xml文件作為附件創(chuàng)建一封E-mail。或者該xml文檔是插件的一個(gè)配置文件。這時(shí),你就需要在C#中獲取web resource資源了。CRM中web resource不過(guò)是一個(gè)特殊的entity,在數(shù)據(jù)庫(kù)中你也可以看到web resource table。web resource的內(nèi)容(content)以Base64編碼保存在數(shù)據(jù)庫(kù)中(參見(jiàn)Base 64 Encoding 編碼)。你只需要知道web resource的name,然后就可以用RetrieveMultiple方法獲取該web resource。下面的代碼演示了,如何獲取一個(gè)名為aw_testxml.xml的web resource,并將其內(nèi)容作為附件發(fā)送給一封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); }?
總結(jié)
以上是生活随笔為你收集整理的MS CRM 2011 C#中获取Web Resource的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cocos creator 打包apk_
- 下一篇: 复习.net/c#时的小文章之万年草稿版