MOSS2007开发之旅——EventHandler

在实际项目中,我们经常会遇到这样一些需求,比如:删除文档库中一个文档时,要求发Mail通知相关人员;库存管理时,当入库时,添加某类产品和数量,库存也要相应增加某类产品和数量,当出库时,添加出库产品和数量,则库存应相应减少产品和数量。要实现这种需求,我们就要用到SP的EventHandler。

要使用EventHandler,需经历以下步骤:

  • 启动EventHandler支持
  • 编写EventHandler程序
  • 附加EventHandler程序

具体的操作步骤请参考高维鹏(Brian)SharePoint 2007图文开发教程(5)---体验Event Handler。这里我们看库存管理时怎么实现的,共享出来,大家可以举一反三。

案例描述

这里的库存管理例子,我们设计四个列表,产品类别(Category)、产品(Product)、入库产品(Import Product)、出库产品(Export Product)。产品类别包括Category Name和Remarks两个字段自定义字段;产品包括Product Name、Count和Remarks三个自定义字段。其实每个Item还有几个默认的字段如:Created(创建时间)、Created By(创建人)等等公用字段,这里我们将入库产品和出库产品的创建时间显示出来,大家可以在Action中修改当前View,我就截一个图给大家参考一下:

image

案例实现

我们创建四个EventHandler模板分别命名为Category、Product、ImportProduct、ExportProduct。模板默认创建4个文件ItemEventReceiver.cs、ItemEventReceiver.xml、ListEventReceiver.cs、ListEventReceiver.xml。其中ItemEventReceiver的签名是:

public class CategoryItemEventReceiver : SPItemEventReceiver
{
    public CategoryItemEventReceiver();
    public override void ItemAdded(SPItemEventProperties properties);
    public override void ItemAdding(SPItemEventProperties properties);
    public override void ItemAttachmentAdded(SPItemEventProperties properties);
    public override void ItemAttachmentAdding(SPItemEventProperties properties);
    public override void ItemAttachmentDeleted(SPItemEventProperties properties);
    public override void ItemAttachmentDeleting(SPItemEventProperties properties);
    public override void ItemCheckedIn(SPItemEventProperties properties);
    public override void ItemCheckedOut(SPItemEventProperties properties);
    public override void ItemCheckingIn(SPItemEventProperties properties);
    public override void ItemCheckingOut(SPItemEventProperties properties);
    public override void ItemDeleted(SPItemEventProperties properties);
    public override void ItemDeleting(SPItemEventProperties properties);
    public override void ItemFileConverted(SPItemEventProperties properties);
    public override void ItemFileMoved(SPItemEventProperties properties);
    public override void ItemFileMoving(SPItemEventProperties properties);
    public override void ItemUncheckedOut(SPItemEventProperties properties);
    public override void ItemUncheckingOut(SPItemEventProperties properties);
    public override void ItemUpdated(SPItemEventProperties properties);
    public override void ItemUpdating(SPItemEventProperties properties);
}

ListEventReceiver的签名是:

public class CategoryListEventReceiver : SPListEventReceiver
{
    public CategoryListEventReceiver();
    public override void FieldAdded(SPListEventProperties properties);
    public override void FieldAdding(SPListEventProperties properties);
    public override void FieldDeleted(SPListEventProperties properties);
    public override void FieldDeleting(SPListEventProperties properties);
    public override void FieldUpdated(SPListEventProperties properties);
    public override void FieldUpdating(SPListEventProperties properties);
}

看方法的命名都能看出来时什么意思,比如:当你添加一个产品时,你也要在库存中添加相应产品,则你应在ItemAdding中操作这段逻辑。如下代码:

public override void ItemAdding(SPItemEventProperties properties)
{
    using (SPWeb web = properties.OpenWeb())
    {
        SPList productList = web.Lists[Common.ProductListName];
        string productName = productList.Fields[Common.ProductFields.ProductName].StaticName;
        string contentType = productList.Fields[Common.ProductFields.ContentType].StaticName;
        string productNameValue = properties.AfterProperties[contentType].ToString();
        int importCount = Int32.Parse(properties.AfterProperties[Common.ProductFields.Count].ToString());

        SPQuery query = new SPQuery();
        query.Query = string.Format("<Where><Eq><FieldRef Name='{0}'/><Value Type='Text'>{1}</Value></Eq></Where>", productName, productNameValue);

        SPListItemCollection items = productList.GetItems(query);
        SPListItem item = null;
        if (items.Count > 0)
        {
            item = items[0];
            int productCount = Int32.Parse(item[Common.ProductFields.Count].ToString());

            item[Common.ProductFields.Count] = productCount + importCount;
            item.Update();
        }
        else
        {
            item = productList.Items.Add();
            item[Common.ProductFields.ContentType] = productNameValue;
            item[Common.ProductFields.ProductName] = productNameValue;
            item[Common.ProductFields.Count] = importCount;
            item.Update();
        }

        properties.AfterProperties[productName] = productNameValue;
    }
}

这里就不将代码一一贴出来了,我将所有代码打包提供下载,下面就看看运行效果:

我在Import Product列表中增加产品TV数量为3:

image

然后你单击Product列表,则产品TV也自动添加到Product列表中。

image

代码下载:Known.MOSS.Exercise.rar

posted @ 2009-11-02 09:59  known  阅读(2111)  评论(1编辑  收藏  举报