Серверная объектная модель Sharepoint 2013. Копировать разрешения с одного сайта на другой при создании сайта
Я хочу скопировать разрешения из семейства сайтов A на сайт, который я создаю в семействе сайтов 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. удалить исходные разрешения с недавно созданного сайта.
Этот код не успешно:
- скопировать группы из другого семейства сайтов на новый сайт.
2 ответа
Решение
Нашел метод, который кто-то создал...
http://rajeshagadi.blogspot.com/2011/04/how-to-programmatically-copy-web-level.html
public static void CopyWebRoleAssignments(SPWeb sourceWeb, SPWeb destinationWeb)
{
//Copy Role Assignments from source to destination web.
foreach (SPRoleAssignment sourceRoleAsg in sourceWeb.RoleAssignments)
{
SPRoleAssignment destinationRoleAsg = null;
//Get the source member object
SPPrincipal member = sourceRoleAsg.Member;
//Check if the member is a user
try
{
SPUser sourceUser = (SPUser)member;
destinationWeb.EnsureUser(sourceUser.LoginName);//EDITED
SPUser destinationUser = destinationWeb.AllUsers[sourceUser.LoginName];
if (destinationUser != null)
{
destinationRoleAsg = new SPRoleAssignment(destinationUser);
}
}
catch
{ }
if (destinationRoleAsg == null)
{
//Check if the member is a group
try
{
SPGroup sourceGroup = (SPGroup)member;
SPGroup destinationGroup = destinationWeb.SiteGroups[sourceGroup.Name];
destinationRoleAsg = new SPRoleAssignment(destinationGroup);
}
catch
{ }
}
//At this state we should have the role assignment established either by user or group
if (destinationRoleAsg != null)
{
foreach (SPRoleDefinition sourceRoleDefinition in sourceRoleAsg.RoleDefinitionBindings)
{
try { destinationRoleAsg.RoleDefinitionBindings.Add(destinationWeb.RoleDefinitions[sourceRoleDefinition.Name]); }
catch { }
}
if (destinationRoleAsg.RoleDefinitionBindings.Count > 0)
{
//handle additon of an existing permission assignment error
try { destinationWeb.RoleAssignments.Add(destinationRoleAsg); }
catch (ArgumentException) { }
}
}
}
//Finally update the destination web
destinationWeb.Update();
}
Переработал функциональность как метод Extension без обработки рабочего процесса try-catch:
public static void CopyWebRoleAssignmentsFrom(this SPWeb web, SPWeb fromWeb)
{
web.BreakRoleInheritance(false);
foreach (SPRoleAssignment sourceRoleAsg in fromWeb.RoleAssignments)
{
SPRoleAssignment destinationRoleAsg = null;
SPPrincipal member = sourceRoleAsg.Member;
if (member is SPUser)
{
SPUser sourceUser = member as SPUser;
SPUser user = web.SiteUsers[sourceUser.LoginName];
destinationRoleAsg = new SPRoleAssignment(user);
}
else if (member is SPGroup)
{
SPGroup sourceGroup = (SPGroup)member;
SPGroup group = web.SiteGroups[sourceGroup.Name];
destinationRoleAsg = new SPRoleAssignment(group);
}
foreach (SPRoleDefinition sourceRoleDefinition in sourceRoleAsg.RoleDefinitionBindings)
{
destinationRoleAsg.RoleDefinitionBindings.Add(web.RoleDefinitions[sourceRoleDefinition.Name]);
}
if (destinationRoleAsg.RoleDefinitionBindings.Count > 0)
{
web.RoleAssignments.Add(destinationRoleAsg);
}
}
web.Update();
}