可视化webpart基础开发——利用事件接收器实现同步操作两个文档库(添加、删除、修改文档)
1、测试文档库(Doclib1、Doclib2):
增加一栏“测试栏1”。
2、新建“可视化web部件项目”,添加“解决方案资源管理器”里边选中项目右键“添加”-“新建项”-"事件接收器“
如图操作,选择”列表项事件“和”文档库“集相应处理事件
实现代码:
- using System;
- using System.Security.Permissions;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Security;
- using Microsoft.SharePoint.Utilities;
- using Microsoft.SharePoint.Workflow;
- namespace TestFileReceiver.EventReceiverTest
- {
- /// <summary>
- /// 列表项事件
- /// </summary>
- public class EventReceiverTest : SPItemEventReceiver
- {
- /// <summary>
- /// 正在添加项.
- /// </summary>
- public override void ItemAdding(SPItemEventProperties properties)
- {
- base.ItemAdding(properties);
- }
- /// <summary>
- /// 正在更新项.
- /// </summary>
- public override void ItemUpdating(SPItemEventProperties properties)
- {
- base.ItemUpdating(properties);
- }
- /// <summary>
- /// 正在删除项.
- /// </summary>
- public override void ItemDeleting(SPItemEventProperties properties)
- {
- string url = properties.OpenWeb().Url;
- using (SPSite site = new SPSite(url))
- {
- using (SPWeb web = site.OpenWeb())
- {
- if (properties.ListTitle == "Doclib1")//判断操作的文档库是否是Doclib1
- {
- if (properties.ListItem.FileSystemObjectType == SPFileSystemObjectType.File)//判断增加的项的类型是否是文件
- {
- SPFolder tagetfolder = web.Folders["Doclib2"];
- SPFile file = properties.ListItem.File;
- string filename = properties.ListItem.Name;
- for (int i = 0; i < tagetfolder.Files.Count; i++)
- {
- string spfilename = tagetfolder.Files[i] == null ? "" : tagetfolder.Files[i].Name;
- if(filename==spfilename)
- {
- tagetfolder.Files[i].Delete();
- }
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 已添加项.
- /// </summary>
- public override void ItemAdded(SPItemEventProperties properties)
- {
- string url = properties.OpenWeb().Url;
- using(SPSite site=new SPSite (url))
- {
- using (SPWeb web = site.OpenWeb())
- {
- if (properties.ListTitle == "Doclib1")//判断操作的文档库是否是Doclib1
- {
- if (properties.ListItem.FileSystemObjectType == SPFileSystemObjectType.File)//判断增加的项的类型是否是文件
- {
- SPFolder tagetfolder = web.Folders["Doclib2"];
- SPFile file = properties.ListItem.File;
- string filename = properties.ListItem.Name;
- file.CopyTo(url+"/"+tagetfolder.Url+"/"+filename);
- }
- }
- }
- }
- }
- /// <summary>
- /// 已更新项.
- /// </summary>
- public override void ItemUpdated(SPItemEventProperties properties)
- {
- string url = properties.OpenWeb().Url;
- using (SPSite site = new SPSite(url))
- {
- using (SPWeb web = site.OpenWeb())
- {
- if (properties.ListTitle == "Doclib1")//判断操作的文档库是否是Doclib1
- {
- if (properties.ListItem.FileSystemObjectType == SPFileSystemObjectType.File)//判断增加的项的类型是否是文件
- {
- SPFolder tagetfolder = web.Folders["Doclib2"];
- SPListItem item = properties.ListItem;
- item.CopyTo(url + "/" + tagetfolder.Url + "/" + item.Name);
- }
- }
- }
- }
- }
- /// <summary>
- /// 已删除项.
- /// </summary>
- public override void ItemDeleted(SPItemEventProperties properties)
- {
- base.ItemDeleted(properties);
- }
- }
- }
部署测试如下
操作Doclib1文档库,上传修改文档
查看Doclib2文档库,相应文档也已上传修改
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。