【问题标题】:UnauthorizedAccessException exception in ItemAdded and Updated handler in event receiver事件接收器中的 ItemAdded 和 Updated 处理程序中的 UnauthorizedAccessException 异常
【发布时间】:2013-08-12 16:25:12
【问题描述】:

我有一个名为“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);
                        }
                    }
                }
            }
        });
    }

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

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-------->2013 年 8 月 11 日上午 7:03:23

谁能帮帮我。

提前致谢。

【问题讨论】:

    标签: sharepoint sharepoint-2010 sharepoint-2013 unauthorizedaccessexcepti eventreceiver


    【解决方案1】:

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

    SPListItem listItem = properties.ListItem;
    

    NOT 以管理员身份但以当前用户(可能没有所需权限)的身份检索 listItem,对此:

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

    这会以管理员身份REALLY检索listItem。

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      相关资源
      最近更新 更多