tags:

views:

215

answers:

2

Hello, 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

A: 

Why not create an SPItemEventReceiver and bind that to the list / content type?

Colin
Thanks, i didnt know about it. I will search on how to do use SPItemEventReceiver in NewForm.aspx.
ajitdh
A: 

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/"&gt;       
</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
naivists
Thank you so much. But how do you bind list with this event? SPWeb web = SPContext.Current.Web; SPContentType ct = web.ContentTypes["MyList"]; SPEventReceiverDefinition eventReceiver= ct.EventReceivers.Add(); Here, ct is null, Mylist is a costum list, and web.ContentTypes doesnt have my list in its collection.
ajitdh
updated my answer to include the deployment instructions
naivists
Thanks. It would be better if i could do everything in my NewForm.aspx. Anyway, I will create a feature then. I hope it is easy to create a feature. :)
ajitdh
Thanks, I went to this folder to add the xml. "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES". But, in relation to assembly, It seems I have to develop an assembly and install it in gac, which is a big no no due to problem it will give for maintenance. is there anyway to know not to create your own assembly?
ajitdh
SPContext.Current.FormContext.OnSaveHandler = OnSave; This is what I was searching for.:) Thank you so much.
ajitdh