I have in a previuos post described how to programmatically download a SQL RS report, create a CRM email and attach the report as a pdf to it and send it. This is quite many steps and sometimes it will just be good enough to send mails, for instance when developing addons for workflows.
email em = new email();
activityparty fromparty = new activityparty();
fromparty.partyid = new Lookup();
fromparty.partyid.type = EntityName.systemuser.ToString(); //Change to some other entity if needed.
fromparty.partyid.Value = FROM-SYSTEMUSER-GUID;
em.from = new activityparty[] { fromparty };
activityparty toparty = new activityparty();
toparty.partyid = new Lookup();
toparty.partyid.type = EntityName.contact.ToString(); //Change to some other entity if needed
toparty.partyid.Value = TO-CONTACT-GUID;
em.to = new activityparty[] { toparty };
em.subject = SUBJECT-STRING;
em.sender = “crm@example.com“;
em.regardingobjectid = new Lookup();
em.regardingobjectid.type = EntityName.REGARDING ENTITY TYPE
em.regardingobjectid.Value = REGARDING OBJECT GUID
em.description = BODY-STRING;
em.ownerid = new Owner();
em.ownerid.type = EntityName.systemuser.ToString();
em.ownerid.Value = OWNER-SYSTEMUSER-GUID;
Guid createdEmailGuid = service.Create(em);
SendEmailRequest req = new SendEmailRequest();
req.EmailId = createdEmailGuid;
req.TrackingToken = “”;
req.IssueSend = true;
// Send the email message.
SendEmailResponse res = (SendEmailResponse)service.Execute(req);
I find this code very helpfull. It can probably be generalized by using BusinessEntity object and so forth but I am usually quite satisfied with this.
Gustaf Westerlund
CRM and SharePoint Consultant
Humandata AB
www.humandata.se
This comment has been removed by a blog administrator.
This comment has been removed by the author.
Hi Gustaf,
smart solution!
Do you have an idea to get for example the to-contact-guid dynamically?
I got a usecase where i want to go the other way round and write the "to" guid into annother lookup.value. Here is my draft (taken code snipets from Blakes CRM Blog), unfortunatly not working. You have an idea to get it work?
Thanks in advance
// Obtain the target business entity from the input parmameters.
DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];
// Verify that the entity represents an fax
if (entity.Name == "fax")
{
// Verify that properties contains to
DynamicEntity activity = (DynamicEntity)context.InputParameters.Properties["Target"];
if (activity.Properties.Contains("to"))
{
// lookup the first partyid.value insert into another lookup
DynamicEntity[] to = (DynamicEntity[])activity.Properties["to"];
if (to.Count() > 0 && to[0].Properties.Contains("partyid"))
{
Lookup partyId = (Lookup)to[0].Properties["partyid"];
Lookup RegardingContact = new Lookup();
RegardingContact.type = EntityName.contact.ToString();
RegardingContact.Value = new Guid(partyId.Value.ToString());
LookupProperty RegardCont = new LookupProperty("crm1_regardingcontactid", RegardingContact);
entity.Properties.Add(RegardCont);
Well, I'd try to debug it a bit. Since you are working with dynamic entities it's always good to make sure of every step and all the data.
Gustaf, is there a way to send an email and not have it tracked?
Yes, by not using the CRM functionality for sending emails, but using the standard .NET classes for this instead. I don't think there is a way to not have it tracked and still sent from CRM.