How I simplified the Users and Group UI – Adding Users

So, in the previous posts we’ve looked at how to resolve a MembershipGroupId for a group (or at least, one way of doing so), and how to list users. Now, how do we add new users to our group?

Well, on our ‘list of users’ page, I have what looks like a a SharePoint toolbar. It’s actually just a table cunningly formatted to look like a toolbar, and the ‘Add users’ link on it is a LinkButton. In the codebehind it’s click event is:

protected void BtnAddUsersToGroup_Click(object sender, EventArgs e)
{
int iGroup = (int)ViewState[GROUP];
SPUtility.Redirect("SimpleUG/AddUsers.aspx?MembershipGroupId=" + iGroup, SPRedirectFlags.RelativeToLayoutsPage, this.Context);
}

So, all we’re doing is going to a page called ‘AddUsers’ and passing the MembershipGroupId again.

This page looks like:

custom-add-page

It’s much simpler than the normal add users page. We’re not giving the administrators as many options, which I think they’ll thank us for.

All we’ve got here is a PeoplePicker control. In the main content area of our page (having stripped out a lot of formatting) we’ve got:

<wssawc:PeopleEditor
AllowEmpty=false
ValidatorEnabled="true"
id="userPicker"
runat="server"
ShowCreateButtonInActiveDirectoryAccountCreationMode=true
SelectionSet="User,SecGroup,SPGroup"
/>
<asp:Button UseSubmitBehavior="false" runat="server" OnClick="BtnOK_Click" Text="<%$Resources:wss,multipages_okbutton_text%>" id="btnOK" accesskey="<%$Resources:wss,okbutton_accesskey%>"/>

So, a lot of formatting, a PeopleEditor control, and a button to click OK. The main bit of the click event handler for the button is:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web2 = site.OpenWeb())
{
web2.AllowUnsafeUpdates = true;
for (int i = 0; i < pickedList.Count; i++)
{
PickerEntity current = (PickerEntity)pickedList[i];
SPUser user = web2.EnsureUser(current.Key);
group.AddUser(user);
}
web2.AllowUnsafeUpdates = false;
}
}
});

Here, for the group we’re adding to, we get each picked SPUser for the PeoplePicker, and add them to the group. Then I return the user to the list of users page.

  • Introduction
  • How to Display a list of users
  • How to Find the MembershipGroupId
  • How to Add Users
  • How to Remove Users
  • Conclusion
  • Advertisements
    How I simplified the Users and Group UI – Adding 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 )

    Twitter picture

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

    Facebook photo

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

    Google+ photo

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

    Connecting to %s