Developing a SharePoint Group Email Web Part

By , September 4, 2009

There are several different situations that I have come across where there is a need to send an email to all of the members of a SharePoint group. This first came up internally when several departments wanted this feature available on their team sites. Users Just wanted to be able to click somewhere on their site and have Outlook open with an email addressed to the members of their team. We were able to find a suitable solution from Advis that did pretty much what we wanted, at least for a while. After using it for months, we found one large flaw that drove me to make my own version… It limited the total size of the string of email addresses being sent to outlook to 255 characters.

In other words, when outlook opened, the To field would be populated with the email addresses of the group of site members, but would be cut off at 255 total characters if there were a lot of members in the site. So, I decided to build my own version and solve that problem while adding a bit more functionality.
EmailWebPart

I added three options in the Editor Part.
1) Email All Site Collection Users (SPSite)
2) Email All SubSite Users (SPWeb)
3) Show All Site Security Groups

I will go over the code in some detail below and make the full code (and .wsp file) available here as I intend to do with all Web Parts I post here, but before I do I want to point out one very important thing about this Web Part. It makes use of Elevated Privileges which I went over here to pull information about the SharePoint security groups. So this allows for every user that can get to a page with this web part on it to email the members of groups that they normally may not be allowed to see. Essentially allowing them to see who is in each group. This was fine in my situation, but if you intend on using it as-is be aware of that. Alternatively, as I provide the code you could change that if you don’t like it. OK, moving on…

This Web Part only has two classes, the main web part class and a custom editor class. It is about 400 total lines of code, so I will only go over the important parts here in the blog, the rest can be downloaded here. There are three buttons that each have their own function attached to them (Mail To, CC To and BCC To) that are all very similar. I will go over the function called when Mail To is clicked below.

void mailToButton_Click(object sender, EventArgs e)
{
    try
    {
        SPSite siteColl = SPContext.Current.Site;
        SPWeb site = SPContext.Current.Web;
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
            {
                using (SPWeb ElevatedWeb = ElevatedsiteColl.OpenWeb(site.ID))
                {
                    SPGroupCollection groups = ElevatedWeb.Groups;
                    SPGroup group;
                    to = "";
                    if (siteUsersCheckBox.Items[0].Selected)
                    {
                        foreach (SPUser user in ElevatedWeb.SiteUsers)
                        {
                            if (user.Email != null && user.Email != "")
                                to += user.Name + " <" + user.Email + ">;";
                        }
                    }
                    if (webUsersCheckBox.Items[0].Selected)
                    {
                        foreach (SPUser user in ElevatedWeb.AllUsers)
                        {
                            if (user.Email != null && user.Email != "")
                                to += user.Name + " <" + user.Email + ">;";
                        }
                    }
                    foreach (ListItem listItem in groupCheckBox.Items)
                    {
                        if (listItem.Selected == true)
                        {
                                group = groups[listItem.Value];

                                foreach (SPUser user in group.Users)
                                {
                                    if (user.Email != null && user.Email != "")
                                        to += user.Name + " <" + user.Email + ">;";
                                }
                        }
                    }
                }
            }
        });
        if (to != "")
        {
            string response = "<script TYPE='text/javascript'>window.location='mailto:" + to + "';</script>";
            Context.Response.Write(response);
        }
        groupCheckBox.ClearSelection();
        siteUsersCheckBox.ClearSelection();
        webUsersCheckBox.ClearSelection();
    }
    catch (Exception ex)
    {
        this.Controls.Add(new LiteralControl(ex.Message));
    }
}

As you can see, the first part of the code above is checking to see what groups were selected, and building the To string accordingly. This is pretty straight forward and there are a couple of different ways you could get at the user email addresses, but this is my preferred method. The last part of the code is the very simple, yet interesting part in that it allowed me to remove the 255 character constraint of the other version. I built a little javascript with a mailto link in it, that will open up the default email client of the user (Outlook in my case) and construct a blank email with the To address filled in.

if (to != "")
{
    string response = "<script TYPE='text/javascript'>window.location='mailto:" + to + "';</script>";
    Context.Response.Write(response);
}

The only problem (if it is a problem) that I can find with my solution is if someone refreshes the page after clicking one of the web part’s buttons. In this case another email message will be opened as it is still in the postback of the page.

The editor class is straight forward, listing three choices which I outlined above. Please feel free to take a look over it and use it if you see fit. Thanks and enjoy.

5 Responses to “Developing a SharePoint Group Email Web Part”

  1. Clayton says:

    Hello Steve…

    I appreciate this web part you have built. Great job with it. We have just run into this limitation and I ran across your solution. I have installed it and it looks great, but it does not seem to work for any group that has 40-ish members or more. It works for a group with 38 members. The next group has 49 and that does not work, nor do any of the larger groups. When I try those, I get an error on the page that says “The data area passed to a system call is too small.” Have you run into this in your testing?

    Thanks for your time and assistance!

  2. Clayton, I haven’t seen that happen and we have a couple sites using it with over 100 users in the lists and we use the firstname.lastname format, so they are pretty long email addresses. What is giving that error message, the web part? or does it take you to a sharepoint error page? Maybe someone doesn’t have an email address entered in their profile…? Just a thought since it said it is “too small”.
    Check the event viewer on the server and the sharepoint logs to see if it gives you something more specific.

  3. Clayton says:

    Steve…Thanks for getting back to me. The errors are from Internet Explorer. I’m using version 7 if that matters. I tried some of the other larger groups (+50 and above) and I get either the “data area” error or an error that says Expected ‘;’. There was one AD item in some of the groups that does not have an email address associated with it so I removed it and tried again but I still get the same errors. Nothing in the event viewer on the server and nothing in the SahrePoint logs that I can see related to this.

    I appreciate your time contacting me about this.

  4. Lim says:

    Hi Steve,
    i am unable to open your sln file. May i know what is EmailWebPartEditor for?

    Thanks.

  5. Jake says:

    Do we have an option to send group email to external email ID

Leave a Reply