复制一个SharePoint群组的应用程序

3
根据这篇文章:复制SharePoint角色组,我正在尝试创建一个控制台应用程序来复制一个SharePoint群组,包括其权限。
根据Tjassens的答案,我得到了以下结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;  

namespace REGroupCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite spSite = new SPSite("http://dev"))
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    // first we find the group that we want to clone
                    SPGroup group = spWeb.Groups["Test Group"];

                    // then we use this retreived group to get the roleassignments on the SPWeb object
                    SPRoleAssignment ass = spWeb.RoleAssignments.GetAssignmentByPrincipal(group);

                    string groupName = "Test Group 2"; // group to create
                    string groupDescription = "Group created by REGroupCopy";
                    string user = "michael";

                    spWeb.SiteGroups.Add(groupName, user, user, groupDescription);
                    SPGroup newGroup = spWeb.SiteGroups[groupName];
                    SPRoleAssignment roleAssignment = new SPRoleAssignment(newGroup);

                    //add role to web
                    spWeb.RoleAssignments.Add(roleAssignment);
                    spWeb.Update();
                }
            }         
        }
    }
}

很遗憾,我认为我没有完全理解所有内容。具体来说,我认为以下这些代码行不正确,但我不确定应该如何更改:

                string groupName = "Test Group 2"; // group to create
                string groupDescription = "Group created by REGroupCopy";
                string user = "michael";

                spWeb.SiteGroups.Add(groupName, user, user, groupDescription);

我并不一定需要有人来帮我修复这个问题(毕竟,这是一次学习练习)。相反,你能否帮我理解我的思考过程出了什么问题,以及我需要学习哪些知识来纠正这个问题?

2个回答

1
您已经找到了代码的正确问题。当您调用以下方法时:
spWeb.SiteGroups.Add(groupName, user, user, groupDescription); 

您忘记了用户不应该是一个字符串,而应该是一个实际的SPUser对象。如果您获取了SPUser对象,则应该能够将新组添加到SPWeb/SPSite。

例如,您可以使用以下方式获取用户对象:

SPUser spUser = spWeb.EnsureUser(loginName);

1

添加方法:

第一个参数:新的组名

第二个参数:所有者(SPUser对象)

第三个参数:组的默认用户(SPMember对象)。

第四个参数:新组的描述

从网站管理员新建组

第一个参数类似于名称文本框

第二个参数和第三个参数类似于组所有者人员选择器

第四个参数类似于关于我富文本框


你能提供一个例子吗?并且,最好能够在控制台应用程序中完成所有操作(如果可能的话)- 这似乎与此不符? - Michael A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接