Working with People Fields

Oct 8, 2014 at 8:51 PM
Edited Oct 8, 2014 at 8:55 PM
I'm just starting to look into this question, but no initial research is turning up anything recognizably relevant. I have a form with a dropdown menu that contains a list of user names; this list is populated thanks to a GetUserCollectionFromGroup call. The menu allows me to pick a user in the given group and display a checkbox list of assigned responsibilities, which come from a list called Priorities.

The ultimate purpose of the form is to be able to make changes to those responsibilities using the checkboxes. I've gotten everything to the point where I can tell which boxes have changed and I've preserved the IDs for responsibilities that have changed. What I want to do now is use the collected IDs, compare them one at a time against the Priorities list, then add or remove the user as the case may be. If I check a responsibility in the form, the name should be added to the Priority list's AssignedTo field; unchecking will remove the name from this field.

What I'm not sure of is how to take a string (the username from the dropdown) and add or remove the user from the People type field in the list. I have a feeling this is going to require an Update List call, but I'm not sure how to convert the string to a user object (if that's the right terminology) or if that's even necessary.

Has anyone had experience with this?
Coordinator
Oct 8, 2014 at 9:16 PM
You'll end up using UpdateListItems. If you know the user's ID in the Site Collection, that's all you'll need to do the update in the Person or Group field.

If you search these discussions or my blog you'll undoubtedly find some examples.

M.
Oct 8, 2014 at 9:55 PM
I've tried using UpdateListItem to update an AssignedTo field and I haven't been able to get it working correctly. I asked about it here: https://spservices.codeplex.com/discussions/569270
Oct 9, 2014 at 12:05 PM
Marc, your level of involvement and responsiveness with SPServices never ceases to amaze me. I appreciate the input! I've been doing some more research this morning and have yet to find something that shows a clear way for me. I'm hoping this helps.

I first build the dropdown with this code:
$().SPServices({
  operation: "GetUserCollectionFromGroup",
  groupName: groupName,
  async: false,
  completefunc: function (xDataUser, Status) {
    htmlBuilder = "<label for='groupMember'>Members of " + groupName + ":</label><br /><br />";
    htmlBuilder += "<select name='groupMember' id='groupMember'>";
    htmlBuilder += "<option>Select a group member</option>";
    
    $(xDataUser.responseXML).find("User").each(function() {
      htmlBuilder += "<option>" + $(this).attr("Name").trim() + "</option>";
    });
    
    htmlBuilder += "</select>";
  }
});
This results in a list with entries like "Smith, John" or "Doe, Jane"-- display names only. I suppose I could store display name and ID in an array for future reference.

Once the user makes the assignment selections and clicks the Submit button, I then want to update the source list's AssignedTo field for the items that have changed. If the user ID for this particular user is 300, at this point do I then update AssignedTo by passing in something like "300;#DOMAIN\Smith, John," or is it sufficient to only pass 300?
Oct 10, 2014 at 1:55 PM
Hi AurumPotabile,

I tried doing it like "300;#DOMAIN\Smith, John," and that did not work for me. I haven't tried simply using "300" the ID.
Coordinator
Oct 10, 2014 at 2:54 PM
Sorry I didn't get back to you. The easiest way to update a Person or Group field is to just use the ID.

In my test site, I'm user 9, and this updates the Sales Rep column to me:
$().SPServices({
  operation: "UpdateListItems",
  listName: "Sales Opportunities",
  ID: 2,
  valuepairs: [["Sales_x0020_Rep", "9"]]
});
M.
Oct 10, 2014 at 4:41 PM
Edited Oct 10, 2014 at 4:49 PM
Marc, I'm sure you have more pressing concerns than this! Besides, you didn't need to-- your comments above put me on the right path, so I've been working feverishly to get it working-- which it does now.

SimK, here's what I ended up with. Keep in mind that I'm coming from a combobox menu, so you'll need to tailor things to your own needs, but this might get you on the right track.

First, I'm building a list of my users-- note the value I'm building for each <option> tag:
$().SPServices({
  operation: "GetUserCollectionFromGroup",
  groupName: groupName,
  async: false,
  completefunc: function (xDataUser, Status) {
    htmlBuilder = "<label for='groupMember'>Members of " + groupName + ":</label><br /><br />";
    htmlBuilder += "<select name='groupMember' id='groupMember'>";
    htmlBuilder += "<option>Select a group member</option>";
    
    $(xDataUser.responseXML).find("User").each(function() {
      userId = $(this).attr("ID");
      userName = $(this).attr("Name");
      htmlBuilder += "<option value='" + userId + ";#" + userName + "'>" + userName + "</option>";
    });
    
    htmlBuilder += "</select>";
  }
});
The final format for each option is <option value="300;#Smith, John">Smith, John</option>

I grab the currently assigned user list in a GetListItems call, then either add or remove the selected user based on the checkbox state and store the result in a variable called existingUsers. All I have to do then is run this UpdateListItems call:
$().SPServices({
  operation: "UpdateListItems",
  async: false,
  listName: myList,
  ID: myId,
  valuepairs: [["AssignedTo", existingUsers]]
});
Et voila tout!

EDIT:

As Marc mentioned (though I didn't try it), you may be able to get away with using the ID only. I went the route I did because the string returned from a GetListItems call returned ID;#UserName, so I just followed suit.

Thanks again for the help, Marc; I sincerely appreciate it.