Navigation Bar

Wednesday, April 8, 2009

CRM

public
void SendEmailUsingActivity(string ToAccountID, string Subject,string Body, string AttachmentPath,string FileName)
{
//Tracking Send Email in CRM Activity
string loginid = "";
CrmService serviceusers = new CrmService();
serviceusers.Credentials = System.Net.CredentialCache.DefaultCredentials;
WhoAmIRequest whoAmI = new WhoAmIRequest();
WhoAmIResponse Iam = (WhoAmIResponse)serviceusers.Execute(whoAmI);
Guid userid = Iam.UserId;
loginid = userid.ToString();

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

try
{
// create an email
email emailCreate = new email();
string tdate = DateTime.Today.ToString("dd/MM/yyyy");
emailCreate.actualstart = new CrmDateTime();
emailCreate.actualstart.Value = Utilities.getFormatedDate(tdate.ToString());
emailCreate.actualend = new CrmDateTime();
emailCreate.actualend.Value = Utilities.getFormatedDate(tdate.ToString());

// specify the FROM part of the email
activityparty from = new activityparty();
from.partyid = new Lookup();
from.partyid.type = EntityName.systemuser.ToString();
from.partyid.Value = new Guid(loginid);
emailCreate.from = new activityparty[] { from };

// specify the TO part of the email
string[] members = new string[1];
members[0] = ToAccountID;
activityparty[] ap = new activityparty[members.Length];
int i = 0;
foreach (String memberID in members)
{
ap[i] = new activityparty();
ap[i].partyid = new Lookup();
ap[i].partyid.type = EntityName.account.ToString();
//ap[i].partyid.type = EntityName.contact.ToString();
ap[i].partyid.Value = new Guid(memberID);
i++;
}
emailCreate.to = ap;

//Specify Subject & Body
emailCreate.subject = Subject;
emailCreate.description = Body.Replace("\n","
");
Guid emailId = service.Create(emailCreate);
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailId;
req.TrackingToken = "";
req.IssueSend = true;
if (AttachmentPath != "")
{
//Add an attachment
// Create a new Attachment object.
// Attach it to the email.
activitymimeattachment attachment = new activitymimeattachment();
attachment.activityid = new Lookup();
attachment.activityid.Value = emailId;
attachment.activityid.type = EntityName.email.ToString();
attachment.attachmentnumber = new CrmNumber();
attachment.attachmentnumber.Value = 1;

// Create the Attachment in CRM.
Guid attachmentId = service.Create(attachment);

// Get a pointer to the file and open up a stream
FileInfo pointer = new FileInfo(AttachmentPath);
FileStream fileStream = pointer.OpenRead();
// Encode the data using base64
byte[] byteData = new byte[(int)fileStream.Length];
fileStream.Read(byteData, 0, (int)fileStream.Length);
string encodedData = System.Convert.ToBase64String(byteData);
//close the stream.
fileStream.Flush();
fileStream.Close();
// Create the request object to upload the file to CRM.
UploadFromBase64DataActivityMimeAttachmentRequest upload = new UploadFromBase64DataActivityMimeAttachmentRequest();
// Set the properties of the request object.
upload.ActivityMimeAttachmentId = attachmentId;
upload.FileName = FileName;
upload.MimeType = "application/octet-stream";
upload.Base64Data = encodedData;
// Upload the file to CRM.
UploadFromBase64DataActivityMimeAttachmentResponse uploaded = (UploadFromBase64DataActivityMimeAttachmentResponse)service.Execute(upload);
}
// Finally Send the email message.
SendEmailResponse res = (SendEmailResponse)service.Execute(req);
}
catch (Exception ex)
{
//Response.Write(ex.StackTrace.ToString());
}
service.Dispose();
}

No comments:

Post a Comment