Dynamics 365 Web API Email send

可紊 提交于 2019-12-13 15:31:22

问题


I'm building a subscription for pages at a website. So a to-be subscriber post via a form and get added to a marketing list in Dynamics 365 Online.

From a scheduled job at the website i then make a request of the contacts in a marketing list.

Then i need to send them an email that a new page have been created with this properties and a link to that page.

So i would like to make that responsibility Dynamics 365 Online.

So im using the Web API and the action: SendEmailFromTemplate Not sure if i can use this action or if i need to create a custom action.

I would need to pass data such as URL of the page, title and Text. Or just one string - the body of the e-mail message that contains all of this.

So i have created an email template at the CRM and it semse like the code finds it!

This is the action i think i'm after: https://msdn.microsoft.com/en-us/library/mt607523.aspx

If i check the documentation at the SOAP Service there is actually examples: https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.sendemailfromtemplaterequest.aspx?cs-save-lang=1&amp

But i'm going with the Web API.

So i tried this:

dynamic regarding = new ExpandoObject();
var oppIndexer = regarding as IDictionary<string, Object>;
oppIndexer["contactid"] = contact.ContactId; //contact that will recive the 
email.
oppIndexer["@odata.type"] = "Microsoft.Dynamics.CRM.contact";

dynamic target = new ExpandoObject();
var targetIndexer = target as IDictionary<string, Object>;
targetIndexer["torecipients"] = "myemail@outlook.com";
targetIndexer["sender"] = "mysecondemail@businessname.com";
targetIndexer["inreplyto"] = "mysecondemail@businessname.com";
targetIndexer["subject"] = "This is the subject";
targetIndexer["description"] = "This should be the body of the email";
targetIndexer["directioncode"] = true; //outgoing
targetIndexer["@odata.type"] = "Microsoft.Dynamics.CRM.email";

dynamic sendEmailFromTemplate = new ExpandoObject();
sendEmailFromTemplate.TemplateId = Guid.Parse("my-email-template-guid-inserted-here");
sendEmailFromTemplate.Regarding = regarding;
sendEmailFromTemplate.Target = target;

api.ExecuteAction("SendEmailFromTemplate", sendEmailFromTemplate);

But i get exception: "The e-mail must have at least one recipient before it can be sent" when i post it.

What might be the problem?

I think the documentation at: https://msdn.microsoft.com/en-us/library/mt607523.aspx

Just declare that the type for target and regarding should be of type "crmbaseentity". A base class...

Anyone know what might cause this?

This is the Email Entity: https://msdn.microsoft.com/en-us/library/mt608007.aspx

For the field "torecipients" one could read: "Shows the email addresses corresponding to the recipients". So i'm not sure this is the field i should use. If the text would be "The recipients with "," separator that will recive the message" i would be more certain that i would have to use this field.

Update: Okey, so this is what you posted:

{
 "email_activity_parties": [ 
    { "partyid_systemuser@odata.bind": "/systemusers(852a441c-b544-e611-80e3-c4346bc5e750)", "participationtypemask": 1 }, 
    { "partyid_systemuser@odata.bind": "/systemusers(852a441c-b544-e611-80e3-c4346bc5e750)", "participationtypemask": 2 }
  ],
 "description": "description lorem ipsum",
 "subject": "rubrik",
 "regardingobjectid_opportunity_email@odata.bind": "/opportunities(e9e6eb64-9c4c-e611-80e4-c4346bc58294)"
}

If i would to change this so it suits my need. I dont think im going to be using opportunities. Or must i? There are three parameters for SendEmailFromTemplate Action: https://msdn.microsoft.com/en-us/library/mt607523.aspx TemplateId, Regarding and Target. I changed this code a bit below (untested)

{
 "TemplateId": "id-for-email-template",
 "Regarding": [ { "contactid@odata.bind": "/contacts(contact-guid-X)" } ],
 "Target": {
     "email_activity_parties": [ 
        { "partyid_systemuser@odata.bind": "/systemusers(systemuser-guid-Y)", "participationtypemask": 1 }, //1 is "sender"
        { "contactid@odata.bind": "/contacts(contact-guid-X)", "participationtypemask": 2 } // 2 is "to"
      ],
     "description": "description lorem ipsum",
     "subject": "rubrik",
     "contactid@odata.bind": "/contacts(contact-guid-X)"
    }
}

I will try out something like this..


回答1:


Email is an activity, all activities will be split into Activity Pointer + Activity Party. What you have to do is put the same contact you are using for regardingobject into toParty as well.

//create activityparty
 Entity Fromparty = new Entity("activityparty");
 Entity Toparty = new Entity("activityparty");

//To set to Contact
Toparty["partyid"]= new EntityReference("contact", _ contactid));

//From set to User
Fromparty["partyid"]= new EntityReference("systemuser", _From));

//create email Object and set attributes
Entity email = new Entity("email");

email["from"] = new Entity[] { Fromparty };
email["to"] = new Entity[] { Toparty };
email["directioncode"] = true;

//setting the Regarding as Contact
email["regardingobjectid"] = new EntityReference("contact", _contactid);

Ignore torecipients field, use to only.

Error The e-mail must have at least one recipient before it can be sent will go away.

Update:

We have to use navigation property in terms of web api.

//Create email
var email = new JObject(
           new JProperty("email_activity_parties",
             new JArray(
                  new JObject(
                          new JProperty("partyid_systemuser@odata.bind", "/systemusers (<guid>)"),
          new JProperty("participationtypemask", 1)),
                     new JObject(
                           new JProperty("partyid_systemuser@odata.bind", "/systemusers(<guid>)"),
                   new JProperty("participationtypemask", 2)))),
                  new JProperty("description", txtEmail.Text.Replace("\r\n","<br>")),
                  new JProperty("subject", "Test Subject"),
                  new JProperty("regardingobjectid_opportunity_email@odata.bind", "/opportunities(<guid>)"));


来源:https://stackoverflow.com/questions/47450856/dynamics-365-web-api-email-send

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!