简体   繁体   English

Sharepoint-捕获NewForm.aspx / Edit.aspx的保存事件

[英]Sharepoint - Capture save event of NewForm.aspx/Edit.aspx

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: 我发现了一种Javascript方法:
    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). 但是由于安全性,这不是我要寻找的东西,我仍然不认为您可以在javascript中更改用户的权限(我希望不是)。

I just want to edit NewForm.aspx and add C# code that will be executed immediately before or just after item is added/edited. 我只想编辑NewForm.aspx并添加将在添加/编辑项目之前或之后立即执行的C#代码。

Thanks 谢谢

为什么不创建SPItemEventReceiver并将其绑定到列表/内容类型?

You will have to wait until the item has been created, and then BreakRoleInheritance() on it. 您将必须等到创建该项目之后,再在其上创建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
      }
  }

However, be aware that you will have some problems, if a user adds the item and then immediately tries to open the "DispForm.aspx". 但是,请注意,如果用户添加了项目然后立即尝试打开“ 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. 这是因为事件接收器在并行线程上工作,并且如果BreakRoleInheritance执行BreakRoleInheritance ,则用户可能没有对该项目的读取权限。 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. 编辑:当您要部署事件处理程序时,通常创建一个可以在Web范围内激活/停用的功能。 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. EDIT2:如果确实需要在newform.aspx中执行此newform.aspx ,则必须添加一些在页面中呈现的控件。 Inside that control, you set an 'OnSaveHandler" 在该控件内,您设置了一个“ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM