Permission to Add Replies to List Items For Discussion List in SharePoint 2013 Using jQuery

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:

  1. Create a group called "Test Group".
  2. Remove your account from the Test Group.
  3. Create a Discussion Board called TestDisp.

    TestDisp

  4. Now navigate to the NewForm.aspx page.
  5. Syntax: http://yoursitecollection/Lists/ListName/NewForm.aspx
    In our example: http://yoursitecollection/Lists/TestDisp/NewForm.aspx
  6. Go to settings and edit the page.

    edit the page

  7. Click on Add a Webpart and add Content Editor Webpart.

    Webpart

  8. Now add a reference to jQuery JavaScript Library v1.8.3 and SPServices - Version 0.7.2 jQuery file.
    1. <script type="text/javascript" src="/ sites/Devsite /SPServices.0.7.2.js"></script>  
    2. <script type="text/javascript" src="/sites/Devsite/jqueryv1.8.3.js"></script>  
  9. Now add the following lines of code:

    1. <script type="text/javascript">       
    2. function PreSaveAction()  
    3. {       
    4.     var checkForGroup=false;  
    5.  checkForGroup= CheckIfuserBelongsToGroupOrNot();  
    6.     if(checkForGroup==true)  
    7.     {  
    8. //current user has permissions.So the user can create the discussion item.  
    9.         return true;  
    10.     }  
    11.     else  
    12.     {  
    13.         alert('Sorry! You do not have permission to create.Please contact site collection administrator for necessary permissions' );  
    14.         return false;  
    15.     }   
    16.  }  
    17. function CheckIfuserBelongsToGroupOrNot()  
    18. {  
    19.     $().SPServices({  
    20.         operation: "GetGroupCollectionFromUser",  
    21.         userLoginName: $().SPServices.SPGetCurrentUser(),  
    22.         async: false,  
    23.         completefunc: function (xData, Status) {  
    24.             var xml = xData.responseXML.xml;  
    25. //In Our Example its Test Group.  
    26.             if($(xData.responseXML).find("Group[Name='Enter the Group Name Here ']").length == 1)  
    27.             {  
    28.                 //current user belongs to group  
    29.                 checkForGroup=true;                     
    30.             }  
    31.             else  
    32.             {  
    33.                 checkForGroup=false;  
    34.             }  
    35.         }  
    36.     });  
    37. }  
    38. </script>  
  10. Now click on ok and stop editing the webpart.
  11. That's it!
  12. Now let's start testing.

Testing

  1. Enter a value and click on the save button.

    Testing

  2. You will not be able to save the discussion item.

    Save

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.

Up Next
    Ebook Download
    View all
    Learn
    View all