Sharepoint 2010事件接收器仅适用于管理员帐户

时间:2012-11-14 10:16:32

标签: sharepoint eventreceiver

我有一个事件接收器,当用户创建文件夹时会自动创建13个子文件夹。以下代码可以执行此操作,当管理员帐户创建文件夹时一切正常。但是当涉及到其他用户时,不会在任何错误中创建子文件夹。

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);
    SPWeb web = properties.OpenWeb();
    web.AllowUnsafeUpdates = true;
    SPDocumentLibrary docLib = (SPDocumentLibrary)web.List[properties.ListId];
    if (properties.ListItem.ContentType.Name == "Folder"
        && properties.ListItem.Folder.ParentFolder.ToString().ToLower().EndsWith("opportunity"))
    {
        String url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
        SPFolder libFolder = properties.ListItem.Folder;
        string newFolderUrl = (web.Url + "/" + libFolder.ToString());
        foreach (string subfolder in folders)
        {
            SPListItem newFolder = docLib.Items.Add(newFolderUrl, SPFileSystemObjectType.Folder, subfolder);
            newFolder.Update();
        }
    }
}

我阅读了许多关于提升SPSite和SPWeb的帖子,并在创建子文件夹之前编写了一个代码来提升对象。但是,下面的代码不起作用:

public override void ItemAdded(SPItemEventProperties properties)
{
    Guid siteId = properties.SiteId;
    Guid webId = properties.OpenWeb().ID;
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite elevatedSite = new SPSite(siteId))
        {
            using (SPWeb web = elevatedSite.OpenWeb(webId))
            {
                base.ItemAdded(properties);
                web.AllowUnsafeUpdates = true;
                SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists[properties.ListId];
                if (properties.ListItem.ContentType.Name == "Folder"
                     && properties.ListItem.Folder.ParentFolder.ToString().ToLower().EndsWith("opportunity"))
                {
                    String url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
                    SPFolder libFolder = docLib.RootFolder.SubFolders[properties.ListItem.Name];
                    SPFolder libFolder = properties.ListItem.Folder;

                    string newFolderUrl = (web.Url + "/" + libFolder.ToString());
                    foreach (string subfolder in folders)
                    {
                        SPListItem newFolder = docLib.Items.Add(newFolderUrl, SPFileSystemObjectType.Folder, subfolder);
                        newFolder.Update();
                    }
              }
              web.AllowUnsafeUpdates = false;
            }
         }
    });
} 

有人可以帮忙解决问题吗? 请注意,所有用户都是主页成员,可以手动创建文件夹。

1 个答案:

答案 0 :(得分:2)

尝试拨打ItemAdding

在调用方法(与当前登录用户同步)时调用

ItemAdding,在执行方法后调用 - 与SharePoint \ System用户异步,通常是工人流程。

也许这会有所帮助。