1

I have webpage that uses a client side people picker and allows the user to address a message to other valid users within our on-prem SharePoint 2013.

I'm trying to write a record of the message to a list and having problems with the Recipients part of the list item. Recipients is a multipick people column.

I've found many tutorials like this one which show that Recipients will get another hidden field called RecipientsId and that I can use a REST call like

data: { "__metadata": { "type": "SP.Data.ListNameListItem" },
        "Title": "The title",
         "RecipientsId": ["5","7"] };

But there are a couple of reasons the <name>Id column won't work. First The user Id number is based upon if the user has visited my site (or maybe site collection?) In any event a user who visits my site might send to a recipient who has never been to my site and therefore doesn't have a local user id.

The other problem is that I'm following this on how to use the client people picker and it shows how to get the details about the people in the picker with this code:

var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
var users = peoplePicker.GetAllUserInfo();

But the information returned about the users in the people picker doesn't include the Id. I assume this is also because of issue #1 where the user in the people picker may not have an Id on the current site. Here is the information that is returned.

AutoFillDisplayText:"First Last"
AutoFillKey:"i:0#.w|domain\IdNumber"
AutoFillSubDisplayText:"Job Title"
AutoFillTitleText:"First.Last@example.com↵Active Directory↵domain\IdNumber"
Description:"domain\IdNumber
DisplayText:"First Last"
DomainText:"collectionurl"
EntityData:Object
EntityType:"User"
IsResolved:true
Key:"i:0#.w|domain\IdNumber"
LocalSearchTerm:"first last"
MultipleMatches:Array[0]
ProviderDisplayName:"Active Directory"
ProviderName:"AD"
Resolved:true

What I would like to do is user the Key or AutoFillKey or Description to populate the Recipients Column. I have tried those and also tried stripping out the leading i:0#.w and/or domain with no success.

TL;DR

Is there a way to directly write to a person column (and not the hidden related Id) using the REST api?

If there isn't...

How do I get the UserId for a user based on the info available in a people picker?

How to I get a UserId for someone who has never been to my site?

5
+50

In any event a user who visits my site might send to a recipient who has never been to my site and therefore doesn't have a local user id.

It looks like you are able to post people to the list so long as they are present on the site. You might try using the ensureUser method first.

From Microsoft:

EnsureUser method Checks whether the specified login name belongs to a valid user in the site. If the user doesn't exist, adds the user to the site.

$.ajax({
  url: "http://<site url>/_api/web/ensureuser",
  type: "POST",
  data: "{ 'logonName': 'i:0#.f|membership|user@domain.onmicrosoft.com' }",
  headers: {
    "X-RequestDigest": <form digest value>,
    "accept": "application/json;odata=verbose"
  },
  success: successHandler,
  error: errorHandler
});

https://msdn.microsoft.com/en-us/library/office/dn499819.aspx#bk_WebEnsureUser

Example with REST by Vadim Gremyachev:

function ensureUser(webUrl,loginName)
{
   var payload = { 'logonName': loginName }; 
   return $.ajax({
      url: webUrl + "/_api/web/ensureuser",
      type: "POST",
      contentType: "application/json;odata=verbose",
      data: JSON.stringify(payload),
      headers: {
         "X-RequestDigest": $("#__REQUESTDIGEST").val(),
         "accept": "application/json;odata=verbose"
      }
   });  
}


var loginName = 'i:0#.f|membership|jdoe@contoso.onmicrosoft.com'
ensureUser(_spPageContextInfo.webAbsoluteUrl,loginName)
.done(function(data)
{
    console.log('User has been added');
})
.fail(function(error){
    console.log('An error occured while adding user');
});

https://stackoverflow.com/questions/33259576/sharepoint-2013-ensureuser-via-rest-api

Example with CSOM by Steve_Butler:

function EnsureUser(LoginName) {
    appWebContext = new SP.ClientContext.get_current();
    var theUser = appWebContext.get_web().ensureUser(LoginName);
    results = appWebContext.load(theUser);
    appWebContext.executeQueryAsync(Function.createDelegate(null, ensureUserSuccess), Function.createDelegate(null, ensureUserFailure));
}

https://social.msdn.microsoft.com/Forums/sharepoint/en-US/c6630cb5-7508-4193-8917-6ea16cb43309/js-and-rest-posting-to-a-persongroup-field-in-a-list?forum=sharepointdevelopment

UPDATE:

If you have your array of recipients you could add the Deferred of each into an array and iterate over the results.

function ensureArrayOfUsers(arrayOfUserIds){
    var arrayOfPromises = [];
    arrayOfUserIds.map(function(userId){
        arrayOfPromises.push(ensureUser(webUrl, user));
    });
    return $.when.apply($, arrayOfPromises);
}

ensureArrayOfUsers(arrayOfUserIds)
.done(function(){
    // write to list
});
  • So I'm taking it that there isn't a way to actually add users directly, but only with the ID? With the ensureuser is there a way to batch that, since there might be more than one recipient? I'm using jquery for my REST calls, if I can't batch them do you know how I would make sure that I've ensured all users before I proceed? – Rothrock Oct 10 '16 at 20:06
  • I updated my answer. Note I did not test the code. – MickB Oct 10 '16 at 21:48
  • I will give that a try. Thank you. It is a bummer that the people picker doesn't return the information needed to actually use the people picker for any kind of usual purpose, huh? – Rothrock Oct 11 '16 at 14:39
  • That works great. I've never really understood "$.when()" nor .apply() I will have to reread the documentation. Even though I can't add a user directly this makes it not too hard to do. Thank you very much. – Rothrock Oct 11 '16 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.