3

How can I get Current Logged in user of SharePoint Site in Provider Hosted App's code behind?
I tried this:

  1. clientContext.Load(clientContext.Web, web => web.CurrentUser.LoginName);
     clientContext.ExecuteQuery();
     var login_nm = clientContext.Web.CurrentUser.LoginName;
    
  2. userName = Request.LogonUserIdentity.Name.ToString();

  3. string u_nm = User.Identity.Name.ToString();

But what I am getting is current System User. Not the SharePoint user. Above all the Options are to get the Windows User Identity. Not for SharePoint site. What should I do to get SharePoint user in my App.

1 Answer 1

4

This should return the current user: (Works for me here)

Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();

clientContext.Load(web.CurrentUser);
clientContext.ExecuteQuery();
currentUser = clientContext.Web.CurrentUser.LoginName;
12
  • Above My code is somewhat same as yours. But did you try to create user in SharePoint. And not in System User. I mean to say I am getting my System users only. Because my app is running on localhost when I debug it. but how to get SharePoint user in that. May 31, 2013 at 14:00
  • I'm not quite understanding your reply question. That is the SharePoint user who I am connected to and using the app as. Are you elevating anything?
    – Hugh Wood
    May 31, 2013 at 14:22
  • I am telling you that when I run my I from VS in FireFox browser I need to Login twice. First is for my SharePoint site and other is for my Localhost. So when App is running I am getting the localhost user that is System user. And when I run on IE it always give me the System User from which I am logged in. Jun 1, 2013 at 4:16
  • Does your app run on Localhost..? Or SharePoint server..? Jun 1, 2013 at 11:44
  • 1
    Ohk. My app is running on LocalHost... I think this will be the reason that I am getting the local users in my app. Jun 3, 2013 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.