簡體   English   中英

Sharepoint 2013服務器對象模型。 在網站創建期間將權限從一個網站復制到另一個網站

[英]Sharepoint 2013 Server Object Model. Copy permissions from one site to another during site creation

我要將權限從網站集A復制到我在同一Web App的網站集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.從新創建的站點中刪除原始權限。

此代碼無法成功執行:

  1. 將組從另一個網站集復制到新網站。

找到了某人創建的方法...

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();

}

將功能重新擴展為擴展方法,而無需進行工作流的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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM