How I simplified the Users and Group UI – Removing Users

The final component in my users and groups UI is the ability to remove users. This is based very much on how SharePoint works – and it’s a little ugly. My apologies -but this is how SharePoint does it.

Our ListView shows our users. Each user has a checkbox next to them:

ListView

I’ve added a ‘Remove Users’ LinkButton to my fake ‘toolbar’:

Title and View Controls

That LinkButton is defined in my page as:

<asp:LinkButton UseSubmitBehavior="false" id="BtnRemoveUsersFromGroup" runat="server"
Text="Remove Users"
OnClick="BtnRemoveUsersFromGroup_Click"
OnClientClick="return BtnRemoveUsersClick();"
/>

So, on click it runs some client script, and then posts back a click event. So, how do you know which users have been selected within the postback? You could have zero-to-N checkboxes!

Well, the client script is some JavaScript that checks all the checkboxes, appends their values into a hidden field, and it’s that hidden field that is posted back for the click event.

This function gets the selected users (yes, I’m using a bit of jQuery – it was much neater):

function GetSelectedUsers(attributeName, separator)
{
var result = "";
var users = $("input:checkbox[name^='spUserSelectionCheckBox']");
for (var i = 0; i < users.length; i++)
{
var chkBox = users[i];
if (chkBox.checked)
{
var attributeValue = chkBox.getAttribute(attributeName);
if (attributeValue != null && attributeValue.length > 0)
{
if (result.length > 0)
{
result = result + separator;
}
result += attributeValue;
}
}
}
return result;
}

And this function puts all the selected users into our hidden field:

function BtnRemoveUsersClick()
{
var userIds = GetSelectedUsers("value", ",");
var userNames = GetSelectedUsers("account", ",");
var confirmMessage = "<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,people_confirmremoveusersfromgroup%>' EncodeMethod='EcmaScriptStringLiteralEncode'/>";
var noUserSelectedMsg = "<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,people_nouserselected%>' EncodeMethod='EcmaScriptStringLiteralEncode'/>";
if (userIds.length == 0)
{
alert(noUserSelectedMsg);
return false;
}
var hdn = (document.getElementById("<%= HdnUsersToRemove.ClientID %>"));
hdn.value = userIds;
var msg = StBuildParam(confirmMessage, userNames);
if(confirm(msg))
{
<%= Page.GetPostBackEventReference(BtnRemoveUsersFromGroup) %>;
return true;
}
return false;
}

For completeness, our hidden field is an ASP.NET control…

<input runat="server" type="hidden" id="HdnUsersToRemove" />

Yuck! So, in JavaScript, we get all the checkboxes, identify the selected users, put them into a hidden field, and then postback our button’s click event. Thus, in my code behind, I have to split up my string of IDs of selected users, and remove the users:

web.AllowUnsafeUpdates = true;
string[] usersToRemove = HdnUsersToRemove.Value.Split(new char[] { ',' });
foreach (string user in usersToRemove)
{
group.Users.RemoveByID(int.Parse(user));
}
group.Update();
web.AllowUnsafeUpdates = false;

And that’s the last main bit of it.

  • Introduction
  • How to Display a list of users
  • How to Find the MembershipGroupId
  • How to Add Users
  • How to Remove Users
  • Conclusion
  • Advertisement
    How I simplified the Users and Group UI – Removing Users

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

    This site uses Akismet to reduce spam. Learn how your comment data is processed.