3

I need to write a function where I need to check if current user belong to specific sharepoint group.

Senario: I have site like ship, logistic etc and sharepoint group like ship site member, logistic site member. User ABC is added in ship site member as he belong to ship.

When user login to site function need to validate if ABC belong to ship or logistic or both or more.

Please someone provide me piece of code

2
  • How are you writing it? Server side, client side? What version of SharePoint? Sep 17, 2014 at 15:11
  • I need it for server side
    – Brishal
    Sep 19, 2014 at 9:56

2 Answers 2

4

Here you go:

<script type="text/javascript" src="/_layouts/js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="/_layouts/js/jquery.SPServices-2014.01.js"></script>

<script type="text/javascript">

$(document).ready(function(){

isGroupMember("Your User Group", function(result){
if(result){

//do something
}
});
});
function isGroupMember(groupName, callback)
{
$().SPServices({
    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: true,
    completefunc: function(xData, Status) {

callback( !!($(xData.responseXML).find("Group[Name='" + groupName + "']").length) ); /* returns bool */
}
});
};

</script>

This should do what you need it to do. I hope :) Good Luck!

2

You can use the below C# code:

SPGroup group = site.Groups["GroupName"];

bool isUser = site.IsCurrentUserMemberOfGroup(group.ID);

if (isUser)
{
   //condition
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.