0

This question will be a duplicate of this one (which has no accepted answer).

I am trying to save a SP 2013 list item using the REST API.

One of the columns is a People or Group column.

Normally you write to that column by adding "Id" to the column name and using the person/group ID:

MyColumnNameId: 32

However, I do not have the user ID and I only have the login name, e.g. i:0#.w|domain\user or domain\user.

How can I write to that column using just the login name and without using the ID?

What I've tried:

  • Sending the login name directly
  • Trying to get user ID by login first, using this query:

http://domain/sites/mysite/_api/web/siteusers(@v)?@v='" + encodeURIComponent('i:0#.w|domain\user') + "'"

However, this produces an error (and I'm sure the user is there - I got it directly from GetUserById(1):

"{"error":{"code":"-2146232832, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"User cannot be found."}}}"

If I try the same URL without URIencode, it says The query string \"loginName\" is missing or invalid.".

2 Answers 2

0

Try it as below:

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|[email protected]'
ensureUser(_spPageContextInfo.webAbsoluteUrl,loginName)
.done(function(data)
{
    var userId = data.d.Id;
    console.log('User id found');

    //Create a Task item
    var taskProperties = {
        '__metadata' : { 'type': 'SP.Data.TasksListItem' },
        'Title': 'Order approval',
        'AssignedToId' : { 'results': [userId] }
    };  

    createListItem(_spPageContextInfo.webAbsoluteUrl,'Tasks',taskProperties);

})
.fail(function(error){
    console.log('An error occured while adding user');
});


function createListItem(webUrl,listName,itemProperties) 
{    
    return $.ajax({       
       url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",   
       type: "POST",   
       processData: false,  
       contentType: "application/json;odata=verbose",
       data: JSON.stringify(itemProperties),
       headers: {   
          "Accept": "application/json;odata=verbose",
          "X-RequestDigest": $("#__REQUESTDIGEST").val()
       },
        success: successHandlerForNewListItems,
        error: errorHandler    
    });
}


function successHandlerForNewListItems(){
    console.log("added data to people column");
}

function errorHandler(){
        console.log("something went wrong");
}
2
  • "The specified user i:0#.w|domainName\userName not be found." I guess it is impossible to use that for AD users who may not have a sharepoint user id.
    – user56339
    Commented Jan 24, 2017 at 2:16
  • can you try it as domainName\userName ? Commented Jan 24, 2017 at 6:54
0

if you have i:0#.w|domain\user then you can use the endpoint

_api/web/siteusers(@v)?@v='<login name>'

Where <login name> is i:0#.w|domain\user and with that you will obtain the user Id.

:) Greetings

2
  • I've tried it and it gives me "The query string \"loginName\" is missing or invalid."" if I'm passing the login name as is, and "User not found" if passing it URI-encoded.
    – user56339
    Commented Jan 23, 2017 at 12:29
  • you have to encode it, like this 'i%3A0%23.w%7Cdomain\user'
    – Marco
    Commented Jan 23, 2017 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.