Monday, December 28, 2009

Send Email using crm sdk: how to pass activityparty[] array?

// Create an email message.
CrmSdk.email email = new email();



// Set email properties
string[] sOwnerGuids= OwnerGuid.Split(';');

int i = 0;

//If you more than 1 TO: Create an activity party array for the email
activityparty[] partyTo = new activityparty[sOwnerGuids.Length];
foreach (string sGuid in sOwnerGuids)
{
partyTo[i] = new activityparty();
partyTo[i].partyid= new Lookup();
partyTo[i].partyid.type = EntityName.systemuser.ToString();
partyTo[i].partyid.Value = new Guid(sGuid); //new Guid(OwnerGuid);

i = i + 1;
}

//If you have single TO : Create an activity party for the email
//activityparty partyTo = new activityparty();
//partyTo.partyid = new Lookup();
//partyTo.partyid.type = EntityName.systemuser.ToString();
//partyTo.partyid.Value = new Guid[] { OwnerGuid }; //new Guid(OwnerGuid);

email.to = partyTo; //new activityparty[] { partyTo };

activityparty partyFrom = new activityparty();
partyFrom.partyid = new Lookup();
partyFrom.partyid.type = EntityName.systemuser.ToString();
partyFrom.partyid.Value = new Guid(System.Configuration.ConfigurationSettings.AppSettings["CrmAdminGuid"].ToString());
email.from = new activityparty[] { partyFrom };

email.subject = emailSubject;
email.description = emailBody;

email.regardingobjectid = new Lookup();
email.regardingobjectid.type = EntityName.opportunity.ToString();
email.regardingobjectid.Value = new Guid(regardingObjectID);


CrmBoolean direction = new CrmBoolean();
direction.Value = true;
email.directioncode = direction;

TargetCreateEmail targetCreate = new TargetCreateEmail();
targetCreate.Email = email;

CreateRequest request = new CreateRequest();
request.Target = targetCreate;


CrmService myCrm = new CrmService();
myCrm.Url = CRMUrl + "/MSCRMServices/2007/CrmService.asmx";

CrmAuthenticationToken myToken = new CrmAuthenticationToken();
myToken.AuthenticationType = 0;
myToken.OrganizationName = Organization;
myCrm.CrmAuthenticationTokenValue = myToken;
myCrm.Credentials = System.Net.CredentialCache.DefaultCredentials;

CreateResponse response = (CreateResponse)myCrm.Execute(request);


// Create a SendEmail request.
SendEmailRequest req = new SendEmailRequest();
req.EmailId = response.id;
req.TrackingToken = "";
req.IssueSend = true;

// Send the e-mail message.
SendEmailResponse res = (SendEmailResponse)myCrm.Execute(req);

No comments:

Post a Comment