繁体   English   中英

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

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

我喜欢在创建或编辑项目后自定义编辑用户的权限。

  1. 客户不接受为此使用工作流,因为有时工作流启动较晚。
  2. 我发现了一种Javascript方法:
    function PreSaveItem(){...}
    但是由于安全性,这不是我要寻找的东西,我仍然不认为您可以在javascript中更改用户的权限(我希望不是)。

我只想编辑NewForm.aspx并添加将在添加/编辑项目之前或之后立即执行的C#代码。

谢谢

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

您将必须等到创建该项目之后,再在其上创建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执行BreakRoleInheritance ,则用户可能没有对该项目的读取权限。 因此,可能会出现“访问被拒绝”错误。

编辑:当您要部署事件处理程序时,通常创建一个可以在Web范围内激活/停用的功能。 然后,您捕获“功能已激活”并调用如下函数:

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中执行此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

暂无
暂无

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

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