Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How can I get the accountName property from a user field using JavaScript?

$
0
0

Please bear with me, I'm new at developing front-end for SharePoint.

I've got a news page using a custom content type. This content type contains a custom field called Contacts and is of the type People and Groups (multiuser).

I want to query the User Profile Service to get some user properties for each contact, but to do that I need the accountName for each contact. I'm planning to get this information from the Contacts field.

I've managed to retrieve an array of objects, one object for each contact using the code below, but I don't understand how to get the username from it. Each object looks something like this:

$1E_1				22465
$2e_1				"Firstname Lastname"
constructor			function()
$5I_1				function(a)
customFromJson		function()
customWriteToXml		function()
fromJson			function(a)
get_lookupId			function()
get_lookupValue		        function()
get_typeId			function()
initPropertiesFromJson	        function(b)
set_lookupId			function(a)
writeToXml			function(b, a)
__proto__			Object { $1E_1=0,  get_typeId=function(),  writeToXml=function(),  more...}
Clearly $2e_1 gives me the display name for the user, but I don't know what the number in $1E_1 comes from. 
How can I get the accountname? The object obviously contains some interesting functions, but I don't know which one to use or how to use it. Can someone please help me with this?

Once I get the accountname, I can write the code to query the User Profile Service so I just need get an id to use in my query.

This is the code I'm using to get the contact objects:
var context = SP.ClientContext.get_current();
var web = context.get_web();
var pagesLibraryAtNewsSite = web.get_lists().getById(_spPageContextInfo.pageListId);
var currentNewsItem = pagesLibraryAtNewsSite.getItemById(_spPageContextInfo.pageItemId);

context.load(currentNewsItem);
context.executeQueryAsync(function(){
	var contacts = currentNewsItem.get_item('Contacts');

	for (var i=0;i<contacts.length;i++){
		console.log(contacts[i]);
	}
}, function(sender,args) {
	console.log(args.get_message());
});



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>