Grab person loginname and Name from a Person Column in Javascript
-
sexta-feira, 3 de agosto de 2012 00:53
Hello,
I'm trying to pull the information from a Custom Lists Multiple Person field in JavaScript.
Here is the test JavaScript code I'm using to push users into a Person Field within the list :
var clientContext = new SP.ClientContext.get_current(); var oList = clientContext.get_web().get_lists().getByTitle('SomeListName'); this.oListItem = oList.getItemById(getID); var users = new Array(); users.push(SP.FieldUserValue.fromUser("domain\\user1")); users.push(SP.FieldUserValue.fromUser("domain\\user2")); oListItem.set_item('Contacts', users); oListItem.update(); clientContext.executeQueryAsync(Function.createDelegate(this, this.onSetSucceeded), Function.createDelegate(this, this.onSetFailed));And I can pull users out in C# like this:
SPFieldUserValueCollection fieldUserValues = new SPFieldUserValueCollection(); SPQuery userQuery = new SPQuery(); userQuery.ViewFieldsOnly = true; userQuery.RowLimit = 1; userQuery.ViewFields = "<FieldRef Name='Contacts' />"; userQuery.Query = "<Where><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID Type='Integer' /></Value></Eq></Where>"; SPListItemCollection listItems = registrationList.GetItems(userQuery); if (listItems.Count > 0) { SPFieldUserValueCollection fieldUserValueCollection = (SPFieldUserValueCollection)listItems[0]["Contacts"]; foreach (SPFieldUserValue targetContact in fieldUserValueCollection) { retVal.Add(targetContact.User.Name, targetContact.User.LoginName); } }But I need to do this in JavaScript rather than C#.
I've not been able to locate much in the way of solid documentation on this... So, any pointers or code samples would be great on how to pull users (could be more than one) from a Lists People column in a custom list would be great.
Thanks
Todas as Respostas
-
sexta-feira, 3 de agosto de 2012 06:06Moderador
Hi,
I think this msdn article can help you.
http://msdn.microsoft.com/en-us/library/ee550746.aspx
Also look at similar thread with code to get multiple users from field
Let us know your result
Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
-
sexta-feira, 3 de agosto de 2012 16:53
Hello,
The link here doesn't really tell me how to leverage it [especially if its a multi-value person field and in Javascript] (http://msdn.microsoft.com/en-us/library/ee550746.aspx)
This thread (http://social.msdn.microsoft.com/Forums/lv-LV/sharepoint2010general/thread/5183e87c-ee1d-4594-9492-0dfdf6616cce) is good for C#, but the last posted question on that thread sums up my question which is "What would be the Javascript equivalent code?"
Thanks anyway, I'll keep digging...
Ultimately the real problem is that the documentation on this type of stuff from MSFT is complete and total garbage...
-
sexta-feira, 3 de agosto de 2012 18:01
Hello Martin,
You might need to treat Contacts as a text field and parse on client side. You can check it looks like User1ID;#User1Name;#User2ID;#User2Name.
As you can see there is no login name unfortunately. So, you could stick with UserID as it should be enough - at least MS thinks that way. Or you can retrieve login name from UserInformationList by UserID. Check out this thread (client object model as well :(
Hope that helps.
Vladimir
MCP, MCTS, SharePoint tips blog: http://buyevich.blogspot.com -
sexta-feira, 3 de agosto de 2012 19:13
Here's what I have so far:
var tempContacts = this.oListItem.get_item('Contacts'); // we get back a set of objects... // spin them for (i = 0; i < tempContacts.length; i++) { var oUser = tempContacts[i]; var tempId = oUser.get_lookupId(); var tempDisplayName = oUser.get_lookupValue(); }get_lookupId gives me the ID of the user (ex. 16) and the get_lookupValue gives me the display Name (ex. John Smith)
Now, if I have the users ID, there MUST be a way to get their login information (ex. domain\\user) from this information in JavaScript..
at least I hope there is...

