Another question from a forum user:
"I am trying to create a link that will reproduce the same functionality that exists for the "Join Group" menu option when working with a SharePoint security Group. I want to allow members to be able to Request Membership to a Specific SharePoint Group. The group is set up to auto-accept. The administrators of the site\group send out e-mails to members of the group. For that reason we do not include everyone in the group by default. But if they request access, they should be auto-approved. Rather than trying to explain to users how to navigate to the Group Page where the "Action"-> "Join Group" menu option is, I would like to create a link from the home page to re-create the same functionality.
Any ideas? "
Why of course!
To understand how the link works we need to dig a bit. In contrast with regular Action links, the Join Group is not a CustomAction, which would have been too simple. Rather, the Join Group and Leave Group links are created only in the people.aspx page itself.
.Net reflector to the rescue!
The ASPX page reveals that the page inherits the Microsoft.SharePoint.ApplicationPages.PeoplePage class. Using .Net reflector we open the Microsoft.SharePoint.ApplicationPages dll, usually located in \CONFIG\BIN\Microsoft.SharePoint.ApplicationPages.dll. Browsing to the PeoplePage class we find the method that is fired:
Click on that method reveals the following code:
protected void BtnJoinLeaveGroup_Click(object sender, EventArgs e)
str = "reqgroup.aspx?Group=" + SPHttpUtility.UrlKeyValueEncode(this.CurrentGroup.Name);
str = "reqgroupconfirm.aspx?Action=";
str = str + "Join&";
str = str + "Leave&";
str = str + SPHttpUtility.UrlKeyValueEncode("Group", this.CurrentGroup.Name);
SPUtility.Redirect(str + "&" + SPHttpUtility.UrlKeyValueEncode("Source", SPHttpUtility.UrlPathEncode(base.CurrentRequestUrlAndQuery, false)), SPRedirectFlags.RelativeToLayoutsPage, this.Context);
From this we see that there is no Url that is used to add a user, but rather a method call to the SPGroup.Adduser method. Doh! No simple linking possible.
Or is it…?
To mimic the functionality we need to add a new page to hold the code for adding a user to a group. Since we have reflected the dll we know a bit about how that code should look as well, so let’s get down to business.
Creating a page to add users automatically
Start by creating a regular SharePoint application page using your favorite authoring tool. Since all we want to do is to add a user to a group, we just need to override the OnLoad method. Add the following to your .aspx file:
<%@ Assembly Name="Microsoft.SharePoint, Version=126.96.36.199, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" MasterPageFile="~/_layouts/application.master" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
Of course, this doesn’t actually do anything, so we need to modify the overridden method. Substitute base.Onload(e); with this (Comments inline to explain what is going on):
protected override void OnLoad(EventArgs e)
SPWeb web = SPContext.Current.Web;
// Get group id from query string
string groupName = this.Page.Request["GroupName"];
// Find Url to redirect user after addition
string referringPage = "";
if (this.Page.Request.UrlReferrer != null)
referringPage = this.Page.Request.UrlReferrer.ToString();
// Redirect to root url if direct link
// You might want to modify this behavior
referringPage = web.ServerRelativeUrl;
// Get reference to current web and group
currentGroup = web.Groups[groupName];
catch (Exception ex)
throw new SPException(ex.Message);
// Add current user to group. Must allow unsafe updates since we are doing this through GET
web.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = false;
// Redirect user back to previous page.
Right. That should handle our code, so just save the file to the \LAYOUTS directory, and add a link to test. You link should look something like this:
Note that an automatic adding to a group, such as this, requires that you have both set the "Allow reque
sts to join/leave this group? " and "Auto-accept requests?" to Yes. You can do this on the Group Settings page.
You might also want to improve on the code, add better error and exception handling, modifying the functionality of redirects, etc. but I will leave that as an exercise to you.
Let me know if you have problems.
Found this article valuable? Want to show your appreciation? Here are some options:
a) Click on the banners anywhere on the site to visit my blog's sponsors. They are all hand-picked and are selected based on providing great products and services to the SharePoint community.
b) Donate Bitcoins! I love Bitcoins, and you can donate if you'd like by clicking the button below.
c) Spread the word! Below, you should find links to sharing this article on your favorite social media sites. I'm an attention junkie, so sharing is caring in my book!