Generate author link RRS feed

  • Question

  • Hi!

    After I retrieve list values I can get Author with

    var createdBy = item.get_item("Author");
    This will give me user first and last name:

    Now I would need to generate url to that user profile page. How can I do it? How can I get needed info to generate link like this:


    Thank you!

    Monday, June 3, 2013 1:20 PM


All replies

  • You need to start with using the PeopleManager.getPropertiesFor.  Here is an MSDN walkthrough on using that method: (http://msdn.microsoft.com/en-us/library/jj920104.aspx#bk_examplePersonPropsObj)

    The relevant code is:

    function getUserProperties() {
        // Replace the placeholder value with the target user's credentials.
        var targetUser = "domainName\\userName";
        // Get the current client context and PeopleManager instance.
        var clientContext = new SP.ClientContext.get_current();
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
        // Get user properties for the target user.
        // To get the PersonProperties object for the current user, use the
        // getMyProperties method.
        personProperties = peopleManager.getPropertiesFor(targetUser);
        // Load the PersonProperties object and send the request.
        clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
    Here is the MSDN for peoplemanager.getpropertiesfor: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.userprofiles.peoplemanager.getpropertiesfor.aspx

    From there, you will have a PersonProperties class: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.userprofiles.personproperties.aspx

    On that object there is a property called UserUrl, which MSDN states "Gets the URL of the user's profile page.": http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.personproperties.userurl.aspx

    Hope that helps!

    Brandon Atkinson
    Blog: http://brandonatkinson.blogspot.com

    • Proposed as answer by Sohel Rana Tuesday, June 4, 2013 12:46 AM
    • Marked as answer by Zorčec Tuesday, June 4, 2013 7:24 AM
    Monday, June 3, 2013 2:49 PM
  • Thank you, that is example I was looking for to get started.
    But there are still some problems that are bothering me.

    I'm getting error:
    Object reference not set to an instance of an object on server. The object is associated with method GetPropertiesFor.

    I also have a bit different situation then it's in that example, and this code doesn't fit for me and I'm not sure what are my options. I'm populating my list with mixed data including user first and last name that should be link to his profile page. First thing that bothers me is that I need to execute request for each row I have.

    So I would like to know if there is another way I could retrieve all user info that I need in one call?

    Another thing is that in my case, this code won't work because "personProperties" value will get rewritten with every call, so I won't be able to retrieve right info.

    Is there a way to call delegate methods (onRequestSuccess, onRequestFail) with argument that contain personProperties object?
    Also for future use I would like to know if there is a way to send synchronous query's or there are only async?

    • Edited by Zorčec Tuesday, June 4, 2013 8:50 AM
    Tuesday, June 4, 2013 8:39 AM
  • I found much simpler solution to get profile link.

    var createdBy = item.get_item("Author");
    var linkToProfile = '../_layouts/15/userdisp.aspx?ID=' + createdBy.get_lookupId();

    • Edited by Zorčec Thursday, June 6, 2013 12:58 PM
    Thursday, June 6, 2013 12:58 PM