SharePoint 服务器端对象模型操作文档库(上传/授权/查看权限)
2013-09-11 14:18
519 查看
简介:上传文档到文档库,并对项目级授权,查看项目级权限方法
//在列表根目录下创建文件夹
public static string CreatFolderToSPDocLib(string strFolderName, string strDocLibName)
{
string FolderPath = string.Empty;
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.GetListsOfType(SPBaseType.DocumentLibrary);
lists.IncludeRootFolder = true;
SPList list = lists[strDocLibName];
list.EnableFolderCreation = true;
SPListItem item = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
item.Update();
list.Update();
FolderPath = item["FileRef"].ToString();
web.AllowUnsafeUpdates = false;
}
}
}
catch
{
}
return FolderPath;
}
//上传文件到文件夹,并授权给相关用户
public static bool UpLoadFileToFolder(byte[] FileStream, string FileName, string FolderPath, string allLoginName)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPFolder folder = web.GetFolder(FolderPath);
SPListItem listItem = folder.Files.Add(FileName, FileStream).Item;
//断开原来列表项所继承的权限,使其可以设置独立权限
listItem.BreakRoleInheritance(true);
//将原来所继承的权限通通移除
foreach (SPRoleAssignment roleAssignment in listItem.RoleAssignments)
{
roleAssignment.RoleDefinitionBindings.RemoveAll();
roleAssignment.Update();
listItem.Update();
}
//获取将要设置权限的用户
SPUser myUser = web.EnsureUser(allLoginName);
//定义权限分配
SPRoleAssignment myRoleAssignment = new SPRoleAssignment(myUser.LoginName, myUser.Email, myUser.Name, myUser.Notes);
//绑定设置的权限
myRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Reader));
//把这个权限加到我们的列表中
listItem.RoleAssignments.Add(myRoleAssignment);
listItem.Update();
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//通过ID获取列表项
public static string GetRoleAssignmentsOfSPListItem(string ListName, int ItemID)
{
string reValue = string.Empty;
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[ListName];
SPListItem item = list.Items.GetItemById(ItemID);
SPRoleAssignmentCollection Rolecoll = item.RoleAssignments;
foreach (SPRoleAssignment role in Rolecoll)
{
for (int i = 0; i < role.RoleDefinitionBindings.Count; i++)
{
reValue += (role.Member.LoginName + ":" + role.RoleDefinitionBindings[i].Name + ":" + role.RoleDefinitionBindings[i].BasePermissions.ToString());
}
}
web.AllowUnsafeUpdates = false;
}
}
}
catch
{
}
return reValue;
}
后记:几个简单的方法,测试通过,可能不太完善,需要的话可以继续完善。
//在列表根目录下创建文件夹
public static string CreatFolderToSPDocLib(string strFolderName, string strDocLibName)
{
string FolderPath = string.Empty;
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.GetListsOfType(SPBaseType.DocumentLibrary);
lists.IncludeRootFolder = true;
SPList list = lists[strDocLibName];
list.EnableFolderCreation = true;
SPListItem item = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
item.Update();
list.Update();
FolderPath = item["FileRef"].ToString();
web.AllowUnsafeUpdates = false;
}
}
}
catch
{
}
return FolderPath;
}
//上传文件到文件夹,并授权给相关用户
public static bool UpLoadFileToFolder(byte[] FileStream, string FileName, string FolderPath, string allLoginName)
{
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPFolder folder = web.GetFolder(FolderPath);
SPListItem listItem = folder.Files.Add(FileName, FileStream).Item;
//断开原来列表项所继承的权限,使其可以设置独立权限
listItem.BreakRoleInheritance(true);
//将原来所继承的权限通通移除
foreach (SPRoleAssignment roleAssignment in listItem.RoleAssignments)
{
roleAssignment.RoleDefinitionBindings.RemoveAll();
roleAssignment.Update();
listItem.Update();
}
//获取将要设置权限的用户
SPUser myUser = web.EnsureUser(allLoginName);
//定义权限分配
SPRoleAssignment myRoleAssignment = new SPRoleAssignment(myUser.LoginName, myUser.Email, myUser.Name, myUser.Notes);
//绑定设置的权限
myRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Reader));
//把这个权限加到我们的列表中
listItem.RoleAssignments.Add(myRoleAssignment);
listItem.Update();
web.AllowUnsafeUpdates = false;
return true;
}
}
}
catch
{
return false;
}
}
//通过ID获取列表项
public static string GetRoleAssignmentsOfSPListItem(string ListName, int ItemID)
{
string reValue = string.Empty;
try
{
using (SPSite site = new SPSite(SiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[ListName];
SPListItem item = list.Items.GetItemById(ItemID);
SPRoleAssignmentCollection Rolecoll = item.RoleAssignments;
foreach (SPRoleAssignment role in Rolecoll)
{
for (int i = 0; i < role.RoleDefinitionBindings.Count; i++)
{
reValue += (role.Member.LoginName + ":" + role.RoleDefinitionBindings[i].Name + ":" + role.RoleDefinitionBindings[i].BasePermissions.ToString());
}
}
web.AllowUnsafeUpdates = false;
}
}
}
catch
{
}
return reValue;
}
后记:几个简单的方法,测试通过,可能不太完善,需要的话可以继续完善。
相关文章推荐
- SharePoint 服务器端对象模型操作文档库(上传/授权/查看权限)
- SharePoint 服务器端对象模型操作文档库(上传/授权/查看权限)
- 使用SharePoint 2010 客户端对象模型进行文档库及文档的操作
- SharePoint服务器端对象模型 之 访问用户、用户组和权限(Part 1)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 1)
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除)
- SharePoint服务器端对象模型 之 访问用户、用户组和权限(Part 2)
- SharePoint服务器端对象模型 之 访问用户、用户组和权限(Part 3)
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除)
- SharePoint 服务器端对象模型 之 使用LINQ进行数据访问操作(Part 2)
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 3)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除
- 使用对象模型操作文档库-DocumentLibrary
- Oracle创建表空间、创建用户、授权、授权对象的访问以及查看权限集合
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 3)
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 2)
- SharePoint 2013 对象模型操作"网站设置"菜单
- SharePoint服务器端对象模型 之 访问网站和列表数据(Part 4)