我正在尝试从我的自定义列表中获取当前用户的列表计数。但是错误:(“错误:Sys.ArgumentNullException:值不能为null。参数名称:clientObject”)正在被抛出。我需要获取当前用户属性的代码的第一部分工作正常。我认为PopulateEmployeeHeader(currentUser)导致错误。
$(document).ready(function(){
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
$('#errorBase').hide();
});
var userProfileProperties;
var user;
function loadUserData(){
//Get Current Context
var clientContext = new SP.ClientContext.get_current();
//Get Instance of People Manager Class
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
//Get properties of the current user
userProfileProperties = peopleManager.getMyProperties()
clientContext.load(userProfileProperties);
//Execute the Query.
clientContext.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
var employeePhoto =userProfileProperties.get_pictureUrl();
PopulateEmployeeHeader(userProfileProperties.get_displayName());
if(employeePhoto !== undefined && employeePhoto !== "" && employeePhoto !== null)
{
$('#employeePicture').attr('src', userProfileProperties.get_pictureUrl());
}
else
{
$('results').innerHTML = "Picture does not exist in User Profiles list. Please upload your picture.";
}
}
function onFail(sender, args) {
alert("Error: " + args.get_message());
}
var oEmployee;
function PopulateEmployeeHeader(currentUser)
{
// var queryListItem = '<View><Query><Where><Eq><FieldRef Name="j2ur"/><Value Type="User">'+currentUser+'</Value></Eq></Where></Query></View>';
var queryListItem='<View><Query><Where><And><Eq><FieldRef Name="j2ur" />'+currentUser+'</Eq><IsNotNull><FieldRef Name="j2ur" />'+
+'</IsNotNull></And></Where></Query></View>';
var siteUrl = window.location.protocol + "//" + window.location.host+'/';
var clientContext = new SP.ClientContext(siteUrl);
var oEmployees = clientContext.get_web().get_lists().getByTitle('Accounts');
var myquery = new SP.CamlQuery();
myquery.set_viewXml(queryListItem);
this.oEmployee= oEmployees.getItems(myquery);
clientContext.load(oEmployee);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onEmployeeSucceeded),
Function.createDelegate(this, this.onEmployeeFailed)
);
}
function onEmployeeSucceeded(sender, args) {
alert(oEmployee.get_count());
}
function onEmployeeFailed(sender, args) {
alert('Request failed. ' + args.get_message() +'\n' + args.get_stackTrace());
}
})(jQuery);
答案 0 :(得分:0)
我实际上最终重写了代码。以下作品如魅力:)
var web;
var user;
var context;
var userdisplayName;
$(document).ready(function(){
context = SP.ClientContext.get_current();
web = context.get_web();
user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(onSuccess, onFail);
});
function onSuccess()
{
var employeePhoto =userProfileProperties.get_pictureUrl();
if(employeePhoto !== undefined && employeePhoto !== "" && employeePhoto !== null)
{
$('#employeePicture').attr('src', userProfileProperties.get_pictureUrl());
}
else
{
$('results').innerHTML = "Picture does not exist in User Profiles list. Please upload your picture.";
}
}
PopulateHeader(user.get_title());
}
function onFail()
{
alert("Error: " + args.get_message());
}
function PopulateHeader(username)
{
var oList = web.get_lists().getByTitle('Accounts');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'j2ur\'/>' + '<Value Type=\'User\'>'+ username+'</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);
context.load(collListItem);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var empNumber=oListItem.get_item('_x0065_os0');
listItemInfo += "Title : " + oListItem.get_item('Title') + "<br/>";
listItemInfo += "Project : " + oListItem.get_item('_x0069_i43') + "<br/>";
listItemInfo += "EmployeeNumber : " + oListItem.get_item('_x0065_os0') + "<br/>";
}
$('#employeeNumber').html(empNumber);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}