Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Wednesday, February 15, 2012

Getting users from SharePoint groups and binding it to a Simple Asp.Net Table Object

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Drawing.Design;
[assembly:AllowPartiallyTrustedCallers]
namespace AllUsersWebPart
{
    [Guid("31e57d3c-47e9-432e-99ce-ad06626d93e2")]
    public class AllUsersWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        Label lblName;
        Label lblgroups;
        Table tblAllUsers;
        string strError = string.Empty;
        public AllUsersWebPart()
        {
        }

        protected override void CreateChildControls()
        {
            try
            {
                tblAllUsers = new Table();
                //tblAllUsers.Width = 100;
                //tblAllUsers.Height=100;
               // tblAllUsers.CellPadding=0;
                //tblAllUsers.CellSpacing=0;
                lblgroups = new Label();
                lblName = new Label();

                CreateHeaderRow(); // Will Add a header Row to the Table.

                using (SPSite SPSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb 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);
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
        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);
        }
        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);
        }
    }
}

No comments:

Post a Comment