Welcome to MSDN Blogs Sign in | Join | Help

Sending email via workflow as different user

One of the very common customer ask regarding emails and workflow is how to send emails from workflow where the “from” field is set to some one other than the owner of the workflow rules. Two simple scenarios could be:

a) When a contact is created an automated thank you email should be sent from an alias such as helpdesk (queue).
b) When a contact is created an automated thank you email should be sent from the owner of the entity rather than workflow rule owner.

To achieve this, we first write a simple .net assembly that sends an email using a predefined email template to the requested party. Next, we use this .net assembly in the workflow rule to send the email.

Steps involved are:

1) Creating a .net assembly:

a. Create a sample project of name “CrmTest” whose output type is set to class library. I.e. it outputs a dll.
b. Add the web references to the project pointing to the crm server you have. The web reference url looks like: http://localhost/mscrmservices/2006/crmservice.asmx
c. Copy the below code in .cs file, compile the code and place the output dll into the assembly directory on the server.(C:\Program Files\Microsoft CRM\Server\bin\assembly)

using System;

using System.Text;

using System.Xml;

using CrmTest.localhost;

 

namespace CrmTest

{

      public class Test

      {

            public void SendTemplateEmailAs (

                  string callerXml,

                  Guid senderId,

                  string senderType,

                  Guid recipientId,

                  string recipientType,

                  Guid templateId)

            {

                  SendTemplateRequest request = new SendTemplateRequest();

                  // Set sender.

                  request.Sender = new Moniker();

                  request.Sender.Name = senderType;

                  request.Sender.Id = senderId;

 

                  // Set recipient.

                  request.RecipientType = recipientType;

                  request.RecipientIds = new Guid[] { recipientId };

 

                  // Set regarding.

                  request.RegardingType = recipientType;

                  request.RegardingId = recipientId;

 

                  // Set email template.

                  request.TemplateId = templateId;

 

                  // Create crm service.

                  CrmService svc = new CrmService();

                  svc.Credentials = System.Net.CredentialCache.DefaultCredentials;

                  svc.Timeout = -1;

                  // For auditing pupose set the callerId value.

                  svc.CallerIdValue = new CallerId();

                  // When seding as helpdesk set teh called id from the callerXml.

                  svc.CallerIdValue.CallerGuid = GetUserId(callerXml);

                  // When sending as the entity owner set the caller id to sender.

                  //svc.CallerIdValue.CallerGuid = senderId;

 

 

                  // Execute the request.

                  svc.Execute(request);

            }

 

            private Guid GetUserId(string callerXml)

            {

                  //Note: the callerXml is of the fomat:

                  //callerXml = "<caller><userid>684F160A-E4D9-4C3C-ADDD-1464D2B14EB2</userid><merchantid>F671739E-189A-4EE8-AF86-66DC8CF5F6C4</merchantid></caller>";

                  XmlDocument xmldoc = new XmlDocument();

                  xmldoc.LoadXml(callerXml);

 

                  return new Guid(xmldoc.DocumentElement.SelectSingleNode("//caller/userid").InnerText);

            }

      }

}

2) Registering the .net assembly with workflow.

a. Registration for sending email as helpdesk (Queue)

<method name="SendTemplateEmailAsHelpDesk" assembly="CrmTest.dll" typename="CrmTest.Test" methodname="SendTemplateEmailAs" group="Utility">

            <parameter name="callerXml" datatype="caller"/>

            <parameter name="SenderId" datatype="lookup" entityname="queue"/>

            <parameter name="SenderType" datatype="string" default="queue"/>

            <parameter name="RecipientId" datatype="lookup" entityname="contact"/>

            <parameter name="RecipientType" datatype="string" default="contact"/>

            <parameter name="TemplateId" datatype="lookup" entityname="template"/>

</method>

b. Registration for sending email as entity owner (system user)

<method name="SendTemplateEmailAsEntityOwner" assembly="CrmTest.dll" typename="CrmTest.Test" methodname="SendTemplateEmailAs" group="Utility">

            <parameter name="callerXml" datatype="caller"/>

            <parameter name="SenderId" datatype="lookup" entityname="systemuser"/>

            <parameter name="SenderType" datatype="string" default="systemuser"/>

            <parameter name="RecipientId" datatype="lookup" entityname="contact"/>

            <parameter name="RecipientType" datatype="string" default="contact"/>

            <parameter name="TemplateId" datatype="lookup" entityname="template"/>

