SharePoint O365获取具有特定角色的所有用户

时间:2016-03-04 16:59:09

标签: powershell sharepoint permissions office365 csom

是否有办法让所有用户在网站集和子网站级别拥有“完全控制”权限并将其角色更改为其他内容? PowerShell将是理想的,但如果这是唯一的方法,CSOM也会很好。我知道我可以按角色分组,但我需要一种方法来获得明确添加的用户

我尝试了这个较旧的StackOverflow问题:Get all the users based on a specific permission using CSOM in SharePoint 2013但是我在第一个ctx.ExecuteQuery()上不断收到403禁用错误;对于所有网站,即使我是收藏管理员。我也想知道是否有人有PowerShell方法来做这件事。

2 个答案:

答案 0 :(得分:0)

您需要SharePoint Online Management shell通过Powershell建立与O365的连接。

Introduction to the SharePoint Online management shell

答案 1 :(得分:0)

根据错误消息,我怀疑您的代码中缺少凭证。

请尝试以下代码,让我知道它是否适用于您。

    static void Main(string[] args)
    {
        var webUrl = "[your_site_url]";

        var userEmailAddress = "[your_email_address]";

        using (var context = new ClientContext(webUrl))
        {
            Console.WriteLine("input your password and click enter");

            context.Credentials = new SharePointOnlineCredentials(userEmailAddress, GetPasswordFromConsoleInput());
            context.Load(context.Web, w => w.Title);
            context.ExecuteQuery();

            Console.WriteLine("Your site title is: " + context.Web.Title);

            //Retrieve site users             
            var users = context.LoadQuery(context.Web.SiteUsers);

            context.ExecuteQuery();

            foreach(var user in users)
            {
                Console.WriteLine(user.Email);
            }
        }
    }

    private static SecureString GetPasswordFromConsoleInput()
    {
        ConsoleKeyInfo info;

        SecureString securePassword = new SecureString();
        do
        {
            info = Console.ReadKey(true);
            if (info.Key != ConsoleKey.Enter)
            {
                securePassword.AppendChar(info.KeyChar);
            }
        }
        while (info.Key != ConsoleKey.Enter);

        return securePassword;
    }
}
相关问题