1

I have the following Code in a provider hosted add-in:

using (var userContext = spContext.CreateUserClientContextForSPHost())
{             
  if (userContext != null)
  {
    user = userContext.Web.CurrentUser;
    userContext.Load(user, cu => cu.LoginName);
    userContext.ExecuteQuery();
  }                                                         
}

This code (using the Tokenhelper) works fine for most of the employees inside our company. So configuration/firewall etc. must be correct.

One single user (we know of) however gets an error on ExecuteQuery():

System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilde sb)

The user can access the site collection which hosts the app without any problem. Is there maybe a special permission required for retrieving your own login name?

He gets this error on different Browsers. What could be so "special" about this user? Any way to get more detailled Error information? We are currently completely lost where to look at.

1

If you need to read the properties of Current User in provider Hosted Add-in, you should use following code:

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                // Get the people manager instance and load current properties.
                PeopleManager peopleManager = new PeopleManager(clientContext);
                PersonProperties personProperties = peopleManager.GetMyProperties();
                clientContext.Load(personProperties);
                clientContext.ExecuteQuery();

                // Output user profile properties to a text box.
                txtProperties.Text = "";
                foreach (var item in personProperties.UserProfileProperties)
                {
                    txtProperties.Text += string.Format("{0} - {1}{2}", item.Key, item.Value, Environment.NewLine);
                }
            }

I just grabbed the code from here. Modify as per your need.

Also you need to set the Permission in AppManifest.xml to run this code successfully. The permission should be like

enter image description here.

Alternate Approach:

Also you can use following code in your add-in to get the current user's properties.

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateAppOnlyClientContextForSPHost())
{
    Web web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();
    clientContext.Load(web.CurrentUser);
    clientContext.ExecuteQuery();
    var currentUser = clientContext.Web.CurrentUser.LoginName;
}

Update:

Just update the permission to your AppManifest.xml as shown in image and the error will be resolved.

enter image description here

  • I don't need any additional Information, only the LoginName (which works usually fine with CurrentUser.Loginname – Ole Albers Oct 11 '17 at 12:46
  • 1
    Your updated answer is mainly the same code I already posted. Just with more ExecuteQuery's :) I apprechiate your help but this does not solve my problem receiving a 401 error – Ole Albers Oct 11 '17 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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