I am trying to get current user group name when the member is added via Active Directory in SharePoint 2013 using Rest/jQuery. Any help is appreciated.
2 Answers
first you have to use "currentUser" api to get current user id,then try use "GetUserById" function to list all groups.
function getCurrentUser()
{
getUserWebPermissionREST();
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/CurrentUser",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data)
{
getCurrentUserGroupColl(data.d.Id);
},
error: function (data)
{
failure(data);
}
});
}
function getCurrentUserGroupColl(UserID)
{
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + UserID + ")/Groups",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
/* get all group's title of current user. */
var results = data.d.results; var InnrHtmlgrp = "
<ul>";
for (var i = 0; i < results.length; i++)
{
InnrHtmlgrp += "
<li>" + results[i].Title + "</li>";
}
$("#Group").append(InnrHtmlgrp + "
</ul>");
}
});
}
var grp = new SPAPI_UserGroup("https://Site Url"); // pass site url
sLoginUserName1 = "[email protected]"; // email id of the user which is rendering in site
var ug = grp.getGroupCollectionFromUser(sLoginUserName1);
var rows = ug.responseXML.getElementsByTagName('Group');
for (var i=0; i< rows.length; i++){
alert(rows[i].getAttribute('Name'));
}