</method>

3) Constructing the workflow rule.

a) Workflow rule for sending email as Helpdesk (queue). Note you will have to create a queue with name helpdesk before creating the workflow rule.

b) Workflow rule for sending email as the entity owner.

4) Try it out :o).

Notes:
1) Ensure that the Bulk email service is running as the send email template users the bulk email service to send emails.
2) The invoking user should have “append to” privileges for queues for the workflow rule “Send as helpdesk” to work. During automatic invocation the rule owner credentials will be used and if invoked via the UI, the callers credential will be used.

Shashi Ranjan

Published Wednesday, November 15, 2006 10:04 AM by crmblog

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Signature Blocks in Workflow

For some time I have had a CRM workflow rule associated with new Leads that sends them an e-mail saying...

Thursday, November 16, 2006 9:28 PM by Larry's Taco Talk

# re: Sending email via workflow as different user

How can you send emails to someone other than users and logical users?  

My client uses a national Service network, these people are not part of the company and do not access MSCRM. However when Someone logis into the clients web site for service everything has to be rekeyed to send the information via email to the Service Person responsible.  

Friday, November 17, 2006 2:19 PM by John

# re: Sending email via workflow as different user

I have an issue while trying to implement this solution. I have a template for "case acknowledgement". In that template i have used slugs like "case.customer" and "case.owner". But none of these seems to be coming out as expected in the mail that is finally sent. Currently what i notice is that all the slugs that are "user" related, for e,g. User.PrimaryEmail , is coming properly in the mail that is being sent and is getting replaced with the corresponding value of the user who owns that case. Any ideas?

thank you

Wednesday, December 06, 2006 5:01 AM by jijeeshkr

# re: Sending email via workflow as different user

Hi !

When I try to send a mail using the SendTemplateRequest object, the mail is sent with the good template to the right person from the right user but there is no databinding between the RegardingId object and the template, it means that I have no values in the mail.

I checked the RegardingId and the RegardingType and everything is ok... Any Ideas ?

Thanks

Monday, December 18, 2006 11:39 AM by Julien

# sending email via workflow automatically

The problem with me is if i send email by applying rule manually then it sends the email but automatically through workflow it is not sending at the time of creation of new record

(In workflow moniter it shows paused)

Rule  use for case in workflow manager is somewhat like this:

when a case is created

Send Email Template:Case Creation

I saved this case rule and activated in workflow manager

then i created another rule named lead creation

Rule  use for lead in workflow manager is somewhat like this:

when a lead is created

Wait

Lead.Topic = not null

end wait

Email Template:Lead Creation

I saved this lead rule and activated in workflow manager

(At the time of creation of new lead record in workflow monitor it shows active)but no mail is being received automatically.

(Note there is no problem with the above templates)

Please provide a solution to simple workflow rules

Sunday, February 11, 2007 9:10 AM by Sapna Lohia

# sending email via workflow automatically

My problem is as following:

Rule named Case Creation Rule

Rule  used for case in workflow manager is somewhat like this:

when a case is created

Send Email Template:Case Creation

I saved this case rule and activated in workflow manager

At the time of creation of a new case record it shows paused in workflow moniter.On the other hand When i apply this rule through CRM Actions-apply rule it sends the email and in the Workplace-activity it shows email..  completed

Rule named Lead Creation Rule

Rule  use for lead in workflow manager is somewhat like this:

when a lead is created

wait

Lead.Topic=not null

end wait

Email Template:Lead creation

I saved this lead rule and activated in workflow manager

When a new lead record is created automatically it shows in workflow moniter

active but I am not receiving any mail.

(pls note there is no problem with the above used email templates)

Sunday, February 11, 2007 9:30 AM by Sapna Lohia

# re: Sending email via workflow as different user

Hi Sapna Lohia,

you are hitting permission issue. Look at the Note: "The invoking user should have “append to” privileges for queues for the workflow rule “Send as helpdesk” to work. During automatic invocation the rule owner credentials will be used and if invoked via the UI, the callers credential will be used."

Friday, February 16, 2007 8:08 PM by Shashi

# re: Sending email via workflow as different user

Shashi,

I have following difficulties with doing

as you describe:

- I get a warning stating that the namespace

 is unknown (perhaps some missing webreference)

 How should I translate 'localhost' on my

 own server, or doesn't it need to be adapted

 to my server at all?

- 'Registering for sending the email as...'

 How's that done?

Thursday, March 01, 2007 7:54 AM by Mujo

# re: Sending email via workflow as different user

How do I register the .DLL with the workflow

as you mention?

'regsvr32 CrmTets.dll'

results in a windows error that the

module could not be found.

2nd: I get a building warning in Visual Studio

concerning the 'namespace'. How to adapt

Localhost to my own situation?

Thursday, March 01, 2007 8:18 AM by Mujo

# re: Sending email via workflow as different user

I’m trying to send an automated e-mail from a queue Helpdesk after a new case has been made.

All the routing in Exchange/Outlook is going great, because I can send and receive mails in there.

The one thing that doesn’t work is when I reply to such an e-mail, is to drop it in the self-created queue Helpdesk.

Here’s what I’ve done so far.

Did all the steps from the URL

- created a *.DLL in Visual Studio

- copied it to the assembly-map

- edited Workflow.config to include the piece

 of method-code for the queue

- after first alerts within Workflowmonitor

 that my piece of *.dll was not registered

 I’ve added allowunregistered dll’s to be ‘true’

- created a queue within crm called ‘Helpdesk’, with e-mailaddress helpdesk@company.nl

- created the workflow, upon creation of a case, call assembly, etc.

Workflowmonitor completes the workflow succesfully, no errors, just completed

(states ‘ok’ at details per event).

For some reason I can sent back and forth e-mails through various e-mail accounts

(such as helpdesk@company.nl and userA@company.nl), but when I go into CRM queue Helpdesk, it is empty.

Do you have any suggestions from here?

Regards

Friday, March 02, 2007 5:18 AM by Mujo

# Extending [Logical Users] list in Workflow

Hi guys, When you want to send an email via the Workflow manager inside your rule, you have the choice

Monday, March 12, 2007 11:59 AM by Microsoft CRM Support Tools Info

# re: Sending email via workflow as different user

I made an assembly based on your sample.

For some time it works fine, but then CRM Workflow starts failing with Error 80042801 - Method xxxx is not registered in workflow.config file. however it is.

Friday, March 16, 2007 10:44 AM by Alexander Nonikov

# re: Sending email via workflow as different user

Have added the assembly and i can successful select when defining the action - I am trying to set up an auto reply as per the example above however I can only set the recipientid equal to an actual contact I want to set it equal to the sender of the originating email.

Tuesday, April 03, 2007 11:15 AM by dyawlak

# re: Sending email via workflow as different user

Shashi,

I have following difficulties with doing

as you describe:

- I get a warning stating that the namespace

is unknown (perhaps some missing webreference)

How should I translate 'localhost' on my

own server, or doesn't it need to be adapted

to my server at all?

- 'Registering for sending the email as...'

How's that done?

Sunday, January 25, 2009 5:29 AM by rojan

# re: Sending email via workflow as different user

Shashi,

I have following difficulties with doing

as you describe:

- I get a warning stating that the namespace

is unknown (perhaps some missing webreference)

How should I translate 'localhost' on my

own server, or doesn't it need to be adapted

to my server at all?

- 'Registering for sending the email as...'

How's that done?

Sunday, January 25, 2009 5:40 AM by sd_taleshi@yahoo.com

# re: Sending email via workflow as different user

Hi,

i have problem in MS CRM 3.0, i am focusing on how to send a bulk mail through the EMail Campaign, if anyone have idea means, please share with me,

regards

areef

Thursday, May 14, 2009 6:11 AM by Abdul Areef

# re: Sending email via workflow as different user

Hi,

I was developing the new workflow assembly for CRM 3.0 based on this example and found a problem.

I need to send the email to multiple users. So i need to include CC field on email.

Can anyone help me?

Thanks,

---------------------------------

Jessica

Thursday, July 30, 2009 6:04 AM by Jessica Conceição

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker