Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if Current user exist in sharepoint group in a site

Posted on 2013-05-18
5
Medium Priority
?
3,811 Views
Last Modified: 2014-04-01
I have multiple permission groups created on the site and I have a form displaying the drop down list of the group names. This is a choice field.

Before submitting the form, I would like to check if the current user , based on whatever he selects in the dropdownlist, I would like to check if the user exist in that group before the user clicks the submit.

I have the folowing code to find  the current user on the SP site. But i dont know how to modify the code to find if the current user exist in the permission group?  

<script type="text/javascript" language="javascript">
var context = null;
    var web = null;
    var currentUser = null;
    function getWebUserData() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
    }
    function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
        alert('User name:' + userObject.get_title() + '\n Login Name:' + userObject.get_loginName());
    }
    function onFailureMethod(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
    </script>
PLease help.
0
Comment
Question by:ven1234
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39178461
Hi,
perhaps the usage of SPServices would make this easier:
http://morshemesh.blogspot.de/2012/07/getting-all-groups-of-user-with.html

HTH
Rainer
0
 

Author Comment

by:ven1234
ID: 39206691
Hi,
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var groupName;

 $().SPServices({  
      operation: "GetGroupCollectionFromUser",  
      userLoginName: $().SPServices.SPGetCurrentUser(),  
      async: false,  
      completefunc: function(xData, Status) {
        //if current user is not a member of this group...  
        if($(xData.responseXML).find("Group[Name=' a group"]").length != 1)  
        {  
              //...disable the following fields
             alert("Please check the activity you have selected");
             $("input[Title='btnSave']").prop("disabled", "disabled");
              }  
      }  
    });  
});

</script>

The above code works if i have find for 1 group.  But based on the drop down selection, I have to check if that user exist in that group.  Can you please guide me on how to write that code?.
0
 

Author Comment

by:ven1234
ID: 39206696
Something like this
isGroupMember("myGroupName", function(result){
if(result){
//code here
}
});

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 */
}
});
};

I have an drop down field and based on the field selected, I have to assign that value to a variable and that variable needs to be accessed as groupName.

Please help to rewrite the code.
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 2000 total points
ID: 39213559
Hi,
as I have not your full page source, I did a small PoC:
<div id="workingarea">
<select id="groupselection">
	<option value="">Please select</option>
	<option value="EE Testing Members">Members</option>
	<option value="EE Testing Visitors">Visitors</option>
	<option value="EE Testing Owners">Owners</option>
	<option value="TestDummy1">TestDummy1</option>
	<option value="TestDummy2">TestDummy2</option>
</select>
</div>
<p></p>
<div id="debugbox">
Debug messages:<br/>
</div>
<!-- Begin Javascript and JQuery -->
<script type="text/javascript" src="/_layouts/RainerJScripts/jquery-1.8.3.min.js" language="javascript"></script>
<script type="text/javascript" src="/_layouts/RainerJScripts/jquery.SPServices-0.7.2.min.js" language="javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
	// Gets the current sites URL
	var currSiteUrl = $().SPServices.SPGetCurrentSite();
	$("#debugbox").append('Current site url: ' + currSiteUrl +'<br/>');
	// Gets the current user
	var currUser = $().SPServices.SPGetCurrentUser();
	$("#debugbox").append('Current user: ' + currUser+'<br/>');	

	// If group name selection has changed
	$("#groupselection").change(function(){
		var selectedGroupname = $("#groupselection").val();
		if (selectedGroupname != "")
		{
			if(CheckIsUserMemberOfGroup(selectedGroupname) == true)
			{ $("#debugbox").append('User ISMEMBER of group ' + selectedGroupname+'<br/>');
			} else {$("#debugbox").append('User is not member of group ' + selectedGroupname+'<br/>');}
		}
		else
			$("#debugbox").append('Group selection Empty');	
	});
});

function CheckIsUserMemberOfGroup(grouptocheck)
{
	var result = false;
	var selector = 'Group[Name=\'' + grouptocheck + '\']';
	$().SPServices({
		operation: "GetGroupCollectionFromUser",
		userLoginName: $().SPServices.SPGetCurrentUser(),
		async: false,
		completefunc: function(xData, Status) {
			if($(xData.responseXML).find(selector).length == 1) {
				result = true;
        }
      }
   });
   return result;
}
</script>

Open in new window

I filled the select options with SharePoint groups of my current site. Each time I change the option, another test is made.
HTH
Rainer
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question