Hide a field in SharePoint list if the current logged in user belongs to a specific group in SharePoint 2013 using jQuery.
Introduction
This article explains how to hide a field in SP2013 if the current logged in user belongs to a group.
Prerequisites
- Ensure you have a SharePoint site.
- jQuery JavaScript Library v1.8.3
- SPServices - Version 0.7.2 jquery
Scenario
Hide a field in SharePoint list if the current logged in user belongs to a specific group.
Use the following procedure:
- In our example we shall create a group and list that contains a field.
- Create a group called “Test Group”.
- Add your account to the group.
- Create a custom list called called Test.
- Add a field called Test as a single line of text.
- Now navigate to the NewForm.aspx page.
- Syntax: http://yoursitecollection/Lists/ListName/NewForm.aspx
In our example : http://yoursitecollection/Lists/Test/NewForm.aspx
- Goto settings and edit the page.
- Click on Add a Webpart and add Content Editor Webpart.
- Now add a reference to jQuery JavaScript Library v1.8.3 and SPServices - Version 0.7.2 jquery file.
<script type="text/javascript" src="/ sites/Devsite /SPServices.0.7.2.js"></script>
<script type="text/javascript" src="/sites/Devsite/jqueryv1.8.3.js"></script>
- Now add the following lines of code:
<script type="text/javascript">
$(document).ready(function () {
//get the field object
var testField = $("input[id^=Test]");
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function (xData, Status) {
var xml = xData.responseXML.xml;
if ($(xData.responseXML).find("Group[Name='Test Group']").length == 1) {
//current user belongs to group
testField.parent().parent().parent().hide();
}
}
});
});</script>
- Now click on ok and stop editing the webpart.
- That's it!
- Now let's start testing.
Testing
- Before adding the CEWP
- After Adding CEWP webpart
Summary
Thus in this article you saw how to hide a field based on a group in SharePoint 2013 using jQuery.