All users in SharePoint site custom webpartby tdomf_6998b, on October 18th, 2010
Price (USD)0.0
Category: For e.g. webpart, workflow, event aggreger etc.
This webpart will display all the users from each group in the current site. you can however add a foreach loop for each web (or site) in the site collection to get all the users in a site collection.
Our clients is my site name, so the groups are named as "our clients visitors", "Our clients members" and "our clients owners ".
Steps:
1. Create a New webpart Project (side note: I am using vsewss webpart template)
2. Add the below code in webpart. CS file. Dont forget to rename all the webpart files before you deploy.
Code:
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. serialization;
Using system. Web. UI. webcontrols;
Using Microsoft. SharePoint;
Using system. collections;
Using Microsoft. Sharepoint. webcontrols;
Using Microsoft. Sharepoint. webpartpages;
Namespace alluserswebpart
{
[GUID ("bb4dddf0-9923-43b9-a835-210735416ddf")]
Public class allusers: system. Web. UI. webcontrols. webparts. webpart
{
Label lblname;
Label lblgroups;
Table tblallusers;
Public allusers ()
{}
Protected override void createchildcontrols ()
{
Base. createchildcontrols ();
Tblallusers = new table ();
Lblgroups = new label ();
Lblname = new label ();
Createheaderrow (); // will add a header row to the table.
Using (spsite = new spsite (spcontext. Current. Site. ID ))
{
Using (spweb = spsite. openweb (spcontext. Current. Web. ID ))
{
Spusercollection allspwebusers = spcontext. Current. Web. allusers;
Spgroupcollection allspwebgroups = spcontext. Current. Web. Groups;
// Iterate through each group in the current site.
Foreach (spgroup GRP in allspwebgroups)
{
Spusercollection usersingroup = GRP. users;
Foreach (spuser user in usersingroup)
{
Lblname. Text = user. Name;
Foreach (spgroup usergrp in user. Groups)
{
Lblgroups. Text = usergrp. Name;
}
Addtotable (lblname. Text, lblgroups. Text );
}
}}}
This. Controls. Add (tblallusers );
}
// Adding users to the output table.
Protected void addtotable (string username, string GRP)
{
Tablerow r = new tablerow ();
Tablecell cellname = new tablecell ();
Cellname. Text = username;
R. cells. Add (cellname );
Tablecell cellpermissions = new tablecell ();
Cellpermissions. Text = GRP;
R. cells. Add (cellpermissions );
Tblallusers. Rows. Add (R );
}
// Create a header row for the output table.
Protected void createheaderrow ()
{
Tableheaderrow headerrow = new tableheaderrow ();
Headerrow. backcolor = system. Drawing. color. lightblue;
Tableheadercell headertablecell1 = new tableheadercell ();
Tableheadercell headertablecell2 = new tableheadercell ();
Headertablecell1.text = "User Name ";
Headertablecell1.scope = tableheaderscope. column;
Headertablecell2.text = "group ";
Headertablecell2.scope = tableheaderscope. column;
Headerrow. cells. Add (headertablecell1 );
Headerrow. cells. Add (headertablecell2 );
Tblallusers. Rows. addat (0, headerrow );
}
}}
See the attached screenshot
Alluserswebpart.jpg (8 KB)