* 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)
{
}
}
}
}