SPListItem item = null;
item.BreakRoleInheritance(false);
SPUser user = null;
SPWeb web=null;
SPRoleAssignment ass = new SPRoleAssignment(user);
SPRoleDefinition d = web.RoleDefinitions.GetByType(SPRoleType.Reader);
ass.RoleDefinitionBindings.Add(d);
item.RoleAssignments.Add(ass);
private SPWeb crossWeb = null;
/// <summary>
/// 當前web
/// </summary>
public SPWeb CrossWeb
{
get
{
if (this.crossWeb == null)
{
string url = System.Configuration.ConfigurationManager.AppSettings["userinfo"];
url = "http://mosslyh:8081/Office/Conversations/";
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
this.crossWeb = web;
}
}
}
return this.crossWeb;
}
set
{
this.crossWeb = value;
}
}
private string listName = "個人日歷";
/// <summary>
/// 個人日歷列表名稱
/// </summary>
public String ListName
{
get
{
return this.listName;
}
set
{
this.listName = value;
}
}
private SPList list = null;
/// <summary>
/// 個人日歷對象
/// </summary>
public SPList PersonalCalendarList
{
get
{
if (this.list == null)
{
this.list = CrossWeb.Lists[this.ListName];
}
return this.list;
}
set
{
this.list = value;
}
}
public void SetAuthorization(string LoginName, SPUser user)
{
//清除默認權限
for (int i = PersonalCalendarList.Permissions.Count - 1; i >= 0; i--)
{
PersonalCalendarList.Permissions.Remove(i);
}
//不能匿名訪問
PersonalCalendarList.AnonymousPermMask64 = SPBasePermissions.EmptyMask;
PersonalCalendarList.Permissions.Add(CrossWeb.SiteGroups[LoginName], (SPRights)SPPermissionGroup.All);
SPRoleAssignment roleAss = new SPRoleAssignment(user);
SPRoleDefinition d = CrossWeb.RoleDefinitions.GetByType(SPRoleType.Reader);
foreach (SPListItem item in PersonalCalendarList.Items)
{
item.BreakRoleInheritance(false);
roleAss.RoleDefinitionBindings.Add(d);
item.RoleAssignments.Add(ass);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace WssDemo
{
class FilePermission
{
/// <summary>
/// [否決的]
/// </summary>
static public void TestPower()
{
SPSite site = new SPSite("http://yetian:9000/sites/test");
SPWeb web = site.OpenWeb("YeTian");
SPList list = web.Lists["NewTest"];
//重新繼承權限
//list.Permissions.ResetPermissions();
//清除默認權限
for (int i = list.Permissions.Count - 1; i >= 0; i--)
{
list.Permissions.Remove(i);
}
//不能匿名訪問
list.AnonymousPermMask64 = SPBasePermissions.EmptyMask;
list.Permissions.Add(web.SiteGroups["Test訪問者"], (SPRights)SPPermissionGroup.All);
}
static public void TestRole()
{
SPSite site = new SPSite("http://yetian:9000/sites/test");
SPWeb web = site.OpenWeb("YeTian");
SPList list = web.Lists["NewTest"];
if (list.Items.Count > 0)
{
SPListItem item = list.Items[0];
item.BreakRoleInheritance(true);
for (int i = item.RoleAssignments.Count - 1; i >= 0; i--)
{
item.RoleAssignments.Remove(i);
}
SPRoleAssignment ass = new SPRoleAssignment(web.SiteGroups["Test訪問者"]);
SPRoleDefinition def = web.RoleDefinitions["僅查看"];
ass.RoleDefinitionBindings.Add(def);
item.RoleAssignments.Add(ass);
}
}
}
}