当项目添加到库时 SharePoint 运行方法 [英] SharePoint running a method when item added to a library

查看:30
本文介绍了当项目添加到库时 SharePoint 运行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库,我想在添加项目以填充一些附加列时运行一些代码.有什么办法可以让这段代码自动运行.我会去哪里添加它.也可以有一个屏幕来编辑代码的选项.只要指向正确的方向就好了.

I have a library and I want to run some code when an item is added to populate some additonal columns. Is there any way I can have this code run automcailly. Where would I go about adding it. Also would it be possible to have a screen to be able to edit the options of the code. Just some pointing in the right dirrection would be great.

这是创建工作流的案例吗?

Would this be a case of creating a workflow?

推荐答案

工作流不是解决问题的方法.您应该创建一个项目事件接收器.我这样说的原因是因为听起来您需要在添加项目后运行一些代码.由于您不需要维护状态,因此工作流不是解决此问题的正确方法.您可以这样做:在 Visual Studio 中创建一个新类,并让它从 SPItemEventReceiver 继承.覆盖 ItemAdded() 方法并将您的逻辑放在那里.例如:

Workflows are not the way to go about it. You should be creating an item event receiver. The reason I say this is because it sounds like you have a need for some code to just run after you add an item. Since you don't need to maintain state then workflows are not the proper solution to this problem. Here's what you do: create a new class in Visual Studio and have it inherit from SPItemEventReceiver. Override the ItemAdded() method and put your logic in there. Ex:

public class MyItemEventReceiver : SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        // do your stuff
    }
}

此代码将在添加项目后调用.如果您需要在添加项目之前运行代码,那么您将覆盖 ItemAdding() 方法.我公司部署事件接收器的方式有点不同,但这是使用 Elements.xml 的书本"方法:

This code will be called after an item is added. If you need to have code run before the item is added then you'll be overriding the ItemAdding() method. The way my company deploys event receivers is a bit different but this is the 'by the book' method using Elements.xml:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>My Event Receiver</Name>
      <Type>ItemAdding</Type>
      <SequenceNumber>1000</SequenceNumber>
      <Assembly>AssemblyName, Version=1.0.0.0, culture=neutral, PublicKeyToken=[token]</Assembly>
      <Class>Namespace.Class</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

您提到有一个屏幕来编辑代码的选项.听起来你正在接近另一罐蠕虫,所以我不能直接说.但是,如果您想传入与部署不同的各种选项,那么只需将这些选项放入上面的 标记中即可.然后,您可以从代码中的 properties.ReceiverData 属性访问您的选项.另外,请注意 <Filter> 标签没有任何作用 - 它们不是由 WSS 3 团队实现的.希望这会有所帮助.

You mentioned having a screen to edit options of the code. It sounds like you're verging on another can of worms so I can't speak directly to that. However, if you want to pass in various options that differ from deployment to deployment then just throw those into the <Data> tags above. Then you can access your options from the properties.ReceiverData property within your code. Also, please note that the <Filter> tags do nothing - they were not implemented by the WSS 3 team. Hope this helps.

这篇关于当项目添加到库时 SharePoint 运行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