In this article we explore the Server Object Model for dealing with Groups and Users inside SharePoint.
Our Intent
We intend to create a webpart that displays the users & groups in the SharePoint site.
For example:
Group 1
- User 1
- User 2
Group 2
- User 3
- User 4
The Solution
Create a new SharePoint solution and add a new webpart into it. Place a literal control over it, as in:
In the Page Load event add the following code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
RefreshData();
}
private void RefreshData()
{
StringBuilder sb = new StringBuilder("<h2>Groups & Users</h2></br>");
foreach (SPGroup group in SPContext.Current.Web.Groups)
{
sb.Append("<b>Group: </b>" + group.Name + "</br>");
foreach (SPUser user in group.Users)
{
sb.Append(user.Name + "</br>");
}
sb.Append("</br>");
}
Literal1.Text = sb.ToString();
}
What does the code do?
SPContext.Current.Web.Groups returns all the groups for the particular web object. We can enumerate this using a foreach statement.
Group.Users returns the users inside the particular group. The object is represented by the SPUser object model.
Running the Code
On running the project, adding the web part to the page, you will see the following results:
More Information on Groups & Users
I would like to add some related information from MSDN.
SPWeb.AllUsers
Gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site.
SPWeb.Users
Gets the collection of user objects that are explicitly assigned permissions in the Web site.
References
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser.groups.aspx
Summary
In this article we have explored the server object model for fetching groups & users. The web part displaying groups & users is attached with the article.

karthick rPosted Dec 7, 2017, 4:46 AM
Hi in my sharepoint 2013, i have 50 groups created but "SPContext.Current.Web.Groups " gives me only 27 groupnames only. can you help on this ?
Lajapathy ArunPosted Sep 3, 2012, 3:48 PM
Yes, looks pretty nice to read sharepoint :)
Jean PaulPosted Aug 23, 2012, 9:24 AM
I agree to Dinesh too.. Good idea Lajapathy.. I wanted to add that SharePoint has recorded growth in even the recession too.. More Java Documentum, other f/w got converted to SP. Plus there are 10x more aspects in SP compared to ASP.NET or other tehcnologies. So people loving learning knowledge this is an endless stream and we can enjoy everyday.
Dinesh BeniwalPosted Aug 23, 2012, 7:20 AM
That will be great Lajapathy.
Lajapathy AruneditedPosted Aug 23, 2012, 2:27 AMEdited Aug 23, 2012, 2:27 AM
Nice one :) going to start on Sharepoint.
Jean PauleditedPosted Aug 22, 2012, 10:36 PMEdited Aug 22, 2012, 10:36 PM
Hello Yazzy,I can provide the answer based on my understanding of your question. SharePoint 2010 is having its own content database in SQL Server 2008 R2. But it is not recommended way to delete data from the database.The recommend way is to use: 1) Server object model (which requires SharePoint binaries on the execution machineSPList list;more: http://jeanpaulva.com/2012/05/30/sharepoint-2010-sharepoint-object-model/2) Client object model (which do not requires SharePoint binaries on the execution machine. ClientContext context = new ClientContext(ServerText.Text); List list = context.Web.Lists.GetByTitle("Tasks");ListItemCollection listItems = list.GetItems(new CamlQuery() { ViewXml = "<View/>" }); context.Load(listItems); context.ExecuteQuery();listItems[listItems.Count - 1].DeleteObject(); context.ExecuteQuery();more: http://www.codeproject.com/Articles/399156/SharePoint-2010-Client-Object-Model-IntroductionYou can use either of the above to achieve the same.
yazzy yazzyPosted Aug 22, 2012, 10:10 PM
Hi, Please help. i have this project. the user is using Sharepoint 2010 web application (front end programming uses asp.net)to search data. The user can delete data in Sharepoint web application. However, the data can be deleted in the sharepoint database but it can never be deleted in the SQL server 2008 which is connected to the sharepoint. Is there any sharepoint built-in database in 2010? Do I use a list function? Please help with the coding.