gpt4 book ai didi

c# - 复制 SharePoint 组的应用程序

转载 作者:行者123 更新时间:2023-11-30 17:11:11 25 4
gpt4 key购买 nike

根据此条目:Cloning a sharepoint rolegroup我正在尝试创建一个控制台应用程序来复制 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);

我不一定需要有人来帮我解决这个问题(毕竟,这是一个学习练习)。相反,您能否帮助我了解我的思维过程哪里出了问题,以及我需要学习什么来纠正这个问题?

最佳答案

您已找到代码的正确问题。当您调用以下方法时:

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

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

您可以通过使用实例获取用户对象:

SPUser spUser = spWeb.EnsureUser(loginName);

关于c# - 复制 SharePoint 组的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11535807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com