using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SharePointProject2
{
class Class2
{ /// <summary>
/// 判断组是否存在
/// </summary>
/// <param name="web"></param>
/// <param name="groupname"></param>
/// <returns></returns>
public bool IsExistGroup(SPWeb web, string groupname)
{
try
{
foreach (SPGroup grouplist in web.SiteGroups)//判断组是否存在
{
if (grouplist.ToString().ToLower() == groupname.ToLower())
return true;
}
return false;
}
catch (Exception)
{
throw;
}
} /// <summary>
/// 新建组
/// </summary>
/// <param name="web"></param>
/// <param name="groupname"></param>
/// <param name="member"></param>
/// <param name="spuser"></param>
/// <param name="description"></param>
/// <returns></returns>
public bool AddGroup(SPWeb web, string groupname, SPMember member, SPUser spuser, string description)
{
try
{
if (!IsExistGroup(web, groupname))
{
web.SiteGroups.Add(groupname, member, spuser, description);//新建组
return true;
}
return false;
}
catch (Exception)
{ throw;
}
} /// <summary>
/// 判断指定组是否存在用户
/// </summary>
/// <param name="web"></param>
/// <param name="username">Domin\\Name形式</param>
/// <param name="groupname"></param>
/// <returns></returns>
public bool IsExistUser(SPWeb web, string username, string groupname)
{
try
{
foreach (SPUser userlist in web.SiteGroups[groupname].Users)//判断指定组是否存在用户
{
if (userlist.ToString().ToLower() == username.ToLower())
return true;
}
return false;
}
catch (Exception)
{
throw;
}
} /// <summary>
/// 根据指定的组新建用户
/// </summary>
/// <param name="web"></param>
/// <param name="loginname">登录名:Domin\\Name形式</param>
/// <param name="groupname">组名称</param>
/// <param name="email">Email</param>
/// <param name="cnname">中文名</param>
/// <param name="notes">用户说明</param>
/// <returns>bool</returns>
public bool AddUserToGroup(SPWeb web, string loginname, string groupname, string email, string cnname, string notes)
{
try
{
if (!IsExistUser(web, loginname, groupname))
{
web.SiteGroups[groupname].AddUser(loginname, email, cnname, notes);//新建用户
return true;
}
return false;
}
catch (Exception)
{ throw;
}
} /// <summary>
/// 组权限分配与定义(New)
/// </summary>
/// <param name="web"></param>
/// <param name="groupname"></param>
/// <param name="roles"></param>
/// <returns></returns>
public bool AddGroupToRoles(SPWeb web, string groupname, string[] roles)
{
try
{
string[] _roles = roles;
int rolemun = _roles.Length; if (IsExistGroup(web, groupname))
{
//改变站点继承权
if (!web.HasUniqueRoleDefinitions)
{
web.RoleDefinitions.BreakInheritance(true, true);//复制父站点角色定义并且保持权限
} //站点继承权改变后重新设置状态
web.AllowUnsafeUpdates = true; //组权限分配与定义(New)
SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
SPMember memCrossSiteGroup = web.SiteGroups[groupname];
SPPrincipal myssp = (SPPrincipal)memCrossSiteGroup;
SPRoleAssignment myroles = new SPRoleAssignment(myssp);
SPRoleDefinitionBindingCollection roleDefBindings = myroles.RoleDefinitionBindings;
if (rolemun > )
{
for (int i = ; i < rolemun; i++)
{
roleDefBindings.Add(roleDefinitions[_roles[i]]);
}
}
roleAssignments.Add(myroles);
return true;
}
return false;
}
catch (Exception)
{ throw;
}
} protected void btnCreateSite_Click(object sender, EventArgs e)
{ try
{
SPSite site = new SPSite("http://contososrv:8010/"); //string spwt = SPWebTemplate.WebTemplateSTS;//默认选择协作站点模板 web = site.OpenWeb("Test/");
web.AllowUnsafeUpdates = true;
SPMember memUser = web.Users[];
SPUser suser = web.Users[]; //新建组
//web.SiteGroups.Add("skyteam3", memUser, suser, "new skyteam");
//web.SiteGroups["skyteam3"].AddUser("adr\\administrator", "[email protected]", "蔡", "系统维修人员");
//web.Groups["skyteam2"].AddUser("adr\\pccai", "[email protected]", "蔡", "系统维修人员"); //新建子站点
string currentTemplate = web.WebTemplate;
//web.Webs.Add("Test2", "站点名称2", "站点描述2", 2052, "_GLOBAL_#0", true, false); //打开子站点
SPWeb web2 = site.OpenWeb("Test/Test2");
web2.AllowUnsafeUpdates = true;
// web2.SiteGroups.Add("skyteam6", memUser, suser, "new skyteam");//新建组
// web2.SiteGroups["skyteam6"].AddUser("adr\\administrator", "[email protected]", "边", "系统维修人员"); //改变站点继承权
if (!web2.HasUniqueRoleDefinitions)
{
web2.RoleDefinitions.BreakInheritance(true, true);
} //站点继承权改变后重新设置状态
web2.AllowUnsafeUpdates = true; //添加权限级别 (Role)
//SPRoleDefinition roleDefinition = new SPRoleDefinition();
//roleDefinition.Name = "项目角色";
//roleDefinition.Description = "项目角色可以批准所有项目情况.";
//roleDefinition.BasePermissions = SPBasePermissions.FullMask ^ SPBasePermissions.ManagePermissions;
//web2.RoleDefinitions.Add(roleDefinition); //更改权限级别 (Permissions)
SPRoleDefinitionCollection roles = web2.RoleDefinitions;
SPRoleDefinition roleDefinition1 = roles["读取"];
roleDefinition1.BasePermissions = SPBasePermissions.AddListItems |
SPBasePermissions.BrowseDirectories |
SPBasePermissions.EditListItems |
SPBasePermissions.DeleteListItems |
SPBasePermissions.AddDelPrivateWebParts;
roleDefinition1.Update(); //用户权限分配与定义(New)
SPRoleDefinitionCollection roleDefinitions = web2.RoleDefinitions;
SPRoleAssignmentCollection roleAssignments = web2.RoleAssignments;
SPRoleAssignment roleAssignment = new SPRoleAssignment("adr\\administrator", "[email protected]", "Display_Name", "Notes");
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
roleDefBindings.Add(roleDefinitions["项目角色"]);
roleAssignments.Add(roleAssignment); //权限定义(Old)
//SPRoleCollection siteGroups = web2.Roles;
//siteGroups.Add("skyteam6", "Description", SPRights.ManageWeb | SPRights.ManageSubwebs); //获得权限定义
SPRoleDefinition sprole = roleDefinitions.GetByType(SPRoleType.Reader);
string spname = sprole.Name; //组权限分配与定义(New)
SPRoleDefinitionCollection roleDefinitions1 = web2.RoleDefinitions;
SPRoleAssignmentCollection roleAssignments1 = web2.RoleAssignments;
SPMember memCrossSiteGroup = web2.SiteGroups["skyteam6"];
SPPrincipal myssp = (SPPrincipal)memCrossSiteGroup;
SPRoleAssignment myroles = new SPRoleAssignment(myssp);
SPRoleDefinitionBindingCollection roleDefBindings1 = myroles.RoleDefinitionBindings;
roleDefBindings1.Add(roleDefinitions1["设计"]);
roleDefBindings1.Add(roleDefinitions1["读取"]);
roleAssignments1.Add(myroles); //组权限分配与定义(Old)
//SPMember member = web2.Roles["skyteam"];
//web2.Permissions[member].PermissionMask =
// SPRights.ManageLists | SPRights.ManageListPermissions; //更改列表权限(Old)
//SPList list = site.Lists["通知"];
//SPPermissionCollection perms = list.Permissions;
//SPUserCollection users = site.Users;
//SPMember member = users["ADR\\pccai"];
//list.Permissions[member].PermissionMask = SPRights.AddListItems | SPRights.EditListItems; // PermissionCollection perc = web.Permissions;
//perc.AddUser("adr\\administrator", "[email protected]", "title", "Notes", PortalRight.AllSiteRights);
// SecurityManager.AddRole(context, "title", "descriptions", PortalRight.ManageSite); }
catch (Exception)
{
throw;
} } }
}
05-11 11:04