Domanda

I want to create a form which allows users to search for a particular user who may not exist on the current site. I can do that using the SearchPrincipals web service, but the users will have an ID of -1 so I can't use that ID in order to set a people picker field in the background. Is there any way that we can add a user to the current site by his/her account name?

È stato utile?

Soluzione

You can use the EnsureUser method of Web object by referring this article. Here is the sample code which will check if the user exists (and return the SPUser object), if it doesn't, it will add the user and then return the SPUser object in JSON:

$.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 > ,
        "content-type": "application/json;odata=verbose"
        "accept": "application/json;odata=verbose"
    },
    success: successHandler,
    error: errorHandler
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top