Solved

Check if Current user exist in sharepoint group in a site

Posted on 2013-05-18
5
3,113 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
5 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now