jeudi 11 décembre 2014

get current user info using jquery ,REST and com



My real gol is to get information from my current user :login name and picture . I use SharePoint2013 foundation. I tried three method and non of then ork correctly.


1-this is with jquery services: I use this code



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 geting this error :



SCRIPT438: Object doesn't support property or method 'replace'
File: jquery.SPServices-2014.01.js, Line: 2414, Column: 17


2- with REST API method : the code :



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);
}
});


the 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\":\"


3-with COM: this is the code :



//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 realy don't understand where is the issue.Dose any one have an idea.








0 commentaires:

Enregistrer un commentaire