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, July 25, 2012

How to fetch the details of the user if he is available within any one of the SharePoint Groups

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="SPUserFromGRoup.VisualWebPart1.VisualWebPart1UserControl" %>
<table cellpadding="0" cellspacing="0">
<tr><td>Enter UserName</td><td><SharePoint:PeopleEditor ID="pplUserName" runat="server" MultiSelect="false" /></td></tr>
<tr><td colspan="2" align="center"><asp:Button ID="btnPeopleSearch" runat="server"
        Text="Search User" onclick="btnPeopleSearch_Click" /></td></tr>
<tr>
<td colspan="2">
<asp:GridView runat="server" ID="dgvSearchUser"></asp:GridView>
</td>
</tr>
<tr><td colspan="2"><asp:label id="lblErrorMessage" runat="server"></asp:label> </td></tr>
</table>


Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace SPUserFromGRoup.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnPeopleSearch_Click(object sender, EventArgs e)
        {
            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><And><Eq><FieldRef Name='Name' /><Value Type='Text'>" + pplUserName.Accounts[0].ToString() +"</Value></Eq><Eq><FieldRef Name='ContentType' /><Value Type='Choice'>Person</Value></Eq></And></Where>";
                    sQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Name' /><FieldRef Name='EMail' /><FieldRef Name='Department' /><FieldRef Name='FirstName' /><FieldRef Name='LastName' />";
                    sQuery.ViewFieldsOnly = true;
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        dgvSearchUser.DataSource = myColl.GetDataTable();
                        dgvSearchUser.DataBind();
                    }
                });
            }
            catch (Exception ex)
            {

                lblErrorMessage.Text = ex.ToString();

            }


        }
    }
}



No comments:

Post a Comment