Skip to main content

How to get lookup value(username) from people picker column using JavaScript(Client Object Model)?



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

Popular posts from this blog

Angular JS Routing Step by Step using Visual Studio.

In this post, we will see step by step how to create an Angular JS Project using Visual Studio. This is for beginners only J Before we start, make sure ·         You have very basic understanding of AngularJS ·         Basic understanding of Routing ·         Beginner level knowledge on html5 ·         Hands-On using Visual Studio 1.       Open Visual Studio and click “new project”. Select “ASP.NET Web Application”. Enter Project Name, Location and Solution Name . 2.   Create an “ Empty ” ASP.NET Web Application as shown in below figure 3.  Open Solution Explorer, right click on the solution and click “Manage Nuget Packages” Install Angular.js Once installed, you will see all Angular.JS references in “Scripts” folder. 4.    In Solution...

Update List Items : Replace a value in a field which contains some text(URL in this case)

Consider a scenario wherein you need to replace a part of value in single line of text. 1) Fetch List Items based on a particular condition using CAML Query Eg: Get all list items where status = "In Progress" and Content Type ="My Content" 2) From the list items returned by CAML query, modify and replace a particular string from a value. 3) Column type is single line of text Add-PSSnapin Microsoft.Sharepoint.Powershell cls #Log Variables $LogFolderPath = "C:\Logs\"  # LogFolderPath # Text File Path $LogFilePath = $LogFolderPath+ "txtLog_WF_UPDATE_"+(Get-Date).ToString("MM_dd_yyyy_hh_mm_ss")+".txt" # CSV Log File Path with current time stamp $CSVLogFilePath=$LogFolderPath+ "CsvLog_WF_UPDATE_"+(Get-Date).ToString("MM_dd_yyyy_hh_mm_ss")+".csv" #SP variables $web=$null ; $list=$null; $SiteURL = "http://mysharepointsite:30042/sites/en-us"# Library Site Url $ListName =...