Richard Banks
Richard Banks

Reputation: 2983

Inserting a user into a person field in sharepoint using the rest api

I'm trying to update a list item in a remote sharepoint site using the rest api from a workflow. I'm having issues understanding how to populate a person field. I've looked online and read that you should use the id of the user and not the login, however what if I do not know the users id? Where can I get this from?

I've been reading the following link but it doesn't explain where the id comes from

how to add user to sharepoint list item user field using REST api in sp2013?

Upvotes: 1

Views: 4637

Answers (2)

SiD
SiD

Reputation: 1

I know its a a bit late but for anyone looking for the answer on how to get the user id use the below function

function getUserId(userName) {

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers?$select=Id&$filter=Title eq '" + userName + "'",
        type: 'GET',
        headers: {
            "Accept": "application/json;odata=verbose"
        },
        success: function (data) {
            return data.d.results[0].Id;
        },
        error: function (error) {
            console.log(error);
        }
    });
}

Upvotes: 0

Nils
Nils

Reputation: 969

You will be able to view all UserProfile Properties by using this REST call:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='domain\user'

To get a specific Property (in this case UserProfileGUID) use:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='UserProfile_GUID')?@v='domain\user'

If you are using CSOM this might help you: The proper way to write a SharePoint User to a User Field in a SharePoint list

I have done this successfully in the past using both CSOM (js) and SSOM (powershell).

Upvotes: 0

Related Questions