新建文件夹的时候自动按照文档库模板生成子文件夹和文件的解决代码
应用情景:用户希望在新建文件夹的时候,自动在该文件夹下生成子文件夹和文件,子文件夹和文件放在一个文档库中
解决方法:应用Event Handler,不多说,直接上代码
解决方法:应用Event Handler,不多说,直接上代码
Code
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace ***.***.***
{
public class DocumentHandler : SPItemEventReceiver
{
//功能:新建文件夹的时候在新建的文件夹下面自动生成子文件夹和文件,子文件夹和文件放在一个模板文档库中
public override void ItemAdded(SPItemEventProperties properties)
{
//base.ItemAdded(properties);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.OpenWeb().ID))
{
try
{
//得到模板文档库
SPDocumentLibrary templeteDocLib = (SPDocumentLibrary)web.Lists["项目验收文档模板"];
//得到操作的文档库
SPDocumentLibrary targetDocLib = (SPDocumentLibrary)web.Lists[properties.ListId];
//得到新建文件夹的路径地址
string targetDoclibPath = properties.WebUrl + "/" + properties.AfterUrl + "/";
web.AllowUnsafeUpdates = true;
//新建文件夹的时候和判断是在操作的文档库根目录下新建文件夹的情况下才进行操作
if ((properties.ListItem.ContentType.Name == "文件夹" || properties.ListItem.ContentType.Name == "项目文件夹") && properties.ListItem.Folder.ParentFolder.ToString() == targetDocLib.RootFolder.ToString())
{
//设置项目文件夹的权限为某些人才能查看、编辑、删除等操作。
if (!properties.ListItem.HasUniqueRoleAssignments)
{
properties.ListItem.BreakRoleInheritance(false);
}
else
{
//后来用下面这个,当第一次更新完后,item有了自己的权限, HasUniqueRoleAssignments是1
//先重新继承上级权限,再去除所有权限重新分配
properties.ListItem.ResetRoleInheritance();
properties.ListItem.BreakRoleInheritance(false);
}
SPUser user = web.Users.GetByID(properties.CurrentUserId);
//获得触发此事件的用户的登录名
SPRoleAssignment ra1 = new SPRoleAssignment(web.EnsureUser(user.LoginName));
ra1.RoleDefinitionBindings.Add(web.RoleDefinitions["完全控制"]);
properties.ListItem.RoleAssignments.Add(ra1);
// 下面的是给单独用户分配完全控制权限
//首先获得项目负责人的值,原始的值是这样的:0758#km\test,所以要处理出来
string projectMan = properties.ListItem["项目负责人"].ToString();
string[] userID = projectMan.Split('#');
SPRoleAssignment ra3 = new SPRoleAssignment(web.EnsureUser(userID[1]));
ra3.RoleDefinitionBindings.Add(web.RoleDefinitions["完全控制"]);
properties.ListItem.RoleAssignments.Add(ra3);
Copy(web, templeteDocLib, templeteDocLib.RootFolder, 0, targetDoclibPath);
//资料上说ItemUpdated会执行10次,用下面这个方法解决
this.DisableEventFiring();
//设置新建立的文件夹审批状态为通过
//properties.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
properties.ListItem.Update();
this.EnableEventFiring();
targetDocLib.Update();
}
//SPFolder newFolder = web.GetFolder(targetDoclibPath);
//newFolder
}
catch (Exception ex)
{
}
}
}
});
}
//遍历文件夹和文件,并执行建立文件夹和拷贝文件操作
static void Copy(SPWeb web, SPDocumentLibrary doclib, SPFolder folder, int level, string targetDoclib)
{
SPListItemCollection items = doclib.GetItemsInFolder(doclib.DefaultView, folder);
foreach (SPListItem item in items)
{
if (item.FileSystemObjectType == SPFileSystemObjectType.Folder)
{
SPFolder targetFolder = web.GetFolder(targetDoclib);
SPFolder newfolder = targetFolder.SubFolders.Add(item.Name);
newfolder.Item.Update();
//复制该目录下的文件到目标文件夹
SPFileCollection spFiles = item.Folder.Files;
for (int i = spFiles.Count - 1; i > -1; i--)
{
spFiles[i].CopyTo(targetDoclib + item.Name + "/" + spFiles[i].Name);
}
Copy(web, doclib, item.Folder, level + 1, targetDoclib + "/" + item.Name);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace ***.***.***
{
public class DocumentHandler : SPItemEventReceiver
{
//功能:新建文件夹的时候在新建的文件夹下面自动生成子文件夹和文件,子文件夹和文件放在一个模板文档库中
public override void ItemAdded(SPItemEventProperties properties)
{
//base.ItemAdded(properties);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.OpenWeb().ID))
{
try
{
//得到模板文档库
SPDocumentLibrary templeteDocLib = (SPDocumentLibrary)web.Lists["项目验收文档模板"];
//得到操作的文档库
SPDocumentLibrary targetDocLib = (SPDocumentLibrary)web.Lists[properties.ListId];
//得到新建文件夹的路径地址
string targetDoclibPath = properties.WebUrl + "/" + properties.AfterUrl + "/";
web.AllowUnsafeUpdates = true;
//新建文件夹的时候和判断是在操作的文档库根目录下新建文件夹的情况下才进行操作
if ((properties.ListItem.ContentType.Name == "文件夹" || properties.ListItem.ContentType.Name == "项目文件夹") && properties.ListItem.Folder.ParentFolder.ToString() == targetDocLib.RootFolder.ToString())
{
//设置项目文件夹的权限为某些人才能查看、编辑、删除等操作。
if (!properties.ListItem.HasUniqueRoleAssignments)
{
properties.ListItem.BreakRoleInheritance(false);
}
else
{
//后来用下面这个,当第一次更新完后,item有了自己的权限, HasUniqueRoleAssignments是1
//先重新继承上级权限,再去除所有权限重新分配
properties.ListItem.ResetRoleInheritance();
properties.ListItem.BreakRoleInheritance(false);
}
SPUser user = web.Users.GetByID(properties.CurrentUserId);
//获得触发此事件的用户的登录名
SPRoleAssignment ra1 = new SPRoleAssignment(web.EnsureUser(user.LoginName));
ra1.RoleDefinitionBindings.Add(web.RoleDefinitions["完全控制"]);
properties.ListItem.RoleAssignments.Add(ra1);
// 下面的是给单独用户分配完全控制权限
//首先获得项目负责人的值,原始的值是这样的:0758#km\test,所以要处理出来
string projectMan = properties.ListItem["项目负责人"].ToString();
string[] userID = projectMan.Split('#');
SPRoleAssignment ra3 = new SPRoleAssignment(web.EnsureUser(userID[1]));
ra3.RoleDefinitionBindings.Add(web.RoleDefinitions["完全控制"]);
properties.ListItem.RoleAssignments.Add(ra3);
Copy(web, templeteDocLib, templeteDocLib.RootFolder, 0, targetDoclibPath);
//资料上说ItemUpdated会执行10次,用下面这个方法解决
this.DisableEventFiring();
//设置新建立的文件夹审批状态为通过
//properties.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
properties.ListItem.Update();
this.EnableEventFiring();
targetDocLib.Update();
}
//SPFolder newFolder = web.GetFolder(targetDoclibPath);
//newFolder
}
catch (Exception ex)
{
}
}
}
});
}
//遍历文件夹和文件,并执行建立文件夹和拷贝文件操作
static void Copy(SPWeb web, SPDocumentLibrary doclib, SPFolder folder, int level, string targetDoclib)
{
SPListItemCollection items = doclib.GetItemsInFolder(doclib.DefaultView, folder);
foreach (SPListItem item in items)
{
if (item.FileSystemObjectType == SPFileSystemObjectType.Folder)
{
SPFolder targetFolder = web.GetFolder(targetDoclib);
SPFolder newfolder = targetFolder.SubFolders.Add(item.Name);
newfolder.Item.Update();
//复制该目录下的文件到目标文件夹
SPFileCollection spFiles = item.Folder.Files;
for (int i = spFiles.Count - 1; i > -1; i--)
{
spFiles[i].CopyTo(targetDoclib + item.Name + "/" + spFiles[i].Name);
}
Copy(web, doclib, item.Folder, level + 1, targetDoclib + "/" + item.Name);
}
}
}
}
}