Introduction
This article explains how to restrict permission only for a specific group for a Discussion Content Type in SharePoint 2013 using jQuery.
Prerequisites
Ensure you have a SharePoint site.
The following is the scenario:
- To provide permission for a certain group for a Discussion Content Type in NewForm.aspx/EditForm.aspx in SharePoint 2013 list forms.
- Permission to add replies but not to create new list items for discussion board
Use the following procedure:
- Create a group called "Test Group".
- Remove your account from the Test Group.
- Create a Discussion Board called TestDisp.
- Now navigate to the NewForm.aspx page.
- Syntax: http://yoursitecollection/Lists/ListName/NewForm.aspx
In our example: http://yoursitecollection/Lists/TestDisp/NewForm.aspx
- Go to 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">
- function PreSaveAction()
- {
- var checkForGroup=false;
- checkForGroup= CheckIfuserBelongsToGroupOrNot();
- if(checkForGroup==true)
- {
-
- return true;
- }
- else
- {
- alert('Sorry! You do not have permission to create.Please contact site collection administrator for necessary permissions' );
- return false;
- }
- }
- function CheckIfuserBelongsToGroupOrNot()
- {
- $().SPServices({
- operation: "GetGroupCollectionFromUser",
- userLoginName: $().SPServices.SPGetCurrentUser(),
- async: false,
- completefunc: function (xData, Status) {
- var xml = xData.responseXML.xml;
-
- if($(xData.responseXML).find("Group[Name='Enter the Group Name Here ']").length == 1)
- {
-
- checkForGroup=true;
- }
- else
- {
- checkForGroup=false;
- }
- }
- });
- }
- </script>
- Now click on ok and stop editing the webpart.
- That's it!
- Now let's start testing.
Testing
- Enter a value and click on the save button.
- You will not be able to save the discussion item.
Summary
Thus in this article you saw how to add permission to add replies but not to create new list items for a discussion board.