返回首页
当前位置: 主页 > 编程语言 > C#教程 >

moss 列表 权限 实现代码

时间:2014-05-06 16:44来源:电脑教程学习网 www.etwiki.cn 编辑:admin

* User: Administrator Date: 10-07-16 Time: 10:58
* Created in $/SYDXWF/SYDXWF/EventHandlerSYDXWF
* 个人日历权限设置
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Workflow;

namespace EventHandlerSYDXWF
{
public class PersonalCalendarAuthorizationEventHandler : SPItemEventReceiver
{

/// <summary>
/// 添加事件
/// </summary>
/// <param name="properties"></param>
public override void ItemAdded(SPItemEventProperties properties)
{
try
{
this.DisableEventFiring();
SPListItem item = properties.ListItem;
item["Title"] = item.ID.ToString();
item.Update();

this.SetAuthorization(properties);
}
catch (Exception ex)
{

}
}
/// <summary>
/// 权限设置函数
/// </summary>
/// <param name="properties">当前列表属性集合</param>
/// <param name="refUser">用户</param>
public void SetAuthorization(SPItemEventProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.OpenWeb().ID))
{
SPList list = web.Lists[properties.ListId];
SPListItem item = list.Items.GetItemById(properties.ListItemId);
item.BreakRoleInheritance(false);

//查看对象
SPUser refUser = null;
SPRoleAssignment roleAss = null;
SPRoleDefinition roleDefine = null;

refUser = web.AllUsers.GetByID(properties.CurrentUserId);
roleAss = new SPRoleAssignment(refUser);
roleDefine = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
roleAss.RoleDefinitionBindings.Add(roleDefine);
item.RoleAssignments.Add(roleAss);

if (item["ViewObject"] != null)
{
SPFieldUserValueCollection userValues = item["ViewObject"] as SPFieldUserValueCollection;
foreach (SPFieldUserValue userValue in userValues)
{

if (userValue.User == null)
{
SPGroup group = web.SiteGroups.GetByID(userValue.LookupId);
roleAss = new SPRoleAssignment(group);
}
else
{
roleAss = new SPRoleAssignment(userValue.User);
}
roleDefine = web.RoleDefinitions.GetByType(SPRoleType.Reader);
roleAss.RoleDefinitionBindings.Add(roleDefine);
item.RoleAssignments.Add(roleAss);
}
}

item.Update();

}
}
});
}
catch (Exception ex)
{

}
}

}
}

 

------分隔线----------------------------
标签(Tag):c# c#技巧 c#实例教程 c#源代码 c#基础教程
------分隔线----------------------------
推荐内容
猜你感兴趣