Tuesday 6 December 2016

Read Custom User Property created in Central Admin using JavaScript

Hi All,

I have faced recently one problem. Created a custom user property in Central Admin and reading property using JSOM as below.
/**** Code Start****/
$(document).ready(function(){
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CallClientOM);
});
function CallClientOM()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args)
{  
  get_user_profile_by_login(currentUser.get_loginName());
}

function get_user_profile_by_login(login) {
$().SPServices({
 operation: "GetUserProfileByName",
 async: false,
 AccountName: login,
 completefunc: function (xData, Status) {
Supervisor = getUPValue(xData.responseXML, "Supervisor");
$("input[title='Supervisor']").val(Supervisor);
  }
});
}
function getUPValue(x, p) {
  var thisValue = $(x).SPFilterNode("PropertyData").filter(function() {
    return $(this).find("Name").text() == p;
  }).find("Values").text();
  return thisValue;
}
/**** Code End****/

Problem: Here I have created Supervisor field as custom user property, but I am not able to read
from the code(as a non admin). I was set as, Policy Settings -> Default Privacy Setting -> Only Me.
Solution: While creating the property set as below:
Policy Settings -> Default Privacy Setting -> Everyone.
Then it worked!!!



No comments:

Post a Comment