Dynamics 365-Plug In-邮件附件的增删改查
邮件附件的增删改查-转载自官方文档
演示如何使用以下方法创建、检索、更新和删除电子邮件附件:
-
IOrganizationService.Create
-
IOrganizationService.Retrieve
-
IOrganizationService.Update
-
IOrganizationService.Delete
1 using System; 2 using System.ServiceModel; 3 using System.ServiceModel.Description; 4 using System.Text; 5 6 // These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly 7 // found in the SDK\bin folder. 8 using Microsoft.Xrm.Sdk; 9 using Microsoft.Xrm.Sdk.Query; 10 using Microsoft.Xrm.Sdk.Discovery; 11 using Microsoft.Xrm.Sdk.Messages; 12 using Microsoft.Xrm.Sdk.Client; 13 14 15 // This namespace is found in Microsoft.Crm.Sdk.Proxy.dll assembly 16 // found in the SDK\bin folder. 17 using Microsoft.Crm.Sdk.Messages; 18 19 namespace Microsoft.Crm.Sdk.Samples 20 { 21 public class CRUDEmailAttachments 22 { 23 #region Class Level Members 24 25 26 /// <summary> 27 /// Stores the organization service proxy. 28 /// </summary> 29 private OrganizationServiceProxy _serviceProxy; 30 31 // Define the IDs needed for this sample. 32 private Guid _emailId; 33 private Guid[] _emailAttachmentId = new Guid[3]; 34 35 36 #endregion Class Level Members 37 38 #region How To Sample Code 39 /// <summary> 40 /// Create, Retrieve, Update and Delete an e-mail attachment. 41 /// <param name="serverConfig">Contains server connection information.</param> 42 /// <param name="promptforDelete">When True, the user will be prompted to delete all 43 /// created entities.</param> 44 /// </summary> 45 public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) 46 { 47 try 48 { 49 // Connect to the Organization service. 50 // The using statement assures that the service proxy will be properly disposed. 51 using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) 52 { 53 // This statement is required to enable early-bound type support. 54 _serviceProxy.EnableProxyTypes(); 55 56 CreateRequiredRecords(); 57 58 59 // Create three e-mail attachments 60 for (int i = 0; i < 3; i++) 61 { 62 ActivityMimeAttachment _sampleAttachment = new ActivityMimeAttachment 63 { 64 ObjectId = new EntityReference(Email.EntityLogicalName, _emailId), 65 ObjectTypeCode = Email.EntityLogicalName, 66 Subject = String.Format("Sample Attachment {0}", i), 67 Body = System.Convert.ToBase64String( 68 new ASCIIEncoding().GetBytes("Example Attachment")), 69 FileName = String.Format("ExampleAttachment{0}.txt", i) 70 }; 71 72 _emailAttachmentId[i] = _serviceProxy.Create(_sampleAttachment); 73 } 74 75 Console.WriteLine("Created three e-mail attachments for the e-mail activity."); 76 77 // Retrieve an attachment including its id, subject, filename and body. 78 ActivityMimeAttachment _singleAttachment = 79 (ActivityMimeAttachment)_serviceProxy.Retrieve( 80 ActivityMimeAttachment.EntityLogicalName, 81 _emailAttachmentId[0], 82 new ColumnSet("activitymimeattachmentid", 83 "subject", 84 "filename", 85 "body")); 86 87 Console.WriteLine("Retrieved an email attachment, {0}.", _singleAttachment.FileName); 88 89 // Update attachment 90 _singleAttachment.FileName = "ExampleAttachmentUpdated.txt"; 91 _serviceProxy.Update(_singleAttachment); 92 93 Console.WriteLine("Updated the retrieved e-mail attachment to {0}.", _singleAttachment.FileName); 94 95 // Retrieve all attachments associated with the email activity. 96 QueryExpression _attachmentQuery = new QueryExpression 97 { 98 EntityName = ActivityMimeAttachment.EntityLogicalName, 99 ColumnSet = new ColumnSet("activitymimeattachmentid"), 100 Criteria = new FilterExpression 101 { 102 Conditions = 103 { 104 new ConditionExpression 105 { 106 AttributeName = "objectid", 107 Operator = ConditionOperator.Equal, 108 Values = {_emailId} 109 }, 110 new ConditionExpression 111 { 112 AttributeName = "objecttypecode", 113 Operator = ConditionOperator.Equal, 114 Values = {Email.EntityLogicalName} 115 } 116 } 117 } 118 }; 119 120 EntityCollection results = _serviceProxy.RetrieveMultiple( 121 _attachmentQuery); 122 123 Console.WriteLine("Retrieved all the e-mail attachments."); 124 125 126 DeleteRequiredRecords(promptForDelete); 127 } 128 } 129 130 // Catch any service fault exceptions that Microsoft Dynamics CRM throws. 131 catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) 132 { 133 // You can handle an exception here or pass it back to the calling method. 134 throw; 135 } 136 } 137 138 /// <summary> 139 /// This method creates any entity records that this sample requires. 140 /// Creates the email activity. 141 /// </summary> 142 public void CreateRequiredRecords() 143 { 144 // Create Email Activity 145 Email email = new Email 146 { 147 Subject = "This is an example email", 148 ActivityId = Guid.NewGuid() 149 }; 150 151 _emailId = _serviceProxy.Create(email); 152 153 Console.WriteLine("An e-mail activity is created."); 154 } 155 156 157 /// <summary> 158 /// Deletes the custom entity record that was created for this sample. 159 /// <param name="prompt">Indicates whether to prompt the user 160 /// to delete the entity created in this sample.</param> 161 /// </summary> 162 public void DeleteRequiredRecords(bool prompt) 163 { 164 bool deleteRecords = true; 165 166 if (prompt) 167 { 168 Console.WriteLine("\nDo you want these entity records deleted? (y/n)"); 169 String answer = Console.ReadLine(); 170 171 deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); 172 } 173 174 if (deleteRecords) 175 { 176 for (int j = 0; j < 3; j++) 177 { 178 _serviceProxy.Delete(ActivityMimeAttachment.EntityLogicalName, _emailAttachmentId[j]); 179 } 180 _serviceProxy.Delete(Email.EntityLogicalName, _emailId); 181 Console.WriteLine("Entity records have been deleted."); 182 } 183 } 184 185 #endregion How To Sample Code 186 187 #region Main 188 /// <summary> 189 /// Main. Runs the sample and provides error output. 190 /// <param name="args">Array of arguments to Main method.</param> 191 /// </summary> 192 static public void Main(string[] args) 193 { 194 try 195 { 196 // Obtain the target organization's Web address and client logon 197 // credentials from the user. 198 ServerConnection serverConnect = new ServerConnection(); 199 ServerConnection.Configuration config = serverConnect.GetServerConfiguration(); 200 201 CRUDEmailAttachments app = new CRUDEmailAttachments(); 202 app.Run(config, true); 203 204 } 205 catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) 206 { 207 Console.WriteLine("The application terminated with an error."); 208 Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp); 209 Console.WriteLine("Code: {0}", ex.Detail.ErrorCode); 210 Console.WriteLine("Message: {0}", ex.Detail.Message); 211 Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText); 212 Console.WriteLine("Inner Fault: {0}", 213 null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"); 214 } 215 catch (System.TimeoutException ex) 216 { 217 Console.WriteLine("The application terminated with an error."); 218 Console.WriteLine("Message: {0}", ex.Message); 219 Console.WriteLine("Stack Trace: {0}", ex.StackTrace); 220 Console.WriteLine("Inner Fault: {0}", 221 null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message); 222 } 223 catch (System.Exception ex) 224 { 225 Console.WriteLine("The application terminated with an error."); 226 Console.WriteLine(ex.Message); 227 228 // Display the details of the inner exception. 229 if (ex.InnerException != null) 230 { 231 Console.WriteLine(ex.InnerException.Message); 232 233 FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = 234 ex.InnerException 235 as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>; 236 if (fe != null) 237 { 238 Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp); 239 Console.WriteLine("Code: {0}", fe.Detail.ErrorCode); 240 Console.WriteLine("Message: {0}", fe.Detail.Message); 241 Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText); 242 Console.WriteLine("Inner Fault: {0}", 243 null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"); 244 } 245 } 246 } 247 // Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException, 248 // SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException. 249 250 finally 251 { 252 Console.WriteLine("Press <Enter> to exit."); 253 Console.ReadLine(); 254 } 255 256 } 257 #endregion Main 258 259 } 260 }

浙公网安备 33010602011771号