0

In SP2013 Rest api, I want to add permissions to a user. The user is picked from the people picker control. And I can only get the key like

i:0#.w|ups\stom

I can't get the actual user ID (number format). And from the examples of adding permissions using REST, you need a user ID. So is there a way I can use the string key above instead of the numeric ID to target the user I want to add permissions to?

Thanks

  • Are you using JavaScript for this or something else? If you are using Javascript I can whip you up a sample using user ID. Are you setting permissions at the site or list or item level? – Bryan Mathews Mar 2 '16 at 23:22
  • I'm looking for just js+rest but in 1 call using that user login name. I'm setting permissions at item level – omega Mar 2 '16 at 23:23
0

From:http://www.codeproject.com/Tips/798407/SharePoint-REST-API-Get-User-Id-by-User-Name

function GetUserId(userName) {
        /// change this prefix according to the environment. 
        /// In below sample, windows authentication is considered.
        var prefix = "i:0#.w|";
        /// get the site url
        var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
        /// add prefix, this needs to be changed based on scenario
        var accountName = prefix + userName;

        /// make an ajax call to get the site user
        $.ajax({
            url: siteUrl + "/_api/web/siteusers(@v)?@v='" + 
                encodeURIComponent(accountName) + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                ///popup user id received from site users.
                alert("Received UserId" + data.d.Id);
                alert(JSON.stringify(data));
            },
            error: function (data) {
                console.log(JSON.stringify(data));
            }
        });
    }
  • This makes an additional call to get the ID, which I was trying to avoid. I was looking for a way to set permissions using that login name instead of user ID. – omega Mar 2 '16 at 21:39
0

You could also use the the JSOM to get the same information.

You can also get other details using JSOM easily(https://bmspblog.wordpress.com/2015/11/18/creating-a-people-picker-in-a-web-part-page-sharepoint-2013/)

function getUserId(loginName) {
var context = new SP.ClientContext.get_current();
this.user = context.get_web().ensureUser(loginName);
context.load(this.user);
context.executeQueryAsync(
     Function.createDelegate(null, ensureUserSuccess), 
     Function.createDelegate(null, onFail)
    );
}


function ensureUserSuccess() {
    $('#userId').html(this.user.get_id());
}

function onFail(sender, args) {
    alert('Query failed. Error: ' + args.get_message());
  • This makes an additional call to get the ID, which I was trying to avoid. I was looking for a way to set permissions using that login name instead of user ID. – omega Mar 2 '16 at 21:39

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.