Using SPServices to disable a field if a user is not a member of a specified SharePoint group

Mar 15, 2013 at 6:34 PM
Working in SharePoint 2010 Foundation, I'm trying to disable a field on a custom editform.aspx for users who are not members of a specific group.

So, if current user is not a member of the group "Change Control - Admins", disable the CCID field.

I found what seemed to be the solution in the SPServices codeplex discussion area, but it doesn't work as expected. Not sure what I'm missing here.

It does disable the correct field, but does not matter if current user is in the specified group or not.

If i change this:

if($(xData.responseXML).find("Group[Name='Change Control - Admins']").length != 1)

to this:

if($(xData.responseXML).find("Group[Name='Change Control - Admins']").length == 1)

...then the field does not get disabled. However, it again doesn't matter if the current user is in the specified group or not.

Any help would be much appreciated.

Here's my code:
<script language="javascript" type="text/javascript" src="/CodeLibrary/jquery-1.7.2.min.js"></script>
<script language="javascript" type="text/javascript" src="/CodeLibrary/jquery.SPServices-0.7.2.min.ssl.js"></script>
<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='Change Control - Admins']").length != 1)  
        {  
              //...disable the following fields
              $("input[Title='CCID']").prop("disabled", "disabled");
              }  
      }  
    });  
});

function PreSaveAction() {
    $("input[Title='CCID']").removeProp("disabled");
    return true;
}
</script>
Coordinator
Mar 15, 2013 at 6:41 PM
You should look at the full response for the user and see what it looks like. It'll take some debugging with the IE Developer Tools or Firebug.

M.
Mar 15, 2013 at 7:26 PM
try...
if($(xData.responseXML).find("Group[Name='Change Control - Admins']").length == 0)
Members not in the group 'Change Control - Admins' will return the 0 length. That has always worked for me.
Mar 15, 2013 at 8:27 PM
Sympmarc - thanks for the tip. I do have IE Developer installed. Not quite sure how to look at the full response though.
Mar 15, 2013 at 8:29 PM
Gkoliver - thanks, but that does make it dissabled, but it's still disabled whether my user is a member of the specified group or not....which I think is the real problem here.
Coordinator
Mar 15, 2013 at 8:47 PM
It's easier to look at the returned XML with Firebug or Fiddler. You can always do:
alert((xData.responseText);
but with this Web Service there's too much data to see everything.

M.