2010-01-11 39 views
0

的捕獲保存事件我想創建或編輯一個項目後,編輯定製用戶的權限。的Sharepoint - NewForm.aspx/Edit.aspx

  1. 爲此使用工作流程未被客戶端接受,因爲有時候工作流程啓動遲。
  2. 我發現一個JavaScript方法:
    function PreSaveItem(){...}
    但是,這不是我所期待的,由於安全性,我仍然不認爲你可以改變用戶在JavaScript許可(我希望不是)。

我只想編輯NewForm.aspx並添加將在添加/編輯項目之前或之後立即執行的C#代碼。

感謝

回答

0

你將不得不等待,直到該項目已被創建,然後在其上BreakRoleInheritance()

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 
     } 
    } 

但是,要知道,你將有一些問題,如果用戶增加了項目,然後立即嘗試打開「DispForm.aspx」。這是因爲該事件接收器工作在一個並行線程,如果該BreakRoleInheritance在那一刻進行,用戶可能沒有到該項目的讀取訪問。因此,可能會出現「訪問被拒絕」錯誤。

編輯:當你要部署事件處理程序,您通常會創建可激活功能/在網絡範圍內停用。然後,您趕上「功能激活」,並呼籲這樣的功能:

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 

的功能可以被定義爲:

<?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:如果你真的必須這樣做,在newform.aspx,你必須添加一些在頁面中呈現的控件。該控件在裏面,你設置的「OnSaveHandler」

SPContext.Current.FormContext.OnSaveHandler = AddressOf onSave 

然後實現自己的保存功能:

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 
+0

太謝謝你了,但你怎麼綁定列表與此事件的SPWeb網站= SPContext.Current.Web;?SPContentType克拉= web.ContentTypes [ 「MYLIST」]; SPEventReceiverDefinition eventReceiver = ct.EventReceivers.Add() ;在這裏,CT爲null,MYLIST是costum列表,web.ContentTypes不必須在其收集我的名單 – ajitdh 2010-01-11 09:22:49

+0

更新了我的答案,包括部署。說明 – naivists 2010-01-11 09:32:08

+0

謝謝。如果我可以在我的NewForm.aspx中做所有事情會更好。無論如何,我會創建一個功能。我希望創建一個功能很容易。 :) – ajitdh 2010-01-11 09:36:56

0

爲什麼不創建一個SPItemEventReceiver並綁定該到列表/內容類型?

+0

謝謝,我不知道這件事,我會尋找關於如何做到使用SPItemEventReceiver在NewForm 。的.aspx – ajitdh 2010-01-11 09:02:31