0

I am using SharePoint 2013 and have added JavaScript to the default NEW item form so that I can restrict what specific Permissions Groups can input on my Issue log list.

However, my syntax is not working. The following script only allows my administration (user group) full rights and seems to ignore the Owner (user group).

Can anyone help?

    <script language="javascript" src="https://collaboration.louise.com/teams/00005/script/SPServices.js"></script>
<script>
 $(document).ready(function () {

if(checkrole('Owners' && 'Administration')) {

// Issue Summary - *Show*
$('nobr:contains("Issue Summary")').closest('tr').show();

// Description - *Show*
$('nobr:contains("Description")').closest('tr').show();

// Data Domain - *Show*
$("select[Title='Data Domain']").closest('tr').show();

 // Issue ID - *Show*
$('nobr:contains("Issue ID")').closest('tr').show();

 // Work Package ID - *Show*
$('nobr:contains("Work Package ID")').closest('tr').show();

// Issue Score - *Show*
$('nobr:contains("Issue Score")').closest('tr').show();

// Priority - *Show* 
$('nobr:contains("Priority")').closest('tr').show();

 }

 else {

// Issue Score - *Hide*
$('nobr:contains("Issue Score")').closest('tr').hide();

// Priority - *Hide* 
$('nobr:contains("Priority")').closest('tr').hide();

 }

function checkrole(groupname) {
     var IsvalidRet = false;
     $().SPServices({
         operation: "GetGroupCollectionFromUser",
         userLoginName: $().SPServices.SPGetCurrentUser(),
         async: false,
         completefunc: function (xData, Status) {

            if ($(xData.responseXML).find("Group[Name='"+groupname+"']").length == 1) {
                 IsvalidRet = true;
             }
         }
     });
     return IsvalidRet;
 }

});
 </script>
0

It's hard to say what the error can be by just looking at the code, and not knowing what error you are getting or which part is failing.

But I can tell you that you call checkrole wrongly, you pass in the result of a conditional ('Owners' && 'Administration'), while you probably want to do checkrole('Owners') && checkrole('Administration') - although this won't work since it's asynchronous.

So either use Promises, or a callback-function. For example checkrole could accept a "cb" variable, then you could nest them.

  • Thank You. The script is not failing completely as it is allowing the Administration group full access but hides the specified fields from all other groups including Owners unfortunately. I've not used the Promise or Callback functions but I will give them a try. thanks again – Louise13 Feb 14 '17 at 15:05
  • @Louise13 A callback could look like this: gist.github.com/eirikb/0098ae3c74544633a5eb9e3f857d74ab – eirikb Feb 14 '17 at 15:11
0

You can do without the whole checkrole( ) script if you do it the SharePoint way

Put an extra CEWP on the page with a Target Audience set to the Groups you want it executed for.

So hide the fields by default, the CEWP (with Target Audience!) will execute the script for admins and display the admin fields.

If you do want a scripting solution you might be able to use

What exactly is the webPermMasks property?

That information is available by default, so no need for another call to the server asking for User Permissions.

SP.PageContextInfo.get_webPermMasks().has( SP.PermissionKind.editListItems );

Will return true for everyone with list edit rights

Note: This does not have Group info, That's why the Targetted Audience apporach is so convenient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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