Thursday, July 10, 2008

Get Current User Email, Login, Display Name Details

I hit upon a wonderful blog while working on a requirement on getting the current user's Email id in efficient way.
The normal code which microsoft given fails:
SPWeb site = SPContext.Current.Web;
SPUser user = site.CurrentUser;
string DisplayName = user.Name;
string Login = user.LoginName;
string EMail = user.Email;
string User Notes = user.Notes;
Because most of the users who don't have access to All sites won't give details of their email by the code given. If you are wondering what is all about the difference between All users see below:
SPWeb site = SPContext.Current.Web;
SPUserCollection c1 = site.Users;
SPUserCollection c2 = site.AllUsers;
SPUserCollection c3 = site.SiteUsers;

The code gives 3 types of different user collection so i guess the user who logged in and didn't find his email by the code above belongs to one of this group.
The difference between these SPUserCollection is copied from MSDN.:

The Users collection has the smallest membership of these three collections. This collection includes all the external principals that have been explicitly assigned permissions within the current site.
The AllUsers collection includes all members of the Users collection, plus external users that have accessed objects within the site using implicit permissions through group or role membership. For example, imagine a user named Brian with the login of LITWAREINC\BrianC that has never been given explicit permissions to access a site and view a particular list. However, he might still be able to view the list because of his membership within an Active Directory group that has been configured with list view permissions. When Brian first accesses the site or one of its objects (say, a list using implicit permissions), he is added as a member of the AllUsers collection, but he is not added as a member of the Users collection.
The SiteUsers collection is an aggregation that combines membership for each AllUsers collection within the current site collection. The membership of this collection includes all external principals that have been assigned permissions to any object within the site collection as well as all external users that have been granted access to any of the site collection's objects using implicit permissions.

I used a basic way to get the current user using (context of control HTTPCONTEXT) Context.User.Identity.Name or Page.User.Identity.Name which does the same httpcontext.

After we get the current user login i can pass it to the magic of another Class in sharepoint object Model which brings the user details.

using Microsoft.SharePoint.Utilities;

SPWeb osite = SPContext.Current.Web;

SPPrincipalInfo prin = SPUtility.ResolvePrincipal(osite,
Context.User.Identity.Name , SPPrincipalType.All, SPPrincipalSource.All, osite.AllUsers, false);
writer.Write(prin.Email);


using this method u can also search the user by their Email ID, or their Display name. its a cool method who does the search on multiple fields.

2 comments:

Karthi said...

I am using SPPrinciplInfo code in my Custom HTTP handler but, I am facing error in the screen like

System.Unauthorized Access Exception:Attemted to perform an unauthorized operation

Pls help me

Karthi said...
This comment has been removed by the author.