Tuesday, August 18, 2009

Enabling discussion boards for email interaction

Funnily enough I have not found many people complaining about the lack of proper email integration with discussion boards. Probably because they are all using Outlook 2007 by now :-) and there you have full Discussion Board integration just like you can integrate tasks, contacts and calendars.

Within Sharepoint there is the option to accept incoming emails for lists and libraries. This feature works like a dream. First you configure your incoming email settings in Central Administration (Operations Tab) and then configure an incoming email address for the list or library in its settings (Communications column beneath RSS settings).

So far so good. Now you should be able to start new discussions simply by sending an email to a discussion board. An example email address could look like salesdiscussions@portal.litwareinc.com . Cool! But how do you reply to a thread?
This is where the main option is missing. You can only reply to a discussion if you subscribed to it and received the discussion in your email. But guess what, that option does not exist in SharePoint.
You might think, "hey, let me try alerts" And although they are a great tool for all other kinds of lists and libraries, they are not meant to be discribution channels for discussions and thus replying to an alert will not get you anywhere near where you want to be. Two problems. first, the sender is a fixed email address for all alerts, so replying to it will not end up in the list that you want to end up in, and secondly the format of the email is totally wrong for email threads.

Now If you posted the original thread via email, opened that email from your sent items, replied to it by including the original text in the email and changed the To: email to the one of the discussion board, it would actually work! Whopee!
So SharePoint is clever enough to strip out the "Re:" bit, match the subject up and also match up the body to figure out which post you're replying to.

Thus the only feature which is really missing is the Discussion Subscription mechanism and the distribution of discussions to the subscribers.

There are a few options thinkable. Now these are just hypothetic...
1) Create an event handler for the discussion board which reads out the alerts set up on the item/list and sends an email to the subscribers.
PRO: simple to set up and maintain
CON: users receive two emails, the subscription and the alert.

2) Create a feature which replaces the alert feature on that discussion board and maintains its own list of subscribers which then get "spammed" through an event handler.
PRO: users only receive on email
CON: need to create additional User Interface components for subscribing and unsubscribing to discussions and discussion boards

3) Create your own custom email alert handler to catch the alerts and reformat them by adding the original body and changing the reply to field to the list's email address. here is a great article to get you started on this route:
How to create a custom email alert hander by Microsoft http://support.microsoft.com/kb/948321
Just the SPUtility.SendEmail function does not allow you to specify a reply to address, so I would use a different email sending function such as the built in smtp functions in System.Net.Mail.

Two great articles on customising alert templates in general:
1) How to customise the built in alert template by Brett Lonsdale at Combined-Knowledge http://www.combined-knowledge.com/Downloads/Modify_Alert_Notifications.pdf

2) customising Alert Templates in WSS 3.0 on the SharePoint Team Blog
http://blogs.msdn.com/sharepointdeveloperdocs/archive/2007/12/07/customizing-alert-notifications-and-alert-templates-in-windows-sharepoint-services-3-0.aspx


I've tried playing with the custom alert handler and got as far as sending out minimal streamlined emails but the date stamp on the email is different and thus the email hander is not clever enough to match them up. thus you would need to write your custom incoming email handler for the discussionboard as well. here is a link to get you started:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spemaileventreceiver.aspx

15 comments:

alx said...

since I wrote this article I've tried it out and got as far as formatting the alerts to an absoulte minimum, sending them via system.net.mail and changing the reply-to address but the timestamp of the original post and the alert is different and thus the built in engine can not match up the email with the post and creates a new thread instead.
Effectively the next step would be to create your own email handler which is cleverer than the built in one for discussions.

Dan Blaker said...

I've been considering another approach, which is to send notification emails via a workflow. It would work like this:
- In the discussion forum list, there's a custom column for "distribution group". This custom column is either a people picker or a Choice type, with each user's name & email as a checkbox.
- A user creates a new discussion thread from the web interface, then either enters the email addresses or checks the appropriate boxes for the people she wants to send the message to.
- An SPD workflow based off this custom SPD action is attached to the creation of a new item, which checks the email addresses in the "distribution group" and sends an email containing the body of the discussion message to all of them.
- The "From" address on the email is the discussion list's email address, so users can reply directly from email and it will thread them properly.

