How to find the logged in user in Sharepoint?
Asked Answered
A

6

14

I have developed a "web part" that has to be deployed on a Sharepoint server. I need the username of the user, who has logged in the sharepoint server within the web part.

How do I get that username?

Azal answered 11/3, 2010 at 7:31 Comment(0)
A
5

Hey all, i got the answer for my question Hope this will work for you all... First add a reference to the MicrosoftSharepoint.dll file in your web part. then write using Microsoft.SharePoint;

            string username;
            string sspURL = "URL where Sharepoint is deployed";

            SPSite site = new SPSite(sspURL);

            SPWeb web = site.OpenWeb();

            SPUser user = web.CurrentUser;

            username = user.LoginName;

            site.AllowUnsafeUpdates = true;

Yours, Jigar <3

Azal answered 12/3, 2010 at 12:0 Comment(0)
S
29

Following worked for me:

SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
string strUserName = theUser.LoginName;

and check this out.

Silkweed answered 11/3, 2010 at 9:46 Comment(1)
Or much better: string strUserName = SPContext.Current.Web.CurrentUser.LoginName;Gnathic
C
13

You can use:

SPWeb web = SPControl.GetContextWeb(this.Context);
string userName = web.CurrentUser.LoginName;

or

string userName = this.Context.User.Identity.Name;

And you should check this.Context.User.Identity.IsAuthenticated as well to ensure there is a user logged in before trying to extract the username.

Creeps answered 11/3, 2010 at 10:40 Comment(5)
I prefer the second option. Catch for me was with internal mapping to AD, it has a bunch of garbage preceeders like 0#.w|domain\username. But otherwise the 2nd implementation worked great for me.Valentinvalentina
@GoldBishop, you can get the username from the claims representation with the following code: SPClaimProviderManager mgr = SPClaimProviderManager.Local; if (mgr != null) { userName = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName).Value; }Creeps
Interesting, ill run it through some tests and see how it interprets. But otherwise i just did userid = this.Context.User.Identity.Name; userid = userid.Substring( userid.IndexOf( '|' ) + 1 ); and was able to seperate the garbage from what i needed for the information.Valentinvalentina
Which works, but it's not garbage. It's a claims identifier and Welcome to the new world of identities :)Creeps
depends on how you see it, for what i need the information for its garbage. i realize the information is descriptive but it is overhead text i dont need ;)Valentinvalentina
A
5

Hey all, i got the answer for my question Hope this will work for you all... First add a reference to the MicrosoftSharepoint.dll file in your web part. then write using Microsoft.SharePoint;

            string username;
            string sspURL = "URL where Sharepoint is deployed";

            SPSite site = new SPSite(sspURL);

            SPWeb web = site.OpenWeb();

            SPUser user = web.CurrentUser;

            username = user.LoginName;

            site.AllowUnsafeUpdates = true;

Yours, Jigar <3

Azal answered 12/3, 2010 at 12:0 Comment(0)
S
3

SPContext.Current.Web.CurrentUser

Septuor answered 30/7, 2013 at 8:28 Comment(0)
P
1

//don't forget to add System.DirectoryServices.AccountManagement as reference and using System.DirectoryServices.AccountManagement;

    PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "MyDomain","DC=MyDomain,DC=com");
    UserPrincipal insUserPrincipal = new UserPrincipal(insPrincipalContext);
    insUserPrincipal.Name = "*";
    PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher();
    insPrincipalSearcher.QueryFilter = insUserPrincipal;
    PrincipalSearchResult<Principal> results = insPrincipalSearcher.FindAll();
    foreach (Principal p in results)
    {
        Console.WriteLine(p.Name);
    }
Pestilent answered 3/8, 2010 at 5:5 Comment(0)
A
1

You can also get current logged user ID by _spPageContextInfo property.

 _spPageContextInfo.userId

You will get current user's ID by _spPageContextInfo. Try this may help you.

Astigmatism answered 8/7, 2015 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.