SharePoint 2010事件接收器,列表项事件,文档库,事件ItemAdded未触发

时间:2012-04-30 01:58:08

标签: sharepoint-2010

在SharePoint Server 2010中,我有一个文档库,我想在每次创建文件夹时创建子文件夹,我有一个代码片段但它不起作用,我试图调试但事件没有触发,可能任何人请帮助我,这是我的代码:

public class EventReceiver1 : SPItemEventReceiver
{
    /// <summary>
    /// An item was added.
    /// </summary>
    private string[] subFolders = new string[] { "sub-folder1", "sub-folder2", "sub  folder3" };
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        SPWeb web = properties.OpenWeb();
        SPDocumentLibrary ProductsLibrary = (SPDocumentLibrary)web.Lists[properties.ListId];
        if (properties.ListItem.ContentType.Name.ToLower() == "new content type" && properties.ListItem.Folder.ParentFolder.ToString() == ProductsLibrary.RootFolder.ToString())
        {
            string Url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
            SPFolder libFolder = ProductsLibrary.RootFolder.SubFolders[properties.ListItem.Name];
            string newFolderUrl = (web.Url + "/" + libFolder.ToString());
            foreach (string subfolder in subFolders)
            {
                SPListItem newSubFolder = ProductsLibrary.Items.Add(newFolderUrl, SPFileSystemObjectType.Folder, subfolder);
                newSubFolder.Update();
            }
        }
    }
}

谢谢

2 个答案:

答案 0 :(得分:0)

解决方案是打开elements.xml并替换 使用和代码将完美运行。

答案 1 :(得分:0)

确保

  1. 您已将此事件接收器添加为功能的一部分
  2. 激活包含此事件接收器的功能
  3. 如果您没有按照上述步骤

    ,事件不会触发