I am trying to add AD group to SharePoint site in REST using EnsureUser but I kept getting error message "The specified user c:0+.w|S-1-5-21-1814438218-152777602-930774774-762833 could not be found". Can someone please tell me what the correct format is for the logonName string? AD is on prem and we are using SP Online. Below is the code:
string digest = formDigest == "" ? GetRequestDigest() : formDigest; string logonNameStr = WebUtility.HtmlEncode("c:0+.w|") + "S-1-5-21-1814438218-152777602-930774774-762833"; <== This seems to be incorrect string ensureUserUrl = _siteUrl + "_api/web/EnsureUser"; using (var handler = GetHttpClientHandler(_siteUrl)) { using (HttpClient client = GetHttpClient(handler)) { client.DefaultRequestHeaders.Add("X-RequestDigest", digest); var payload = new { logonName = logonNameStr }; string jsonBody = JsonSerializer.Serialize(payload); using (StringContent content = new StringContent(jsonBody)) { content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose;charset=utf-8"); HttpResponseMessage response = client.PostAsync(ensureUserUrl, content).Result; string jsonStr = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) //if adding user failed { throw new Exception(String.Format("Failed to add AD group {0} to SharePoint site: {1}", groupName, jsonStr)); } } } }