Insert to a "People or Group" field from a "people picker"

Feb 21, 2012 at 12:04 PM
Edited Feb 21, 2012 at 12:35 PM

Hi, 

I have a "people picker" control on a page, and I want to grab its value in insert into a column of type "person or group". 

So I have the control:

<SharePoint:PeopleEditor ID="spPeoplePicker" Title="Owner" runat="server" Width="350" SelectionSet="User"/>

And I can grab the NTID no problem, it resolves as DOMAIN\username

_Owner =  $("textarea[title='Owner']").val();

When I try to insert this as part of a value pair - I get an error. It seems that the field is expecting it in the format:

 

ID;#Domain/username

 

How can I get this numeric ID for the user I am trying to add?  Or has anyone else added someone from a people picker to a person or group field?

 

Thanks

 

Mike

Feb 21, 2012 at 12:43 PM

Getting closer....:

 

http://blogs.msdn.com/b/uksharepoint/archive/2009/02/17/quick-tip-using-the-sharepoint-person-or-group-field-in-code-part-1.aspx

 

Coordinator
Feb 21, 2012 at 12:52 PM

Mike:

You should be able to use the value as 0;#domain/username. SharePoint will figure out the ID for you. This assumes you're within a single Site Collection.

M.

Feb 21, 2012 at 1:09 PM

Hi Marc, 

 

Sorry - that doesnt work at all. When I insert "0#;DOMAIN/edwardsm" the value in the field is blank, a closer look with "U2U Caml builder" gives the value as  "0#;"

 

I also tried:

 

0#;DOMAIN/edwardsm
0#;DOMAIN\edwardsm
0#;edwardsm 

 

But none worked. 

 

This article suggests that the userID is available using the Object model, but that the user must be "ensured" first:

 

http://blogs.msdn.com/b/uksharepoint/archive/2009/02/17/quick-tip-using-the-sharepoint-person-or-group-field-in-code-part-1.aspx

 SPUser user = web.EnsureUser(sAlias); 
  sAllContacts += user.ID.ToString() + ";#" + user.LoginName.ToString() + ";#"; 

 

Would this EnsureAlias be available via spservices?

Im using moss 2007 if that makes a difference.

 

Thanks,

 

Mike

Feb 21, 2012 at 2:01 PM

Mike,
I'm assuming you are generating an AJAX update via SPServices... Because a USER/GROUP field is a "lookup" field, try using only the ID integer on the update and set the... Here is an example:

<Method ID='1' Cmd='New'>
    <Field Name='User' LookupId='True'>1</Field>
</Method>

 

Like Marc stated: sharepoint should take care of populating the name of the user... I have done this for People... don't recall if I ever did it for Group's... should work the same.

Also, take a look a this post which also describes how you can grab information about selected users (in the people picker) on the page:

http://spservices.codeplex.com/discussions/268910

Good luck.

Paul

 

Feb 21, 2012 at 3:32 PM

Thanks for the reply, 

I dont understand - the problem is that I dont know the ID Integer of the user. I am using a people picker - from which I can get the NTID (edwardsm) - but I cant get the NTID.

Using "GetUserInfo" service works to get the numeric ID - but only if they are already a sharepoint user. If that person is not already a sharepoint user, then there isnt an ID yet. (If you use the standard new item form - the people picker seems to handle this).

 

Thats why the link above suggests using "ensureuser" - but thats using server side code - I cant work out how to access this via web services. 

 

Thanks

 

Mike

 

 

Feb 21, 2012 at 3:54 PM

Mike,

What I was trying to describe, is that once a user, through the People Picker field, selects a users, SP actually stores information about the selected user in a hidden <div> element on the page until the form is ready to be "saved/submitted"... You can use Javascript to find that hidden element and drill down to get the ID of that selected user. 

The prior link I posted shows you how to get the NTID of a user that was selected/resolved by the people picker (note... its important to not just get whatever value in on "$("textarea[title='Owner']").val()" as you are doing above... a user could have just typed in some junk text and never relay picked a valid and "resolved" user)

Using the method to find the resolved entries by the people picker and IE's Debugger, you can walk through the DOM tree to see the HTML structure and then use jQuery to get the user's ID.

Paul

Feb 21, 2012 at 4:23 PM

Yes - thanks, 

But - it only works if the user has visited the sharepoint site. Even if the People Picker resolved the user (as they are a valid active directory user)....it seems that Sharepoint does not create a user profile unless they login to that site collection.

I think that is what the ensueuser method does - but is there anyway to do this with spservices?:

 

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.ensureuser.aspx

Thanks

Mike

 

 

 

 

 

 

 

 

Feb 21, 2012 at 5:21 PM

Mike,

Just so that we keep each item in sync... your initial issue of, Unable to get the ID for a resolved USER via a People Picker field, is not fixed, Correct? You are able to get the ID?

Or are you saying that if the user is not "...login to that site collection", the People Picker does not resolve the selected USER?

Can you paste the JavaScript code that is attempting to use the ID of the resolved user.

The question around EnsureUser operation would need the help of Marc.

Paul

Feb 21, 2012 at 6:08 PM

Thanks for your help - but I think its the same issue. 

Your solution works - but only if the user has at some point logged into the site collection.  If the user has not logged into the sharepoint site - then the people picker does not produce an ID - and you cant use "GetUserInfo" either.

That's what this ensureuser seems to do... create a new SPID if it doesnt exist...but I dont know if this is feasible using spservices.

Thanks

Mike

 

 

Feb 21, 2012 at 6:22 PM

Mike,

Got it... Thanks for the post back...

I'm sure Marc will try to respond when he gets a chance.

Paul.

Apr 13, 2015 at 9:12 AM
Hello

sorry about write into this old post, but finally you got the solution for this issue?

Thanks
Coordinator
Apr 29, 2015 at 9:16 PM
It's best not to add onto such an old thread. If you're having a problem, please start a new thread with your details.

M.