1

Hi I am facing an issue while inserting Person/Group column with rest api.

I will try to explain clearly. In jquery form, i am using sp client side people picker control to save the manager name to Person/Group column.

In our organisation, any one can use that form to submit data to manager.

so, I have added 'Everyone' to site members group so that they will get contribute permission for the list.

Here the issue i am facing is, user id is not getting created automatically for the users that I am able to search in people picker. if i enter manually the user to site and submitting the form everything is working fine.

but there are 'n' number of managers which i can not add everyone to site.

Is there any resolution for my issue?

captured before adding user into site groups..

after adding the user into group, I am able to get user id, which i am using for rest api post. glad for any help.

1 Answer 1

2

Hi you can do ensure user on client side people peoplePickerUserResolved function. What it will do is it will add the user in you SharePoint site(User Information List) Code block is

var initializePeoplePicker = function (ScriptFunction,peoplePickerElementId) {
    var schema = {};
    schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
    schema['SearchPrincipalSource'] = 15;
    schema['ResolvePrincipalSource'] = 15;
    schema['AllowMultipleValues'] = truw
    schema['OnUserResolvedClientScript']= ScriptFunction;
    schema['MaximumEntitySuggestions'] = 50;
    schema['Width'] = '280px';
    SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
 }


function peoplePickerUserResolved(peoplePickerElementId, userKey) {
            if(userKey.length>0){
              var userIndex = (userKey.length-1);
              var data = {              
                  logonName: userKey[userIndex].Key
               };
    addUserToSite(data).then(function(data){},function(data){});    
}

}

initializePeoplePicker(peoplePickerUserResolved,peoplePickerElementId);

var addUserToSite = function(data) {
        var RestUrl = _spPageContextInfo.webAbsoluteUrl+"/_api/web/ensureuser";          
        return $.ajax({
                    url:RestUrl,
                    type:"POST",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-Type": "application/json;odata=verbose"
                    },
                    data: JSON.stringify( data )
                });
};
2
  • This code is working awesome :) you are life saver for my production issue. I will definitely share this to my frnds who are in need :).. thanks a lot for your valuble inputs Commented Mar 7, 2018 at 9:55
  • Welcome , this is how we learn (by making mistakes)...
    – Sunil Sahu
    Commented Mar 7, 2018 at 10:18

Your Answer

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

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