Friday, February 28, 2014

Custom Permissions on SharePoint List/Library View

SharePoint any version does not offer permissions at a view level. You can have item level permissions, but not view level permissions. There is no easy way or server side method available.

I had a situation where it would be nice if  I could permissions at the view level. Over a year ago, I had a similar problem, but that time I told the client there was no way to set permissions at the view.

Here is a solution to set permissions at a view level. I will take a pages library in my example. In this example, if the current user does not belong to site owners group, they would be redirected to home page.

Assumptions

  • Pages Library provisioned
Solution
  1. Create a new .JS file and add it to anywhere on the site (Hint: SiteAssets)
  2. Add code like this below
  3. Goto AllItems.aspx page/view and click edit.
  4. Add a CE to the page.
  5. Link the CE to this .JS file. (That is all required)
Note: Make sure you are using the right version of SPService and Jquery as they seem to have compatibility issues.


<script src="http://code.jquery.com/jquery-1.11.0.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.SPServices-0.7.1a.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
//Do this for All Documents Views. The regular user does not need to view "All Documents" view.
  $().SPServices({
           operation: "GetGroupCollectionFromUser",
           userLoginName: $().SPServices.SPGetCurrentUser(),
           async: false,
           completefunc: function (xData, Status) {
               if ($(xData.responseXML).find("Group[Name='Site Owners']").length != 1) {
location.href = location.href.replace('/Forms/AllItems.aspx', '/Default.aspx');
               }
             
           }
       });
        


});


No comments:

Post a Comment