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.

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Rahul Gokani
  • 1,074
  • 2
  • 21
  • 50

1 Answers1

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;
Hugh Wood
  • 6,285
  • 1
  • 26
  • 45
  • 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. – Rahul Gokani May 31 '13 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 '13 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. – Rahul Gokani Jun 01 '13 at 04:16
  • Does your app run on Localhost..? Or SharePoint server..? – Rahul Gokani Jun 01 '13 at 11:44
  • It runs on a separate app server – Hugh Wood Jun 03 '13 at 08:27
  • 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. – Rahul Gokani Jun 03 '13 at 08:38
  • Do you happen to have the full code above? Thanks. I have a line like this: var c = new ClientContext(_hostWebUrl); where _hostWebUrl is my SP site. I am receiving the following error: ServerVersion = 'c.ServerVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException' – SearchForKnowledge Mar 26 '15 at 13:56
  • If you want other properties, you need to load them in the load method. Only information you ask for will be retrieved. – Hugh Wood Apr 08 '15 at 16:14
  • Is it possible to do exactly the same thing but in the javascript part ? Or i'll have to get it via .NET C# server-side and serialize the value into javascript. – Alex Jun 01 '15 at 13:42
  • Hi @Alex please refer to this http://sharepoint.stackexchange.com/questions/73032/get-current-user-in-client-object-model-with-javascript it is indeed similar but not the same – Hugh Wood Jun 08 '15 at 12:08
  • @HughWood thanks for the link! I managed to do it by accessing the SP context via my webpart, by calling the parent global scope : parent._spPageContextInfo.userId. This way I can retrieve all the informations from the current session, into my App, accessed by my webpart . ;) – Alex Jun 08 '15 at 12:33
  • Sometimes this isn't available because it's populated into the page context, so ensure it is tested to this effect and that your code runs late enough in the page cycle that it will work as intended. – Hugh Wood Jun 09 '15 at 12:53