Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

SharePoint - Get All Group and Group Users Programmatically in SharePoint

Article:
Viewed:  8106 
Posted On:  26/11/2013 07:35:59 
How we can Get All SharePoint Group and Group Users programmatically in SharePoint 2013 ? 

In this article I am going to show how we can get all SharePoint site group programmatically.  How we can get all user of a particular SharePoint group.

Step 1: Open Visual Studio 2012 -> Select New Project -> Select SharePoint 2013 Empty Project.

SP_Group_GroupUsers.png

Image 1.

Step 2: Give Your Site URL

SP_Group_GroupUsers 2.png

Image 2.

Step 3: Now Right  Click in Solution Explorer And Select Add New Item -> Select Application Page and Name as GetGroupAndGroupUsers.aspx

In this page I am going to  Bind All Group in a drop down list and on selecting a particular group I am binding all user in another drop down list.

Below is my aspx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetGroupAndGroupUsers.aspx.cs" Inherits="SP_Operation.Layouts.SP_Operation.GetGroupAndGroupUsers" DynamicMasterPageFile="~masterurl/default.master" %>

 <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content> 

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <table cellpadding="5" cellspacing="5" width="100%" align="left">
        <tr>
            <td width="20%">All Groups:</td>
            <td>
                <asp:DropDownList ID="ddlGroup" runat="server" OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>My Selected Group: </td>
            <td>
                <asp:Label ID="lblSelectedGroup" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>Users In Selected Group :</td>
            <td>
                <asp:DropDownList ID="ddlGroupUser" runat="server" OnSelectedIndexChanged="ddlGroupUser_SelectedIndexChanged" AutoPostBack="true">
</
asp:DropDownList>
            </td>
        </tr>
 
          <tr>
            <td>My Selected User: </td>
            <td>
                <asp:Label ID="lblUser" runat="server"></asp:Label>
            </td>
        </tr>
       
    </table>
</asp:Content> 

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    SP Group & Users
</asp:Content> 

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    Get All Groups and Their Users
</asp:Content> 

Now GetGroupAndGroupUsers.aspx.cs

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls; 

namespace SP_Operation.Layouts.SP_Operation
{
    public partial class GetGroupAndGroupUsers : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetAllSPGroup();
                GetAllGroupUsers(ddlGroup.SelectedItem.Text.ToString());
                lblSelectedGroup.Text = ddlGroup.SelectedItem.Text.ToString();
                lblUser.Text = ddlGroupUser.SelectedItem.Text.ToString();
            }
        }
 
        private void GetAllSPGroup()
        {
            using (SPSite spSite = new SPSite("http://localhost:2000"))
            {
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    foreach (SPGroup spGroup in spWeb.Groups)
                    {
                        ddlGroup.Items.Add(spGroup.Name);
                    }
                }
            }
        }
 
        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblSelectedGroup.Text = ddlGroup.SelectedItem.Text.ToString();
            GetAllGroupUsers(ddlGroup.SelectedItem.Text.ToString());
        }
 
        private void GetAllGroupUsers(string GroupName)
        {
            ddlGroupUser.Items.Clear();
            using (SPSite spSite = new SPSite("http://localhost:2000"))
            {
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    SPGroup spGroup = spWeb.Groups.GetByName(GroupName);
                    foreach (SPUser spUser in spGroup.Users)
                    {
                        ddlGroupUser.Items.Add(spUser.Name);
                    }
                }
            }
        }
 
        protected void ddlGroupUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblUser.Text = ddlGroupUser.SelectedItem.Text.ToString();
        }
    }
}
 

Now deploy you solution and access your page

http://localhost:2000/_layouts/15/SP_Operation/GetGroupAndGroupUsers.aspx

SP_Group_GroupUsers 3.png

Image 3.

Here you can see All Group in a Drop Down List . . . 

SP_Group_GroupUsers 4.png

Image 4.

You can see All users in a DropDownList . . .  :)

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us