Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sharepoint 事件接收器中的ItemAdded和Update处理程序中的UnauthorizedAccessException异常_Sharepoint_Sharepoint 2010_Sharepoint 2013_Unauthorizedaccessexcepti_Eventreceiver - Fatal编程技术网

Sharepoint 事件接收器中的ItemAdded和Update处理程序中的UnauthorizedAccessException异常

Sharepoint 事件接收器中的ItemAdded和Update处理程序中的UnauthorizedAccessException异常,sharepoint,sharepoint-2010,sharepoint-2013,unauthorizedaccessexcepti,eventreceiver,Sharepoint,Sharepoint 2010,Sharepoint 2013,Unauthorizedaccessexcepti,Eventreceiver,我拥有名为“site1”的SharePoint 2013网站集 我在该网站集中有一个列表和一个文档库,我编写了一个事件接收器将列表项附件移动到文档库中,移动列表项附件后,我用该文档URL更新该列表中的一个文件,然后删除该列表项的附件表单。下面是我正在使用的代码 public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); thi

我拥有名为“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);
                        }
                    }
                }
            }
        });
    }
添加了公共覆盖无效项(SPItemEventProperties属性) { 基本。添加的项目(属性); this.EventFiringEnabled=false; if(properties.List.Title.Equals(“ListName”,StringComparison.CurrentCultureIgnoreCase)) { 尝试 { SPSecurity.runWithLevelatedPrivileges(委托() { 移动附件(属性); 删除附件(属性); }); properties.ListItem.Update(); } 捕获(例外情况除外) { CreateLog.Create(例如StackTrace); CreateLog.Create(例如Message); } } this.EventFiringEnabled=true; } 公共覆盖无效项已更新(SPItemEventProperties属性) { 基本项目更新(属性); this.EventFiringEnabled=false; if(properties.List.Title.Equals(“ListName”,StringComparison.CurrentCultureIgnoreCase)) { 尝试 { SPSecurity.runWithLevelatedPrivileges(委托() { 移动附件(属性); 删除附件(属性); }); properties.ListItem.Update(); } 捕获(例外情况除外) { CreateLog.Create(例如StackTrace); CreateLog.Create(例如Message); } } this.EventFiringEnabled=true; } 公共无效移动附件(SPItemEventProperties属性) { 字符串siteURL=properties.Web.Url; SPSecurity.runWithLevelatedPrivileges(委托() { 使用(spsitetsite=newspsite(siteURL)) { 使用(SPWeb tWeb=tSite.OpenWeb()) { //移动租用请求附件 if(properties.List.Title.Equals(“ListName”,StringComparison.CurrentCultureIgnoreCase)) { 尝试 { SPList docDestination=tWeb.Lists[“LibraryName”]; SPFolder fldRoot=tWeb.Folders[docDestination.Title]; SPFileCollection flColl=null; SPList list=tWeb.Lists[“ListName”]; SPListItem listItem=properties.listItem; 如果(listItem.Attachments!=null&&listItem.Attachments.Count>0) { foreach(listItem.Attachments中的字符串strName) { flColl=fldRoot.Files; SPListItem ListItem=docDestination.Items.Add(); SPFile FileCopy=listItem.ParentList.ParentWeb.GetFile(listItem.Attachments.UrlPrefix+strName); 字符串扩展名=FileCopy.Name.Substring(FileCopy.Name.LastIndexOf('.'); 字符串文件名=listItem[“Title”].ToString().Replace(“,”); 字符串buildfilename=文件名+扩展名; 字符串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[SPBuiltInFieldId.Created]=Convert.ToDateTime(listItem[SPBuiltInFieldId.Created]); item[SPBuiltInFieldId.Modified]=Convert.ToDateTime(listItem[SPBuiltInFieldId.Modified]); flAdded.Item.Update(); listItem[“DocumentURL”]=siteURL+“/”+item.Url; Update(); } } } 捕获(例外情况除外) { CreateLog.Create(例如StackTrace); CreateLog.Create(例如Message); } } } } }); } 公共void DeleteAttachments(SPItemEventProperties属性) { 字符串siteURL=properties.Web.Url; SPSecurity.runWithLevelatedPrivileges(委托() { 使用(spsitetsite=newspsite(siteURL)) { 使用(SPWeb tWeb=tSite.OpenWeb()) { if(properties.List.Title.Equals(“ListName”,StringComparison.CurrentCultureIgnoreCase)) { 尝试 { SPListItem listItem=properties.listItem; 列表文件名=新列表(); 如果(listItem[“附件”]!=null) { foreach(listItem.Attachments中的字符串文件名) {
SPListItem listItem = properties.ListItem;
SPListItem listItem = tWeb.Lists[properties.ListId].GetItemById(properties.ItemId);