Set the value of a 'person or group' column (2010)

Apr 12, 2011 at 10:54 AM
Edited Apr 12, 2011 at 10:57 AM

Hi,

How would you set the value of a "person or group" colum?

I have tried to use SPGetcurrentUser and UpdateListItems - but I get a "one or more fields have not been installed correctly.

I think this is because the contents of a "person or group" seems to be a pair of a numeric ID and the title. eg.:

          23;#Edwards, Mike

What is this number? And can I get it for the current user and concatinate itogether, and use the string?

Can anyone help? Here is my code:

 var thisUserName = $().SPServices.SPGetCurrentUser({
	fieldName: "Title",
	debug: false
});

function addMeToCourse(theid){
	 $().SPServices({
      	       operation: "UpdateListItems",
       	       async: false,
   	       debug:true, 
listName: "Training Attendees Table",
 ID:theid,
valuepairs: [["ows_users", thisUserName]], completefunc: function (xData, Status) { alert(Status); alert(xData.responseXML.xml);
} }); }
Many thanks
Mike
*Edit - I had missed out the "ID:theid" parameter - this wasnt the problem tho :(

 

Apr 12, 2011 at 11:49 AM

I believe your issue is with the Valuepairs.  "ows_users" should probably just be "users".

Try that and see what happens.

Apr 12, 2011 at 11:53 AM

Yes - that works....

Thanks you, thank you, thank you, thank you, thank you, thank you.

Mike

 

Coordinator
Apr 12, 2011 at 11:57 AM
Edited Apr 12, 2011 at 11:57 AM

Yes, assuming that the StaticName (internal name) of the column is "users".

You do also need the ID of the user for the update to work.

BTW, there's no debug mode for the core Web Services calls in $().SPServices.

M.

Apr 12, 2011 at 12:18 PM

Thanks for the input.

 

By "the ID of the user" do you mean the numeric value that makes up the hash pair?

I am having some success using "-1#" plus the fieldName "Name" returned from SPGetCurrentUser.

In this code - "fullAttendees" is a string containing the contents of the same field, but before I update it (so a string of 'id#username;')

var thisUserName = $().SPServices.SPGetCurrentUser({
         fieldName: "Name",
         debug: true
});

updateText =fullAttendees+";#-1;#"+thisUserName;
     $().SPServices({
             operation: "UpdateListItems",
              async: false,
              listName: "Training Attendees Table",
              ID:theid,
              valuepairs: [["users", updateText ]],
              completefunc: function (xData, Status) {
                alert(Status);
                alert(xData.responseXML.xml); 
                loadTraining();           
              }
    });

This *seems* to be working. Thanks for your help.

Coordinator
Apr 12, 2011 at 12:23 PM
Edited Apr 12, 2011 at 12:24 PM

I'm not sure about the -1 thing. If the column value is being updated correctly, then that's an interesting trick. Even if that works, where you'll run into problems is if any two or more people have the same name.

The number 23 in "23;#Edwards, Mike" is the ID of the user named Mike Edwards in the User Information List. This notation is used across SharePoint to specify ID;#value pairs. IMO the primary reasons for this "double" storage is for compatibility with Office desktop apps as well as to improve efficiency on rendering individual items. (Believe you me, it can cause issues, as well.)

M.

Apr 12, 2011 at 12:28 PM

Thanks -

Yeah I found an article somewhere advocating its use - this was using C# - not the web services. But it seems to work.

So how can I get the ID of the current user? The SPGetCurrentUser documentation doesn't appear to contain a reference to an 'ID' fieldname...

Mike

Coordinator
Apr 12, 2011 at 12:49 PM

No, SPGetCurrentUser doesn't have an ID option, but I just took a look and you can grab it from the variable _spUserId, which SharePoint sets.

Try

alert(_spUserId);

I should add ID as an option to SPGetCurrentUser.

M.

Apr 12, 2011 at 12:52 PM

Thats great - thank you very much for your help.

And thanks for your great framework!

Mike

Apr 12, 2011 at 12:55 PM

Hmmm... All this time I've been doing a double call.  SPGetCurrentUser/GetUserInfo.

Thanks for the tips...

Coordinator
Apr 12, 2011 at 1:11 PM

BTW, I just validated that _spUserId is present in both 2007 and 2010.

M.

Coordinator
Apr 12, 2011 at 1:17 PM

Logged and added to v0.6.1ALPHA3.

http://spservices.codeplex.com/workitem/8437

M.