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.

Sunday, March 18, 2012

Binding users from SharePoint Groups within a site to GridView without a single line of loop,

  
Note: - User Information List is the list which carries all the users in the SharePoint site. So I used this list and binded all the users to the GridView. SharePoint 2007 will not have ViewFieldsOnly Attribute for SpQuery Object.

ascx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="UserInfoListWebPart.VisualWebPart1.VisualWebPart1UserControl" %>
<asp:GridView ID="dgvUsers" runat="server"></asp:GridView>
<asp:Label ID="lblErrorMessage" runat="server"></asp:Label>

                                                                        Ascx.cs

try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["User Information List"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>Person</Value></Eq></Where>";
                    sQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Name' /><FieldRef Name='EMail' /><FieldRef Name='Department' /><FieldRef Name='FirstName' /><FieldRef Name='LastName' />";
                    sQuery.ViewFieldsOnly = true;
                    sQuery.ViewAttributes = "Scope='Recursive'";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        dgvUsers.DataSource = myColl.GetDataTable();
                        dgvUsers.DataBind();
                    }
                });
            }
            catch (Exception ex)
            {

                lblErrorMessage.Text = ex.ToString();

            }



No comments:

Post a Comment