0

I'm using a Remote Event Receiver to send an email to the manager of the user when an item is added.

I tried to use the peoplemanager class using this sample code:

public static void GetCurrentUserProfileProperties() 
{ 
    string siteUrl = "https://spotenant-admin.sharepoint.com"; 

    // Connect to the sharepoint site client context. 
    ClientContext clientContext = new ClientContext(siteUrl); 
    //clientContext.Credentials = credentials

    // Get the PeopleManager object and then get the current user's properties. 
    PeopleManager peopleManager = new PeopleManager(clientContext); 
    PersonProperties myProperties = peopleManager.GetMyProperties(); 

    // This request load the AccountName and UserProfileProperties 
    clientContext.Load(myProperties, p => p.AccountName, p => p.UserProfileProperties); 
    clientContext.ExecuteQuery(); 

    foreach (var property in myProperties.UserProfileProperties) 
    { 
        Console.WriteLine(string.Format("{0}: {1}", 
        property.Key.ToString(), property.Value.ToString())); 
    } 
}

When I try to run it I get an error Forbidden. I also tried to use the client context of

clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties) 

but I got an error regarding permissions. Keep in mind that I am a global admin. Also I would like this app to be able and run from other users with limited permissions in order to send the email to their manager.

Any ideas how to do it and why there is an error?

0

The SharePoint add-ins will run with App Permissions instead of with user permissions. So, you need to give your app enough permissions to read the user profiles.

Can you verify your solution APP Manifest file you have the following setting added?

enter image description here

  • Thanks it works now. I can get the key value of the Manager property by looping the properties. Is there a way to get the specific property without looping? I want to get the email of the manager of the current / or targetted user. Any ideas how to implement this? – shak Jun 26 '17 at 12:04
  • There is no other way. You can create a reusable function which finds the value of property but this reusable function will loop through all properties to find the given property – Venkat Konjeti Jun 26 '17 at 13:34

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.