0

我有一个名为“site1”的 SharePoint 2013 网站集

我在该网站集中有一个列表和一个文档库,我编写了一个事件接收器来将列表项附件移动到文档库中,并且在移动列表项附件之后,我正在使用该文档 URL 更新该列表中的一个文件,之后我'正在删除该列表项的附件表单。下面是我正在使用的代码

public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);

        this.EventFiringEnabled = false;


        if (properties.List.Title.Equals("ListName", StringComparison.CurrentCultureIgnoreCase))
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {                       
                    MoveAttachments(properties);
                    DeleteAttachments(properties);                        
                });
                properties.ListItem.Update();
            }
            catch (Exception ex)
            {
                CreateLog.Create(ex.StackTrace);
                CreateLog.Create(ex.Message);
            }

        }

        this.EventFiringEnabled = true;
    }

public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);
        this.EventFiringEnabled = false;   
        if (properties.List.Title.Equals("ListName", StringComparison.CurrentCultureIgnoreCase))
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    MoveAttachments(properties);
                    DeleteAttachments(properties);
                });
                properties.ListItem.Update();
            }
            catch (Exception ex)
            {
                CreateLog.Create(ex.StackTrace);
                CreateLog.Create(ex.Message);
            }
        }


        this.EventFiringEnabled = true;
    }

public void MoveAttachments(SPItemEventProperties properties)
    {
        string siteURL = properties.Web.Url;
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite tSite = new SPSite(siteURL))
            {
                using (SPWeb tWeb = tSite.OpenWeb())
                {
                    //Move Hiring Req Attachments
                    if (properties.List.Title.Equals("ListName", StringComparison.CurrentCultureIgnoreCase))
                    {
                        try
                        {
                            SPList docDestination = tWeb.Lists["LibraryName"];
                            SPFolder fldRoot = tWeb.Folders[docDestination.Title];
                            SPFileCollection flColl = null;
                            SPList list = tWeb.Lists["ListName"];
                            SPListItem listItem = properties.ListItem;

                            if (listItem.Attachments != null && listItem.Attachments.Count > 0)
                            {
                                foreach (String strName in listItem.Attachments)
                                {
                                    flColl = fldRoot.Files;
                                    SPListItem listtem = docDestination.Items.Add();
                                    SPFile FileCopy = listItem.ParentList.ParentWeb.GetFile(listItem.Attachments.UrlPrefix + strName);
                                    string extention = FileCopy.Name.Substring(FileCopy.Name.LastIndexOf('.'));
                                    string fileName = listItem["Title"].ToString().Replace(" ", "_");

                                    string buildfilename = fileName + extention;
                                    string destFile = flColl.Folder.Url + "/" + buildfilename;
                                    byte[] fileData = FileCopy.OpenBinary();
                                    SPFile flAdded = flColl.Add(destFile, fileData, tWeb.CurrentUser, tWeb.CurrentUser, Convert.ToDateTime(listItem[SPBuiltInFieldId.Created]), Convert.ToDateTime(listItem[SPBuiltInFieldId.Modified]));
                                    SPListItem item = flAdded.Item;
                                    item[SPBuiltInFieldId.Created] = Convert.ToDateTime(listItem[SPBuiltInFieldId.Created]);
                                    item[SPBuiltInFieldId.Modified] = Convert.ToDateTime(listItem[SPBuiltInFieldId.Modified]);

                                    flAdded.Item.Update();

                                    listItem["DocumentURL"] = siteURL + "/" + item.Url;
                                    listItem.Update();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            CreateLog.Create(ex.StackTrace);
                            CreateLog.Create(ex.Message);
                        }
                    }
                }
            }                     
        });

    }


public void DeleteAttachments(SPItemEventProperties properties)
    {
        string siteURL = properties.Web.Url;

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite tSite = new SPSite(siteURL))
            {
                using (SPWeb tWeb = tSite.OpenWeb())
                {

                    if (properties.List.Title.Equals("ListName", StringComparison.CurrentCultureIgnoreCase))
                    {
                        try
                        {
                            SPListItem listItem = properties.ListItem;

                            List<string> fileNames = new List<string>();

                            if (listItem["Attachments"] != null)
                            {
                                foreach (string fileName in listItem.Attachments)
                                {
                                    fileNames.Add(fileName);
                                }
                                foreach (string fileName in fileNames)
                                {
                                    SPSecurity.RunWithElevatedPrivileges(delegate()
                                    {
                                        listItem.Attachments.Delete(fileName);
                                    });
                                }
                            }
                            listItem.Update();
                        }
                        catch (Exception ex)
                        {
                            CreateLog.Create(ex.StackTrace);
                            CreateLog.Create(ex.Message);
                        }
                    }
                }
            }
        });
    }

我必须共享点组称为“贡献者”和“用户”贡献者组具有编辑不删除权限,用户组仅具有向该列表添加权限。当贡献者组成员添加列表项时,代码工作正常,但是当用户组成员添加列表项时,它会引发以下错误。

at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex) at Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bPreserveItemUIVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bUnRestrictedUpdateInProgress, Boolean bMigration, Boolean bPublish, String bstrFileName, ISP2DSafeArrayWriter pListDataValidationCallback, ISP2DSafeArrayWriter pRestrictInsertCallback, ISP2DSafeArrayWriter pUniqueFieldCallback) at Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAfterEvents, String filename, Boolean bPreserveItemUIVersion) at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents, String filename, Boolean bPreserveItemUIVersion) at Microsoft.SharePoint.SPListItem.Update() at TA.Tech360.HD.HiringReqEventReciever.HiringReqEventReciever.<>c_DisplayClass24.b_23()-------->8/11/2013 7:03:23 AM

0x80070005-------->8/11/2013 7:03:23 AM

谁能帮我。

提前致谢。

4

1 回答 1

3

您需要从提升的网络获取您正在更新的所有内容。在 MoveAttachments 和 DeleteAttachments 方法中,更改以下行:

SPListItem listItem = properties.ListItem;

它检索 listItem不是作为管理员,而是作为当前用户(可能没有所需的权限),对此:

SPListItem listItem = tWeb.Lists[properties.ListId].GetItemById(properties.ItemId);

这将真正以管理员身份检索 listItem 。

于 2013-08-12T08:25:12.477 回答