I need to the group of the current user : this is my code
function checkGroup() {
var group = [“Directeur des ventes”, “Superviseur”, “VM”, “CP”];
for (var i = 0; i < group.length; i++) {
if (isMember(group[i]) == true)
{
alert(‘je suis membre du group’+ i);
break;
}
}
}
function isMember(groupName){
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + “/_api/web/sitegroups/getByName(‘”+groupName+”‘)/Users?$filter=Id eq ‘” + _spPageContextInfo.userId+”‘”,
method: “GET”,
headers: { “Accept”: “application/json; odata=verbose”},
success: function (data) {
alert(‘im member of group’ + groupName);
return true;
},
async: false,
});
}
but il always get alert('im member of group' + groupName); !!
any idea ,
Hi ,
Try the following Sample that i have created for you :
<script src=”//code.jquery.com/jquery-1.11.2.js” type=”text/javascript”></script>
<script type=”text/javascript” src=”/_layouts/15/sp.runtime.js”></script>
<script type=”text/javascript” src=”/_layouts/15/sp.js”></script>
<script type=”text/javascript”>
$(document).ready(function () {
getCurrentUserWithDetails().done(function(data)
{
var groupNames = [‘Group Template Admins’,’Approvers’];
//determine wether current user is a memeber of group(s)
var userGroups = data.d.Groups.results;
var foundGroups = userGroups.filter(function(g){ return groupNames.indexOf(g.LoginName) > -1});
alert (JSON.stringify(foundGroups));
})
.fail(
function(error){
alert(JSON.stringify(error));
});
});
function getCurrentUserWithDetails()
{
var endpointUrl = _spPageContextInfo.webServerRelativeUrl + ‘/_api/web/currentuser/?$expand=groups’;
//OR
//var endpointUrl = ‘your site collection address/_api/web/currentuser/?$expand=groups’;
return $.ajax({
url: endpointUrl,
method: “GET”,
contentType: “application/json;odata=verbose”,
headers: {
“Accept”: “application/json;odata=verbose”
}
});
}
</script>
Also you can follow my blog www.sp4geeks.com i’m going to publish more posts that will be useful for you in learning SP 2013 REST .
Hope this help.
Thanks,
ME
