Recently I faced an issue on fetching username from people picker column.
Below is the code which can be used in such scenarios-
// JavaScript source code
function getUsers() {
//Get the current client context
ctx = new SP.ClientContext(webUrl);
// Get Web
web = ctx.get_web();
// Get List
list = web.get_lists().getByTitle("My List");
camlQry = new SP.CamlQuery();
//Traverse through all files and folders deep and get some people picker fields like "Author"
camlQry.set_viewXml('<View Scope=\'RecursiveAll\'>' +
'<Query>' +
'<ViewFields>' +
'<FieldRef Name=\'' + colValuesEnum.Title + '\' />' +
'<FieldRef Name=\'' + colValuesEnum.Author + '\' />' +
'</ViewFields> ' +
'</View>');
listItem = list.getItems(camlQry);
ctx.load(listItem);
ctx.executeQueryAsync(success, failed);
}
//Success CallBack Function
function InProgRevReqSucceeded(sender, args) {
var enumerator = listItem.getEnumerator();
while (enumerator.moveNext()) {
var _User = "";
if (listItem.get_item(colValuesEnum.Author) !== 'undefined' && listItem.get_item(colValuesEnum.Author) !== null) {
if (listItem.get_item(colValuesEnum.Author).length > 0) {
// If there is a single value/user
if (listItem.get_item(colValuesEnum.Author).length == 1) {
_User = listItem.get_item(colValuesEnum.Author)[0].get_lookupValue();
}
// If there are more than one values in the people picker column, I am splitting them with a semi-colon
if (listItem.get_item(colValuesEnum.Author).length > 1) {
for (var i = 0; i < listItem.get_item(colValuesEnum.Author).length; i++) {
_User = _User + listItem.get_item(colValuesEnum.Author)[i].get_lookupValue() + ";";
}
_User.trim;
}
}
}
}
}
//Failed CallBack Function
function failed(sender, args) {
console.log('Request failed. ' + '\n' + 'Error Msg: ' + args.get_message() + '\n' + 'Stack Trace: ' + args.get_stackTrace() + '\n' + 'Correlation ID: ' + args.get_errorTraceCorrelationId());
}
Comments
Post a Comment