This is still in the brainstorming phase right now. I still have to figure out how to ensure the same distribution group is used for reply notifications.

On the other hand, maybe we force the users to reply on the site, where they can specify the distribution group; people are accustomed to this process on Facebook, for example, where you're notified of the response but can't reply directly via email.

alx said...

Hey Dan,
Using a SPD Workflow is a nice and simple way to add some interactivity to the discussion board and although you might be able to get people notified, the reply will not work. There is a definite time stamp issue with the reply and you would have to create an eventhandler on the list to deal with the email responses correctly.
But if you simply put a link in the email pointing to the reply url and force them to reply online, you will at least have half the problem of notification solved. Although you hardly need a custom field when you have alerts for this kind of thing. Did you know that it is actually quite simple to change the alert templates and customise them?

Dan Blaker said...

The main problem I want to solve is that team members don't want to get alerts for every discussion item. In my approach (which is clearly flawed in a couple ways), the message creator can decide who should get a notification.

Apparently I read your post too quickly and missed a bunch of things that will make my approach incomplete. Like you say, putting the reply link in the email solves half the problem; but I'm still trying to figure out the best way to populate/display the "distribution group" column.

alx said...

Hmm. you know that people can subscribe to alerts on an individual item and not only on the whole list?
Although that will not have completely the desired effect as alerts will not be triggered on replies. But a clever event handler could take care of this (updating a hidden field on the main thread)

Dan Blaker said...

In fact, I didn't know that you could set up alerts for individual "threads". Thanks for that info!

I'm pretty sure I'm going to have to crack open Visual Studio regardless of which approach I take. I'm not a frequent C# coder, so I try to get as much done in SPD as possible. But ultimately I want to include the discussion forum with custom notifications as part of a site template, and of course SPD workflows are not portable.

alx said...

BTW. you can try to port the workflow from SharePoint Designer to Visual Studio and package it up for reuse. Here are a few simple steps to get you going:
SharePoint Mythbuster - Porting SharePoint Designer Worfklows to Visual Studio

fadwa said...

Dear Alex,

I created spemaileventreceiver and add to the board discussion sharepoint , it's work fine but the problem is the sender and subject it display system account and no title, although the email have subject how i can display the original sender and the subject of the email

alx said...

if you have the email receiver working, then you should set the title of the item in the receiver by analysing the email properties. Setting the created field works the same way. You need to specify the ID of the user, so you will need to find that in the Users List by using the from email and hope to find the person there.
here is a msdn article on how to set different properties on a listitem including how to find and set the author
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.aspx

fadwa said...

Dear Alex,

this is the code which i make it inside the spemaileventreceiver
Dim item As SPListItem = list.Items.Add
item("Title") = emailMessage.Headers("Subject")
item("Body") = emailMessage.HtmlBody
item("Sender")=emailmessage.sender
item.update
how i can get the orginal subject and the sender it display the system accoutn

alx said...
This comment has been removed by the author.
alx said...

Try setting the Author using the user from sharepoint.
First get hold of the SPUser object using the email:

SPUser author;
try{
author = web.AllUsers.GetByEmail(Regex.Replace(emailMessage.Sender, @"\<[^\>]*\>", "")); //the regex strips out any friendly names from the email
}

catch{
author = web.SiteAdministrators[0];
//if the email is not in sharepoint use the site collection admin instead.
}
// now assign the author
item[SPBuiltInFieldId.Author] = author.ID;

Finally try using HTML Encoding on the Title:

item[SPBuiltInFieldId.Title] = SPHttpUtility.HtmlEncode(emailMessage.Headers["subject"]);


Hope this helps

fadwa said...

I try to use the code which u send it to me but the same result appear no changes

alx said...

That is odd. Unless the email address who is sending the emails belongs to the same account which was used to install SharePoint. In this case it will always show as system account due to the badly set up SharePoint farm. Did you try sending from another email, which is assigned to a person on sharepoint?
It is also possible that the person sending the emails has never signed into SharePoint which means that the profile has hot been created yet. That can also cause issues.

But otherwise I can't help any further without looking at your exact environment and debugging the code line by line.

kuk said...

got a very good information i am also working on the same thing replying a thread i have created a emailreceiver which will check the subject of email and will add as a reply to the thread having the same subject...
this is my code i think something is wrong here i m not able to find can someone help me here