Tuesday, May 27, 2008

How to get logged in User in Web Part?

Hi All,

May times we need to know the name of the user who is logged in currently in your site in web part code.

Here is a simple way you can accomplish this :

WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
string strLoggedInUser = p.Identity.Name;

Use WindowPrincipal class to get the Name. This will return you Domain_Name/UserName combination.



Hi. I hit some error.

An error occurred during the compilation of the requested file, or one of its dependencies. The type or namespace name 'WindowsPrincipal' could not be found (are you missing a using directive or an assembly reference?)

I used Microsoft Office Sharepoint Designer.

What is the above error?

Parth Patel said...

add referance of
System.Security or System.Security.Principal

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com