문제

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?

도움이 되었습니까?

해결책

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
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top