问题描述
我的 SharePoint 页面中有一个文档库,其中有 10 个文档.
I have a document library in my SharePoint page and there are 10 documents in it.
如果用户 A 已登录,我希望他只能看到该文档库中的 5 个文档.
If User A is logged in I want him to only see 5 of those documents in that document library.
如何创建一些自定义文档库以使其正常工作?
How can I create some custom document library for this to work?
我安装了 MOSS.
提前致谢!
推荐答案
编写一个 ItemEventReceiver,根据库中的字段(即包含不同角色的列)打破权限.
Write an ItemEventReceiver that breaks the permissions based on a field in the library, i.e. a column that holds the different roles .
我们通过创建一个列表来完成此操作,该列表包含与共享点组相关联的所有角色.
We have done this by creating a list that holds all roles coupled to sharepoint groups.
即
管理员 -> 网站所有者 (SPGroup)、公司管理员 (SPGroup)
经理 -> 经理(SPGroup)
然后在我们的内容类型中,我们有一个指向此列表的查找列.
then in our content type we have a lookup column to this list.
这是 ItemEventReceiver 的代码:
Here's the code for the ItemEventReceiver:
public override void ItemUpdated(SPItemEventProperties properties)
{
lock (_lock)
{
try
{
using (SPSite site = new SPSite(properties.SiteId,
properties.ListItem.ParentList.ParentWeb.Site.SystemAccount.UserToken))
using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
{
web.AllowUnsafeUpdates = true;
var item = web.Lists[properties.ListId].GetItemById(properties.ListItemId);
var roles = item["Roles"] as SPFieldLookupValueCollection;
var rolesList = web.Site.RootWeb.Lists["Company Roles"];
var groupsToAdd = new List<SPFieldUserValue>();
if (item.HasUniqueRoleAssignments)
{
item.ResetRoleInheritance();
item = item.ParentList.GetItemById(item.ID);
}
if (roles != null && roles.Count > 0)
{
// Iterate over the roles and see if there is a group associated
foreach (var role in roles)
{
var roleItem = rolesList.GetItemById(rol.LookupId);
if (roleItem != null)
{
// This is the SPgroup field in the rolesList
var groups = roleItem["Groups"] as SPFieldUserValueCollection;
if (groups != null)
{
groupsToAdd.AddRange(from g in groups
where g.User == null
select g);
}
}
}
if (groupsToAdd.Count > 0)
{
item.BreakRoleInheritance(false);
foreach (var value in groupsToAdd)
{
var group = web.Groups[value.LookupValue];
var assignment = web.RoleAssignments.GetAssignmentByPrincipal(group);
item.RoleAssignments.Add(assignment);
}
}
}
DisableEventFiring();
item.SystemUpdate(false);
EnableEventFiring();
}
}
catch (Exception ex)
{
//LOG ERROR
}
}
}
这篇关于在 SharePoint 中创建自定义文档库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!