我想将网站集 A 的权限复制到我在同一个 Web 应用程序中的网站集 B 中创建的网站。这发生在 ItemAdded 上的列表项事件接收器中。
这是我到目前为止...
static void SetupNewSubSite(int currentYear, SPItemEventProperties properties, int siteIndexId)
{
//set properties to create my new web
string description = properties.AfterProperties["Project_x0020_Description"].ToString();
SPListItem CurrentItem = properties.ListItem;
String subSiteUrl = Convert.ToString(siteIndexId);
SPSite projectSiteCollection = new SPSite(properties.Web.Site.Url + "/" + currentYear);
SPWeb sWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl);
SPWeb oWeb = projectSiteCollection.RootWeb;
SPWebCollection cWebs = oWeb.Webs;
//create the new web
SPWeb xWeb = cWebs.Add(subSiteUrl, properties.AfterProperties["Title"].ToString(),
properties.AfterProperties["Project_x0020_Description"].ToString(), 1033, "{B5B6BDD1-485A-44BC-B093-F1048271C49D}", false, false);
UpdateItemProjectUrl(CurrentItem, properties.Web.Site.Url + "/" + currentYear + "/" + subSiteUrl, currentYear);
//break inheritance and remove permissions from the new site
xWeb.BreakRoleInheritance(false);
LogMessage("Role Count: " + xWeb.RoleAssignments.Count.ToString());
while (xWeb.RoleAssignments.Count > 0)
{
xWeb.RoleAssignments.Remove(0);
}
//Get the roleassignments from the source site
SPRoleAssignmentCollection sRoleAssignments = sWeb.RoleAssignments;
LogMessage("role assignment count from source web: "+ sRoleAssignments.Count.ToString());
foreach (SPRoleAssignment sRoleAssignment in sRoleAssignments)
{
SPPrincipal sPrincipal = sRoleAssignment.Member;
LogMessage("Principal Name: " + sPrincipal.Name.ToString());
try
{
//add roleassignment to newly created web
xWeb.RoleAssignments.Add(sPrincipal);
}
catch (Exception ex)
{
LogMessage(ex.ToString());
}
}
xWeb.Update();
LogMessage("After Permissions Change");
xWeb.Dispose();
projectSiteCollection.Dispose();
oWeb.Dispose();
LogMessage("after dispose");
}
此代码成功: 1. 在其他网站集中创建一个新网站。2. 在新创建的站点上中断继承。3.从新创建的站点中删除原来的权限。
此代码未成功:
- 将组从其他网站集中复制到新网站。