web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Send Email from Custom workflow.

(0) ShareShare
ReportReport
Posted on by

Hi ,

I want to send email from my custom workflow.

I am using below code for from address of email.

ActivityParty fromParty = new ActivityParty();

fromParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, loggedinUser);

This code works on my local environment, but when I deploy code then this not works. because from address is not allowed to send the email.  

There is a queue which is set for email send address. But, I don't know How to retrieve queue from my C# code. 

I want to do something like

ActivityParty fromParty = new ActivityParty();

 fromParty.PartyId = new EntityReference(queue.EntityLogicalName, queue1); //queue1 is a queue(don't know how to retirive it)

Please share any sample code.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hello Ashutosh,

    Here is an example:

    stackoverflow.com/.../how-does-one-find-the-id-of-a-queue-in-microsoft-crm-2011

    string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

                         <entity name='queue'>

                           <attribute name='name' />

                           <attribute name='emailaddress' />

                           <attribute name='queueid' />

                           <order attribute='name' descending='false' />

                           <filter type='and'>

                             <condition attribute='name' operator='eq' value='{0}' />

                           </filter>

                         </entity>

                       </fetch>";    

    EntityCollection result = service.RetrieveMultiple(new FetchExpression(String.format(fetchXml, queueName)));

    var myQueue = (Queue)result.Entities[0];

    Console.WriteLine(myQueye.Id);

    Cheers!

  • Ashutosh9 Profile Picture
    on at


    Hi Pedro Pisco,

    Thank you for the reply. I am able to get queued. But when I pass this queueid I got "

    System.NullReferenceException: Object reference not set to an instance of an object." this exception.

    Below is my code I am trying to send email

    string queueName = "Business Sales Operations";
    string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='queue'>
    <attribute name='name' />
    <attribute name='emailaddress' />
    <attribute name='queueid' />
    <order attribute='name' descending='false' />
    <filter type='and'>
    <condition attribute='name' operator='eq' value='{0}' />
    </filter>
    </entity>
    </fetch>";

    EntityCollection result = service.RetrieveMultiple(new FetchExpression(String.Format(fetchXml, queueName)));
    var myQueue = (Queue)result.Entities[0];
    tracingService.Trace("My queue ID " + myQueue.Id);

    // Create Email
    Email email = new Email();

    // Set the from user of the email to Logged in user
    ActivityParty fromParty = new ActivityParty();
    fromParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, loggedinUser); //loggedinUser myQueue.Id
    tracingService.Trace("fromParty.PartyId " + fromParty.PartyId.ToString());

    string subject = string.Empty;
    string description = string.Empty;

    GetEmailData(caseStatus, out subject, out description, caseEntity, tracingService);

    email.Subject = subject;
    email.Description = description;

    List<ActivityParty> lstTo = new List<ActivityParty>();
    //List<ActivityParty> lstFrom = new List<ActivityParty>();

    //var fromActivityParty = new ActivityParty();
    //fromActivityParty.AddressUsed = "emailid@microsoft.com";
    //lstFrom.Add(fromActivityParty);
    //tracingService.Trace("Added " + "emailid@microsoft.com");

    if (!string.IsNullOrEmpty(toEmailfield))
    {
    if (toEmailfield.Contains(','))
    {
    string[] emailAddresses = toEmailfield.Split(',');

    foreach (string emailAddress in emailAddresses)
    {
    var toActivityParty = new ActivityParty();
    toActivityParty.AddressUsed = emailAddress;
    lstTo.Add(toActivityParty);
    }
    }
    else
    {
    var toActivityParty = new ActivityParty();
    toActivityParty.AddressUsed = toEmailfield;
    lstTo.Add(toActivityParty);
    }
    }
    // Add To, Cc and From users to email
    email.From = new[] { fromParty }; //fromParty fromActivityParty
    email.To = lstTo;
    //email.Cc = lstTo;

    // Set the case regarding
    email.RegardingObjectId = new EntityReference(Incident.EntityLogicalName, caseEntity.Id);

    // Create email
    Guid emailId = service.Create(email); //This line is throwing exception.

    SendEmailRequest sendRequest = new SendEmailRequest();
    sendRequest.EmailId = emailId;
    sendRequest.TrackingToken = string.Empty;
    sendRequest.IssueSend = true;
    tracingService.Trace("4");
    // Send the email message.
    service.Execute(sendRequest);
    tracingService.Trace("5");

  • Suggested answer
    TNS Profile Picture
    1,197 on at

    Hi,

    Try your fetch xml like this:

    string queueName = "Business Sales Operations";

    string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

    <entity name='queue'>

    <attribute name='name' />

    <attribute name='emailaddress' />

    <attribute name='queueid' />

    <order attribute='name' descending='false' />

    <filter type='and'>

    <condition attribute='name' operator='eq' value='"+queueName+@"' />

    </filter>

    </entity>

    </fetch>";

    and if you want to use value = '{0}' like this then you can do it like:

    string queueName = "Business Sales Operations";

    string fetchXml = String.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

    <entity name='queue'>

    <attribute name='name' />

    <attribute name='emailaddress' />

    <attribute name='queueid' />

    <order attribute='name' descending='false' />

    <filter type='and'>

    <condition attribute='name' operator='eq' value= '{0}' />

    </filter>

    </entity>

    </fetch>,queueName)";

    Thanks!

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Ashutosh,

    As far as i can see on your code, i believe you want to set the queue Id on the From Party, however looks like it is not being set, you are still using the SystemUser entity.

    ActivityParty fromParty = new ActivityParty();
    fromParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, loggedinUser);

    You should do something like this:

    ActivityParty fromParty = new ActivityParty();
    fromParty.PartyId = new EntityReference(Queue.EntityLogicalName, myQueue.Id);

    Cheers!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans