vendredi 12 décembre 2014

get current user info using jquery ,REST and com



My goal is to get the login name and picture for the current user. I am using SharePoint 2013 Foundation. I tried three method and non of then worked correctly:




  1. This is with jQuery SPServices:



    function getCurrentUserInfo() {
    var thisUsersValues = $().SPServices.SPGetCurrentUser({
    fieldNames: ["ID", "Name", "Picture"],
    debug: false
    });

    var name = thisUsersValues["Name"];

    var ID = thisUsersValues["ID"];

    var SIPAddress = thisUsersValues["Picture"];

    }



But I am getting this error:



SCRIPT438: Object doesn't support property or method 'replace'


File: jquery.SPServices-2014.01.js, Line: 2414, Column: 17





  1. REST API method:



    function getinfo(loginName) {
    var theData = {
    "propertiesForUser": {
    "__metadata": { "type": "SP.UserProfiles.UserProfilePropertiesForUser" },
    "accountName": loginName,
    "propertyNames": ["PreferredName", "PictureURL"]
    }
    };

    var requestHeaders = {
    "Accept": "application/json;odata=verbose",
    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
    };

    jQuery.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertiesFor",
    type: "POST",
    data: JSON.stringify(theData),
    contentType: "application/json;odata=verbose",
    headers: requestHeaders,
    success: function (data) {
    document.getElementById("profilelink").href = userProfileProperties[1];;
    document.getElementById("username").innerHTML = userProfileProperties[0];
    if (userProfileProperties[1] !== null) {
    document.getElementById("Userprofileimage").src = userProfileProperties[1];
    }
    },
    error: function (jqxr, errorCode, errorThrown) {
    console.log(jqxr.responseText);
    }
    });
    }



Error:



"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"The method GetUserProfilePropertiesFor cannot be invoked as its parameter propertiesForUser is not supported.\"},\"innererror\": {\"message\":\"The method GetUserProfilePropertiesFor cannot be invoked as its parameter propertiesForUser is not supported.\" ,\"type\":\"Microsoft.SharePoint.Client.InvalidClientQueryException\",\"stacktrace\":\"





  1. With JSOM:



    //SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
    function getUserProperties(targetUser) {

    // get the target users domain name and account name.
    var tUser = targetUser.substring(7);
    // Get the current client context.
    var clientContext = new SP.ClientContext.get_current();
    //Get PeopleManager Instance
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    // Get the properties to retrieve
    var profilePropertyNames = ["PreferredName", "PictureURL"];
    var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(
    clientContext,
    tUser,
    profilePropertyNames);
    // Get user profile properties for the target user
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    // Load the UserProfilePropertiesForUser object.
    clientContext.load(userProfilePropertiesForUser);
    //Execute the Query
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
    }



I really don't understand where the issue is. Does anyone have an idea?








0 commentaires:

Enregistrer un commentaire