Tuesday 25 December 2012

Hide a SharePoint list field based on user group using jQuery

I was Trying to hide add new link of SharePoint dataview webpart.
After search, I found these two solutions:
Note:- Please note, to get Group name using spservice, you must have permission to get view group at least.
Otherwise, It will throw error.
download jquery-1.8.2.min.js from here
and jquery.SPServices-0.7.2.min.js from here

<script type="text/javascript" src="../Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.SPServices-0.7.2.min.js"></script>
<script language="javascript" type="text/javascript">
var newLinkFld = $(".ms-addnew");
$(document).ready(function() {
  $().SPServices({
    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: false,
       completefunc: function (xData, Status) {
       var xml = xData.responseXML.xml;
       if (xml.search('GroupNameHere') != -1)
  {
          newLinkFld.parent().parent().parent().show();      
  }
  else
  {
  newLinkFld.parent().parent().parent().hide();
  }
     }
  });
});
</script>

OR

<script type="text/javascript" src="../Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.SPServices-0.7.2.min.js"></script>
<script language="javascript" type="text/javascript">
var newLinkFld = $(".ms-addnew");
$(document).ready(function() {

   $().SPServices({
     operation: "GetGroupCollectionFromUser",
     userLoginName: $().SPServices.SPGetCurrentUser(),
     async: false,
     completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='GroupNameHere']").length == 1)
        {
           newLinkFld.parent().parent().parent().show(); 
        }
        else
        {
          newLinkFld.parent().parent().parent().hide();
        }
      }
   });
});
</script>



No comments :

Post a Comment