.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
Sambanthamoorthy

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Display SharePoint Users, Groups and Sites in a GridView and Use a DropDownList to filter the values

Posted By:Ravishankar       Posted Date: April 16, 2011    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

This article will explain how to display SharePoint Users, Groups and Sites in a GridView and use a DropDownList to filter the values.
 

This article will explain how to display SharePoint Users, Groups and Sites in a Gridview and use a DropDownList to filter the values.

Step 1: Open the Visual Studio and create a new project; select visual webpart (I am giving an example using a visual webpart).

Step 2: Add the GridView to your user control, e.g.


    
    
      
                    
                        
                      
       
        
        
        
       
       
    
    
    
    
    
    


Step 3: Code behind for the GridView will look like below:

protected void Page_Load(object sender, EventArgs e)
 {

            try
            {
                DataTable dtLists = new DataTable();
                dtLists.Columns.Add("Country");
                dtLists.Columns.Add("UserName");
                dtLists.Columns.Add("Role");

                //Get all users by site position and group.
                SPSite spSite = new SPSite(SPContext.Current.Site.Url);
                foreach (SPWeb web = spSite.AllWebs)
                {
                        foreach (SPGroup group in web.Groups;)
                        {
                            foreach (SPUser user in group.Users)
                            {
 
                                DataRow dr = dtLists.NewRow();
                                dr["Country"] = web.Title;
                                dr["Role"] = group.Name;
                                dr["User"] = user.LoginName;
                                dtLists.Rows.Add(dr);
 
                            }
                        }
 
                    }
 
                }
                gvLists.DataSource = dtLists;
                gvLists.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }

}


Step 4: Output

Share1.gif

Step 5: Next, add a Dropdown to filter data just above the GridView like this:

User :
Country :
Role :


Step 6: Add the code behind for filters:

//call this method in page load to load the value
private void bindfilters()
        {
            //for country
            try
            {
                filterCountry.Items.Clear();
                filterUser.Items.Clear();
                SPWeb oWeb = SPControl.GetContextWeb(Context);
                oWeb.AllowUnsafeUpdates = true;
                filterCountry.Items.Insert(0, new ListItem("--All Country--", "0"));
                filterUser.Items.Insert(0, new ListItem("--All users--", "0"));
                foreach (SPUser user in oWeb.AllUsers)
                {
                    filterUser.Items.Add(user.LoginName);
                }
                SPSite site = SPContext.Current.Site;
                SPList listname = site.RootWeb.Lists[countrySiteListName];
                foreach (SPListItem item in listname.Items)
                {
                    filterCountry.Items.Add(item[countrySiteCoulumnName].ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex
            }
        }
//Action on dropdowns
        /// 
        /// Filtering the gridview data based on Users
        /// 
        /// 
        /// 
        protected void filterUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                filterCountry.ClearSelection();//.Items.Clear();
                //filterUser.Items.Clear();
                filterRole.ClearSelection();
                if (filterUser.SelectedItem.Text != "--All users--")
                {
                    DataTable dtLists = new DataTable();
                    dtLists.Columns.Add(countrySiteCoulumnName);
                    dtLists.Columns.Add("UserName");
                    dtLists.Columns.Add("Role");

                    //Get all users by site position and group.
                    SPSite site = SPContext.Current.Site;
                    SPList listname = site.RootWeb.Lists[countrySiteListName];
 

foreach (SPWeb web in SPContext.Current.Site.AllWebs)                    {

                            SPGroupCollection groupcoll = web.Groups;
                            foreach (SPGroup group in groupcoll)
                            {
                                    SPUser user = group.Users[filterUser.SelectedItem.Text];
                                    DataRow dr = dtLists.NewRow();
                                    dr["Country"] = web.Title;
                                    dr["Role"] = group.Name;                                    dr["UserName "] = user.LoginName;
                                    dtLists.Rows.Add(dr);

                       }

                     }
                    gvLists.DataSource = dtLists;
                    gvLists.DataBind();
                }
                else
                {
                    GetUsers();
                }
            }
            catch (Exception ex)
            {
            }
        }

        /// 
        /// Filtering the gridview data based on Country
        /// 
        /// 
        /// 
        protected void filterCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                filterUser.ClearSelection();
                filterRole.ClearSelection();

                if (filterCountry.SelectedItem.Text != "All")
                {

                    DataTable dtLists = new DataTable();
                    dtLists.Columns.Add("Country");
                    dtLists.Columns.Add("UserName");
                    dtLists.Columns.Add("Role");

                    SPSite spSite = new SPSite(SPContext.Current.Site.Url);
                    using (SPWeb web = spSite.AllWebs[filterCountry.SelectedItem.Text])
                    {
                        SPGroupCollection groupcoll = web.Groups;
                      
                        foreach (SPGroup group in groupcoll)
                        {
                            // filterRole.Items.Add(group.Name);
                            foreach (SPUser user in group.Users)
                            {
                                DataRow dr = dtLists.NewRow();
                                dr["Country"] = web.Title;
                                dr["Role"] = group.Name; //role.Name;
                                dr["UserName"] = user.LoginName;
                                dtLists.Rows.Add(dr);
                            }
                        }
                    }
                    gvLists.DataSource = dtLists;
                    gvLists.DataBind();
                }
                else
                {
                    GetUsers();
                }
            }
            catch (Exception ex)
            {
                            }
        }

        /// 
        /// Filtering the gridview data based on Role
        /// 
        /// 
        /// 
        protected void filterRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                filterCountry.ClearSelection();//.Items.Clear();
                filterUser.ClearSelection();
                //filterRole.ClearSelection();
                if (filterRole.SelectedItem.Value != "All")
                {
                    DataTable dtLists = new DataTable();
                    dtLists.Columns.Add("Country");
                    dtLists.Columns.Add("UserName");
                    dtLists.Columns.Add("Role");
                    SPSite spSite = new SPSite(SPContext.Current.Site.Url);
                    foreach (SPWeb web = spSite.AllWebs)
                    {
                         SPGroup group = web.Groups[filterRole.SelectedItem.Text];
                         foreach (SPUser user in group.Users)
                                {
                                    DataRow dr = dtLists.NewRow();
                                    dr["Country"] = web.Title;
                                    dr["Role"] = group.Name; //role.Name;
                                    dr["UserName"] = user.LoginName;
                                    dtLists.Rows.Add(dr);
                                }

                    }
                    gvLists.DataSource = dtLists;
                    gvLists.DataBind();
                }
                else
                {
                     //method will load gridview complete data.
                    GetUsers();
                }
            }
            catch (Exception ex)
            {
                           }
        } 

Step 7: Output

Share2.gif

Hope you like this article....


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend