JSOM 对User的操作

时间:2021-02-05 22:02:38

一、操作当前用户

//Load current user info
function LoadCurUser()
{
var curUser;
curUser = curWeb.get_currentUser();
curContext.load(curUser);
curContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); function onQuerySucceeded() {
$("#hdUserName").text(curUser.get_title());
$("#txtReqName").text(curUser.get_title());
//Load mail configuration
if (wfType)
LoadMailConfig();
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}

二、操作其它用户

function LoadUserByID(userId)
{
var camlQuery = new SP.CamlQuery();
var caml = '<View><Query><Where><Eq><FieldRef Name=\'ID\'/>'
+ '<Value Type=\'Number\'>' + userId + '</Value></Eq>'
+ '</Where></Query><RowLimit>1</RowLimit></View>';
camlQuery.set_viewXml(caml);
var userList = curWeb.get_siteUserInfoList();
var users = userList.getItems(camlQuery);
curContext.load(users); curContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded(sender, args) {
var user = users.itemAt(0);
var userName = user.get_item('UserName');
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}

三、操作用户组

function retrieveAllUsersAllGroupsSpecificProperties() {

    var clientContext = new SP.ClientContext(siteUrl);
this.collGroup = clientContext.get_web().get_siteGroups();
clientContext.load(collGroup, 'Include(Title,Id,Users.Include(Title,LoginName))'); clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
} function onQuerySucceeded() { var userInfo = ''; var groupEnumerator = collGroup.getEnumerator();
while (groupEnumerator.moveNext()) {
var oGroup = groupEnumerator.get_current();
var collUser = oGroup.get_users();
var userEnumerator = collUser.getEnumerator();
while (userEnumerator.moveNext()) {
var oUser = userEnumerator.get_current();
this.userInfo += '\nGroup ID: ' + oGroup.get_id() +
'\nGroup Title: ' + oGroup.get_title() +
'\nUser: ' + oUser.get_title() +
'\nLogin Name: ' + oUser.get_loginName();
}
} alert(userInfo);
} function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

四、使用get_fieldValues()获取其它字段信息

Get Name:

var obj =  item.get_fieldValues().Name;

Get UserName:

var obj =  item.get_fieldValues().UserName;

Get Email:

var obj = item.get_fieldValues().Email;

Get First Name:

var obj = item.get_fieldValues().FirstName;

Get Job Title:

var obj = item.get_fieldValues().JobTitle;

Get Mobile Phone:

var obj = item.get_fieldValues().MobilePhone;

Get Work Phone:

var obj = item.get_fieldValues().WorkPhone;

Get Notes:

var obj = item.get_fieldValues().Notes;

Get Picture:

var obj = item.get_fieldValues().Picture;

Get SIP Address:

var obj = item.get_fieldValues().SipAddress;

Get Title:

var obj = item.get_fieldValues().Title;

Get  Department:

item.get_fieldValues().Department;

Get  Web Site:

item.get_fieldValues().WebSite;

Get TimeZone:

item.get_fieldValues().TimeZone;