2

What I want

Simply - the user's profile information to obtain the mail adress.

What I have

A SP.FieldUserValue object filled with the user which mail I want to obtain. enter image description here

Approach

MailContact: function () {
    /// <summary>
    /// Opens userspecific mailservice with a predefined mail and target
    /// </summary>
    SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
        // Make sure PeopleManager is available 
        SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function () {
            var context = SP.ClientContext.get_current();
            var peopleManager = new SP.UserProfiles.PeopleManager(context);

            var PageUserProperties = peopleManager.getPropertiesFor(INSERTIDENTIFIERHERE);
            context.load(PageUserProperties);

            context.executeQueryAsync(
                function () {
                    PageUserProperties;
                    debugger;
                },
                function (sender, args) {
                    //todo errorhandling
                    console.log('Error while receiving the properties from the UserProfile');
                    console.log(args);
                }
            );
        });
    });        
}

Problem

The information delivered by the SP.FieldUserValue is missing the domain/username data and it seems like .getPropertiesFor() function needs this information.

Isn't there a way to obtain a userprofile by a users id (the id is contained in the SP.FieldUserValue object)?

Here is a wrapper used that accesses a SOAP service in 2007 and returns a user by ID. Do I really have to use this?

Solution

Here's the answer

MailContact: function () {
    /// <summary>
    /// Opens userspecific mailservice with a predefined mail and target
    /// </summary>
    var context = SP.ClientContext.get_current();

    var user = context.get_web().getUserById(this.Variables.Contact.get_lookupId());

    context.load(user);

    context.executeQueryAsync(
        function () {
            console.log(user.get_email());
            debugger;
        },
        function (sender, args) {
            //todo errorhandling
            console.log('Error while receiving the properties from the UserProfile');
            console.log(args);
        }
    );      
}
  • Since its 2013 why can't you use REST API? – Amal Hashim Nov 27 '14 at 12:53
  • The REST api would be another possible solution. It's just sad, that the SP.UserProfiles.PeopleManager cannot deliver a userprofile by id.. – Mx. Nov 27 '14 at 13:40
  • The user id is specific to a site collection. User (login) name is unique. As the User Profile Service is an application service for a whole farm (with several web applications, each potentially with a lot of site collections), or even for other farms (in case it is shared across farms), it would be only possible to provide a method to lookup user profiles by id, if it would have a parameter of type site id, for example. – pholpar Nov 27 '14 at 13:55
1

You should first "convert" the SP.FieldUserValue (let's call it fuv in code) to an SP.User object, then have you already the login name you need:

var userId = fuv.get_lookupId();
var user = context.get_web().getUserById(userId);

context.Load(user);
context.executeQueryAsync(onRequestSuccess, onRequestFail);

function onRequestSuccess() 
{       
  var loginName = user.get_loginName();    }

function onRequestFail(sender, args) 
{
  alert("error msg");
}
-1

You could retrieve the user's email address using SP.UserProfiles.PersonProperties.email property:

var context = SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(context);

var properties = peopleManager.getPropertiesFor(accountName);
context.load(properties);

context.executeQueryAsync(
  function () {
     console.log(properties.get_email());
  },
  function (sender, args) {
     //Error handling goes here...
  }
);
  • accessing the properties is not the problem. getting the userprofile via a SP.FieldUserValue is because it only contains the display name and the id and not a domain/username information. – Mx. Nov 27 '14 at 13:33

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.