1

I need to hide "Approver" add "Title" fields in my SharePoint list. I used the following JS.

<script type="text/javascript" src=class="vglnk" 
href="https://...SiteAssets/jquery-
1.10.1.min.js"</script>

<script type="text/javascript" src= class="vglnk" href="https://...SiteAssets/jquery.SPServ
ices-2014.02.min.js"</script>
<script type="text/javascript">
$(document).ready(function() {
 $().SPServices({
  operation: "GetGroupCollectionFromUser",
  userLoginName: $().SPServices.SPGetCurrentUser(),
  async: false,
  completefunc: function(xData, Approver) {
  var xml = xData.responseXML.xml;

   //If the current User does belong to the group "Test Group"
    if (xml.search('Test Group') == -1)
     {
      //   alert("No, User Doesn't Exists!");                  
       $("select[title=Approver]").parent().parent().parent().hide(); 
       $("select[title=Title]").parent().parent().parent().hide(); 
       //or use: $('td.ms-formlabel:contains("Status")').parent().hide();
      //You can also use:$('nobr:contains("Status")').closest('tr').hide();
       }  
    }
 });
});
</script>
  • And your question is? Is there an error from your JS? – Mike Smith - MCT - MVP Jan 26 '18 at 22:29
  • It doesn't hide. Still exist – Jen Jan 27 '18 at 1:03
  • Which version of SharePoint 2013, 2016 or SharePoint Online? And, how did you add the JS to the page? – Mike Smith - MCT - MVP Jan 27 '18 at 1:19
  • Also, what happens when you use $("select[title=Approver]").parent().parent().parent().hide(); from the browser's F12 tools console? – Mike Smith - MCT - MVP Jan 27 '18 at 1:21
  • I used CEWP after I saved on the site asset SP10. Error:(Uncaught ReferenceError: $ is not defined$(document).ready(function() {) – Jen Jan 27 '18 at 23:41
1

Your code seems fine. You just need to specify with ifs which permissions requires the users that opens the list.

SPServices GetGroupCollectionFromUser() function gets all user's permissions, and in the if you just need to:

//Example
console.log(xml);
xml = ['Read', 'Contribute'];

//If the current User does NOT belong to the group "Contribute"
if (xml.search('Contribute') != -1){
    $("select[title=Approver]").parent().parent().parent().hide(); 
    $("select[title=Title]").parent().parent().parent().hide(); 
}
0

Make sure the Jquery and SPService library is loaded in the page correctly and here is the completed code which works:

<!-- jQuery Reference. You can refer it from Layouts Folder/Doc Library too, after uploading the script. -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<!-- Download SPServices from: http://spservices.codeplex.com/ Or use this CDN  -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
 $().SPServices({
  operation: "GetGroupCollectionFromUser",
  userLoginName: $().SPServices.SPGetCurrentUser(),
  async: false,
  completefunc: function(xData, Status) {
  var xml = xData.responseXML.xml;

   //If the current User does belong to the group "Service desk Operators"
    if (xml.search('Service Desk Operators') == -1)
     {
          //   alert("No, User Doesn't Exists!");                  
           $("select[title=Status]").parent().parent().parent().hide(); 
          //or use: $('td.ms-formlabel:contains("Status")').parent().hide();
          //You can also use:  $('nobr:contains("Status")').closest('tr').hide();
       }  
    }
 });
});
</script>
  • I tried the code that you attached. But, It doesn't hide the column. – Jen Jan 29 '18 at 19:55
  • Error:Uncaught TypeError: Cannot read property 'parent' of null at <anonymous>:1:28 (anonymous) @ VM252:1 – Jen Jan 30 '18 at 20:21

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.