Tag Cloud

CRM 2011 (161) CRM 4.0 (144) C# (116) JScript (109) Plugin (92) Registry (90) Techpedia (77) PyS60 (68) WScript (43) Plugin Message (31) Exploit (27) ShellCode (26) FAQ (22) JavaScript (21) Killer Codes (21) Hax (18) VB 6.0 (17) Commands (16) VBScript (16) Quotes (15) Turbo C++ (13) WMI (13) Security (11) 1337 (10) Tutorials (10) Asp.Net (9) Safe Boot (9) Python (8) Interview Questions (6) video (6) Ajax (5) VC++ (5) WebService (5) Workflow (5) Bat (4) Dorks (4) Sql Server (4) Aptitude (3) Picklist (3) Tweak (3) WCF (3) regex (3) Config (2) LINQ (2) PHP (2) Shell (2) Silverlight (2) TSql (2) flowchart (2) serialize (2) ASHX (1) CRM 4.0 Videos (1) Debug (1) FetchXml (1) GAC (1) General (1) Generics (1) HttpWebRequest (1) InputParameters (1) Lookup (1) Offline Plug-ins (1) OutputParameters (1) Plug-in Constructor (1) Protocol (1) RIA (1) Sharepoint (1) Walkthrough (1) Web.config (1) design patterns (1) generic (1) iframe (1) secure config (1) unsecure config (1) url (1)

Pages

Wednesday, May 01, 2013

Promote an E-mail Message to Microsoft Dynamics CRM

This sample code shows how to create an e-mail activity instance from the specified e-mail message.



   1:  //# Promote an E-mail Message to Microsoft Dynamics CRM

   2:  using System;

   3:  using CrmSdk;

   4:  using Microsoft.Crm.Sdk.Utility;

   5:   

   6:  namespace Microsoft.Crm.Sdk.HowTo.Email

   7:  {

   8:     public class DeliverPromoteEmail

   9:     {

  10:        public DeliverPromoteEmail()

  11:        {

  12:   

  13:        }

  14:   

  15:        public static bool Run(string crmServerUrl, string orgName)

  16:        {

  17:           bool success = false;

  18:   

  19:           try

  20:           {

  21:              // Set up the CRM Service.  

  22:              CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

  23:              service.PreAuthenticate = true;

  24:   

  25:              #region Setup Data Required for this Sample

  26:              

  27:              // Create a user to send an e-mail message to (To: field).

  28:              contact emailContact = new contact();

  29:              emailContact.firstname = "Adam";

  30:              emailContact.lastname = "Carter";

  31:              emailContact.emailaddress1 = "adam.carter@example.com";

  32:   

  33:              // Create the contact.

  34:              Guid emailContactId = service.Create(emailContact);

  35:   

  36:              // Get a system user to send the e-mail (From: field)

  37:              WhoAmIRequest systemUserRequest = new WhoAmIRequest();

  38:              WhoAmIResponse systemUserResponse = service.Execute(systemUserRequest) as WhoAmIResponse;

  39:              

  40:              ColumnSet selectClause = new ColumnSet();

  41:              selectClause.Attributes = new string[] { "internalemailaddress" };

  42:   

  43:              // Execute the request.

  44:              systemuser emailSender = service.Retrieve(EntityName.systemuser.ToString(), systemUserResponse.UserId, selectClause) as systemuser;

  45:   

  46:              #endregion

  47:   

  48:              // Create the request.

  49:              DeliverPromoteEmailRequest deliverEmailRequest = new DeliverPromoteEmailRequest();

  50:              

  51:              // Set all required fields

  52:              deliverEmailRequest.Subject = "SDK Sample Email";

  53:              deliverEmailRequest.To = emailContact.emailaddress1;

  54:              deliverEmailRequest.From = emailSender.internalemailaddress;

  55:              deliverEmailRequest.Bcc = String.Empty;

  56:              deliverEmailRequest.Cc = String.Empty;

  57:              deliverEmailRequest.Importance = "high";

  58:              deliverEmailRequest.Body = "This message will create an email activity.";

  59:              deliverEmailRequest.MessageId = Guid.NewGuid().ToString();

  60:              deliverEmailRequest.SubmittedBy = "";

  61:              deliverEmailRequest.ReceivedOn = new CrmDateTime();

  62:              deliverEmailRequest.ReceivedOn.Value = DateTime.Now.ToString();

  63:              

  64:              // We will not attach a file to the e-mail, but the Attachments property is required.

  65:              deliverEmailRequest.Attachments = new BusinessEntityCollection();

  66:              deliverEmailRequest.Attachments.EntityName = EntityName.activitymimeattachment.ToString();

  67:              deliverEmailRequest.Attachments.BusinessEntities = new activitymimeattachment[0];

  68:   

  69:              // Execute the request.

  70:              DeliverPromoteEmailResponse deliverEmailResponse = (DeliverPromoteEmailResponse)service.Execute(deliverEmailRequest);

  71:   

  72:              #region check success

  73:              

  74:              // Query for the delivered e-mail and verify the status code is "Sent".

  75:              ColumnSet deliveredMailColumns = new ColumnSet();

  76:              deliveredMailColumns.Attributes = new string[] { "statuscode" };

  77:   

  78:              email deliveredEmail = service.Retrieve(EntityName.email.ToString(), deliverEmailResponse.EmailId, deliveredMailColumns) as email;

  79:   

  80:              if (deliveredEmail.statuscode.Value == EmailStatus.Sent)

  81:              {

  82:                 success = true;

  83:              }

  84:   

  85:              #endregion

  86:   

  87:              #region Remove Data Required for this Sample

  88:   

  89:              // Delete the sent e-mail messages. 

  90:              service.Delete(EntityName.email.ToString(), deliveredEmail.activityid.Value);

  91:   

  92:              // Delete the contacts created for e-mail messages.

  93:              service.Delete(EntityName.contact.ToString(), emailContactId);

  94:   

  95:              #endregion

  96:           }

  97:           catch (System.Web.Services.Protocols.SoapException)

  98:           {

  99:              // Perform error handling here.

 100:              throw;

 101:           }

 102:           catch (Exception)

 103:           {

 104:              throw;

 105:           }

 106:           

 107:           return success;

 108:        }

 109:     }

 110:  }

No comments: