Tuesday, July 19, 2016

Restricted Edit Event Receiver - SharePoint 2013

Scenario
  • You need to restrict edits to a document library based on the current value of a field.
  • Full trust farm solution for SharePoint 2013 scoped to Web.
Solution
  • I created a configurable event receiver that will restrict edits (metadata and content) based on the value in one of the document's metadata columns.
  • Additional feature: Delay implementation of the restriction for a number of seconds from Created Date to allow for additional automated processes to update new documents.
  • Additional feature: Exclude a document from the restriction based on a regular expression using its  filename.
  • Additional feature: Automatically populate the Title field (or other text field) based on another choice or text field.
  • Open-Source code is available here:  RestrictedSave ZIP
    • Feel free to modify and use this code.  You may not resell it or a modified version of it as part of a packaged solution without my permission.
  • Disclaimer:  This will only prevent edits that trigger the item updating event receiver.  Some programmatic and 3rd party API calls made using bulk edit operations, workflow actions, or other event receivers may bypass the event receiver.  In addition, this code is not hardened for high security scenarios and is only meant as a first line defense against unwanted typical user edits.
Installation
  • Download the RestrictedSave WSP file and deploy to your farm.
  • Enable the Restricted Save feature on your sub-site.
    • Feature Description:
      Configured via the RestrictedSave list. If this site does not have one, a RestrictedSave list will be added. Document libraries named in the RestrictedSave list will not save a file if the user is unauthorized and the assigned column contains a certain value. If no required permission is specified, then the field can only be edited if the restricted column's value is changed. Multiple document libraries, columns, or values may be configured but must exist in this subsite. Optional New File Delay and Regular Expression Exclusion may be configured as well. Will also set the document title and/or filename based on a choice field by using the 'Title Choice Field' columns.
  • Configure the RestrictedSave list that was added to the site according to your needs.
Example

  • A standard configuration of the RestrictedSave list to restrict edits on a document library named "RestrictDeleteTest" when the "LOBType" column is set to "Choice 1".

  • The resultant error displayed on a document's edit properties form in the "RestrictDeleteTest" library when a filename modification is attempted.


  • Error Message in clear text
UNABLE TO SAVE! -- When column "LOBType" is set to "Choice 1" the item is locked and cannot be updated. -- Please work from a new copy or change the column value.





RestrictedSave List Configurable Fields
  • Instructions are provided in-line on the form.
  • Add one line for each restriction.  All lines will be parsed for each document edited in the sub-site.

--------------------------------------------------------------------------------------


And again in text format for search-engines, visually impaired, and copy/paste:

Document Library Name *


Column Name


Column Value


Required Permission To Edit

   

New Item Delay Seconds


Title Exclusion Regex


Title Choice Field Name


Title Choice Target

   

1 comment: