Improve Your Technology

Just another blog for techology

Changing page item level permissions using event reciever

Changing item level permissions for pages library

In order to change item level permission as and when item added to a list or library, the best approach we follow is

  1. Writing event receiver for the list on ItemAdded
  2. Executing permission change block with elevated privileges.

But the same approach is not going to work on pages (publishing sites) library. The main reason for failing this approach is

  1. To change the permissions we have to execute permission block with elevated privileges. This block will run with system account user.
  2. When you are creating item with in the elevated privileges block, you will get item not found or index out of rang as the item is not available for other users.
  3. To make item available to other users, we can check in item with minor version with in the ItemAdded event receiver and then execute elevated block.
  4. If you are checking item with minor version and if the page template associated with any content type and there are any required fields then check in will fail.

This is not a bug or issue for publishing application. Pages which are checked in or published will be available for other users so when you are trying to identify an item in run with elevate privileges block , you are getting file not found or index out of rang.

To overcome this issue

  1. If content type associates to the page temple don’t have any required field then
    1. In the ItemAdded event receiver, check in the page with minor version
    2. Modify user permission with in the elevated block
    3. Checkout the item with the current user.
    4. If content type associated to the page template have required fields then
      1. Create ItemCheckedIn event receiver
      2. Make sure this event receiver code run only once.
      3. Modify user permission with in the elevated block.
      4. Train content authors to check-in the page with minor version after page created and updated required field.

Sample code block

if (!properties.ListItem.HasUniqueRoleAssignments){

SPSecurity.RunWithElevatedPrivileges(delegate() {

using (SPSite site = new SPSite(properties.WebUrl)){

using (SPWeb web = site.OpenWeb()){

SPListItem page = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID);

page.BreakRoleInheritance(true);

}

}

}

}

October 12, 2012 - Posted by | Event Reciever, SharePoint, SharePoint 2010, Technology | , ,

No comments yet.

Leave a comment