R – Sharepoint – Capture save event of NewForm.aspx/Edit.aspx

sharepoint

I like to custom edit the permission of users after creating or editing an item.

  1. Using workflow for that was not accepted by client because sometime workflow is late to start.
  2. I found a Javascript approach of:
    function PreSaveItem(){...}
    But this not what I am looking for due to security, and I still dont think you can change permission of user in javascript (I hope not).

I just want to edit NewForm.aspx and add C# code that will be executed immediately before or just after item is added/edited.

Thanks

Best Answer

You will have to wait until the item has been created, and then BreakRoleInheritance() on it.

public class MyListHandler : Microsoft.SharePoint.SPItemEventReceiver
{
 public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);

        properties.ListItem.BreakRoleInheritance(false);
        //you will have to add the required permissions here
      }
  }

However, be aware that you will have some problems, if a user adds the item and then immediately tries to open the "DispForm.aspx". This is because the event receiver works on a parallel thread and, if the BreakRoleInheritance is performed at that moment, the user may not have read access to the item. Hence, the "Access denied" error may appear.

EDIT: When you want to deploy your event handler, you typically create a feature that can be activated/deactivated at the web scope. You then catch the "feature activated" and call a function like this:

Public Sub AddEventHandlerToList( _
          ByVal opList As SPList, _
          ByVal spAssemblyName As String, _
          ByVal spClassName As String, _
          ByVal ipType As SPEventReceiverType)
    opList.EventReceivers.Add(ipType, spAssemblyName, spClassName)
    opList.Update()
End Sub

The feature can be defined as:

<?xml version="1.0" encoding="utf-8"?>
 <Feature  Id="{ABABABE1-1234-5678-9012-345678912345}"
      Title="MySuperFeature
      Description="Something more descriptive here"
      Scope="Web"
      DefaultResourceFile="core"
      ReceiverAssembly="your.assembly.name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=546479a7bab11231"
      ReceiverClass="your.namespace.MyListHandler"   
      xmlns="http://schemas.microsoft.com/sharepoint/">       
</Feature>

EDIT2: if you really have to do it in the newform.aspx, you have to add some control that is rendered in the page. Inside that control, you set an 'OnSaveHandler"

 SPContext.Current.FormContext.OnSaveHandler = AddressOf onSave

Then, implement your own save function:

Public Sub onSave(ByVal sender As Object, ByVal e As EventArgs)
    Dim sRedirectUrl As String
    Dim operation As SPLongOperation = Nothing
        operation = New SPLongOperation(Me.Page)
        operation.Begin()

        If SaveButton.SaveItem(SPContext.Current, False, "") Then
            sRedirectUrl = SPUrlUtility.CombineUrl(SPContext.Current.Site.Url, SPContext.Current.List.Forms.Item(PAGETYPE.PAGE_DISPLAYFORM).Url)
            sRedirectUrl &= "?ID=" & SPContext.Current.Item.ID
        End If

        SPContext.Current.Item.BreakRoleInheritance(false);

        operation.End(sRedirectUrl)
End Sub