Friday, February 15, 2013

Project Server - Workflow approval email: Approval required for project: '... has been assigned to you

The full potential of Project Server is realized only when a powerful workflow solution is deployed along with it. One of the best workflow solutions is Solution Starters Dynamic Workflow (and it is free), which I mentioned in on of my previous posts.

Dynamic Workflow is great solution, but, it has many faults as well. One of them are email notifications to users who have workflow tasks assigned to them (approvers).

When project is send to approval, all tasks for all users are stored in SharePoint list called Project Server Workflow Tasks, and in that moment email notification is send to users who are approvers for that task.

This how this email looks like:




The problem is in its subject:

Project Server Workflow Tasks - Approval required for project: '... has been assigned to you

 In subject (or anywhere else in email) user can not see the name of the project, so user does not know what project he must approve/reject.

I have searched for the solution of this problem, but without success. These notifications are SharePoint generated emails, and subject of the email and the body are generated from series of XML definitions from SharePoint XML files (file is called alertemplates.xml and it is located in
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML

But, modifying this XML file is not recommendable, because all list on entire farm are using it, and it is also complicated to find what you need to change in order to get the desired results. I my next post, I will describe how to make your custom alert template XML file.

So, I've come up with a different solution. Solution is to turn off email notifications for this SharePoint list and to build a custom event receiver for that list which will send its own notifications with content that you desire:

SOLUTION:

1. Go to SharePoint list "Project Server Workflow Tasks":
     Site Actions --> Site Settings --> Site libraries and lists (in Site Administration) --> Customize Project Server Workflow Tasks
2. Click on Advanced settings in section General Settings.
3. In section E-mail Notification, select option "No" for Send e-mail when ownership is assigned option.

4. Now it is time to open Visual Studio 2010 and build your event handler.

NOTE: Building an event handler for this particular list is not straight forward, but it can be found on blogs, so I will not go in detail here. If someone needs help with this part, leave a comment, and will try to help.

5. After the event handler has been made, System.Net.Mail library can be used for sending mail form code. Created new class and create following class in it. In it, you can read task title, owner of the task, approver, etc from SharePoint list. Here is the code:


class ProjectWorkflowEvent: SPItemEventReceiver
{
     public override void ItemAdded(SPItemEventProperties properties)
     {
           base.ItemUpdated(properties);
          
           //reading title value from Project Server Workflow Tasks list
           string _title = properties.ListItem["Title"].ToString();
           string _decision = properties.ListItem["Outcome"].ToString();
            
           SPFieldLookupValue _assignedTo = new SPFieldLookupValue(properties.ListItem["Assigned      To"].ToString());
           string _assignedName = _assignedTo.LookupValue;

           SPSite oSiteCollection = new SPSite(properties.WebUrl);
              
           string _userMail = "";

           using (SPWeb oWebSite = oSiteCollection.OpenWeb())
           {
                 //read user email address
                 SPUser oUser = oWebSite.EnsureUser(_assignedName);
                 _userMail = oUser.Email;
            }

            //send email  
            string smtpServer =    SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
            string smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;

           //Create the mail message and supply it with from and to info
           MailMessage mailMessage = new MailMessage(from@address.com, to@address.com);

           //Set the subject and body of the message
           mailMessage.Subject = "Approval required for project: '" + _title + "' has been assigned to you";
           mailMessage.Body = "My custom body";
                  
         
           //Create the SMTP client object and send the message
           SmtpClient smtpClient = new SmtpClient(smtpServer);
           smtpClient.Send(mailMessage);
      }
}

Now, your email can look something like this (or what ever you like):

8 comments:

  1. Hi! Great post. But when I tried to do it, in this line:
    public class Class1 : SPItemEventReceiver

    Visual Studio 2010 tell me: The type or namespace could not be found. No meeting dll de Microsoft.Sharepoint... What I can do?

    Thanks!

    ReplyDelete
  2. Two possible solutions
    1. Do you have Microsoft.SharePoint.dll included in references?
    2. Go to project properties and switch from .NET Framework 3.5 Client Profile to .NET Framework 3.5 (or 4.0)

    Let me know if it helped.
    Regards

    ReplyDelete
    Replies
    1. Thanks a lot. I don´t have de SharePoint.dll but I Copied form another computer. Now I am trying to deploy de event in a sharepoint site, but when y write this in the command prompt:

      stsadm -o activatefeature -filename EventHandler\Feature.xml -url http://Server/Site/Subsite

      I get: Operation is not valid due the current state of the object.

      Why? what object? Thanks!

      Delete
    2. Check this article (and look at the comments below the article):
      http://sharepointx-men.com/2010/07/07/error-occurred-in-deployment-step-activate-features-operation-is-not-valid-due-to-the-current-state-of-the-object/

      Delete
  3. Hi,

    Thanks a lot for this wonderful post.Can you please let me know how to register the event receiver in Project Server Workflow Tasks.

    Thanks in advance

    ReplyDelete
    Replies
    1. Here are detailed instructions on Event Handler deployment and registering:
      http://msdn.microsoft.com/en-us/library/gg615466%28v=office.14%29.aspx
      If you have any problems, let me know.

      Delete
    2. Thanks a lot Mario.I am facing difficulties in finding the correct event to which to attach this receiver. The requirement is to fire it when an item is added to the Project Server Workflow Task List.Any help is really welcome as i am completely stuck

      Delete
    3. Well, if your task is to register event on that list, then your job is simple.
      Just go to Visual Studio and create new project --> in templates select Visual C# --> SharePoint --> 2010 --> on the right side select "Event Receiver", click Next.
      On the next screen write the location to your PWA site --> click Next.
      On the next screen select "List Item Events", then list "Tasks" and select checkbox on the event "An item is being added" (or "An item was added") and click Finish.

      When you deploy that solution, you have new event receiver on your list.

      That list is plain SharePoint list, so it can be handled in this way (no need for complex Project Server events).

      Check out this post:
      http://sharepoint1on1.blogspot.com/2012/10/disable-event-firing-part-3-multiple.html

      Here you can see description of small and useful program for events on your SP Farm. With this tool you can verify how many event receivers you have on you lists.

      Delete