如何在C#中使用CSOM获取Office 365组织中所有用户的列表?

huangapple go评论72阅读模式
英文:

How to get a list of all users from Office 365 Organization in C# using CSOM?

问题

I've 4000+ users in my Office 365 organization and want to add all those users to my SharePoint subsite in C# using CSOM. I know I can achieve this using PowerShell Codes, but I want to do it in C# using CSOM. I can add a particular user by the following code, but how to add all 4000+ users in a single code? Is there any way to iterate an object in a loop which has all 4000+ users in it?

using System;
using System.Security;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

namespace Basic_Site_Subsite_Group_User_Creation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Opens the Admin URL
            using (ClientContext ctx = new ClientContext("https://developer19.sharepoint.com/sites/Created_with_Communication_site"))
            {
                // Authenticating with Tenant Admin
                SecureString password = new SecureString();
                foreach (char c in "password".ToCharArray())
                    password.AppendChar(c);
                ctx.Credentials = new SharePointOnlineCredentials("kailash@kailash.cf", password);
                
                Group gru = ctx.Web.SiteGroups.GetByName("subsite1");
                User use = ctx.Web.EnsureUser("anil@kailash.cf");
                gru.Users.AddUser(use);
                ctx.ExecuteQuery();
            }
        }
    }
}
英文:

I've 4000+ users in my Office 365 organization and want to add all those users to my SharePoint subsite in C# using CSOM. I know I can I achieve this using PowerShell Codes, but I want to do it in C# using CSOM. I can add a particular user by the following code, but how to add all 4000+ users in a single code?. Is there any way to iterate a object in a loop which has all 4000+ users in it?

using System;
using System.Security;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
namespace Basic_Site_Subsite_Group_User_Creation
{
    class Program
    {
        static void Main(string[] args)
        {
            //Opens the Admin URL 
            using (ClientContext ctx = new ClientContext("https://developer19.sharepoint.com/sites/Created_with_Communication_site"))
            {
                //Authenticating with Tenant Admin
                SecureString password = new SecureString();
                foreach (char c in "password".ToCharArray())
                    password.AppendChar(c);
                ctx.Credentials = new SharePointOnlineCredentials("kailash@kailash.cf", password);
                    Group gru = ctx.Web.SiteGroups.GetByName("subsite1");
                    User use = ctx.Web.EnsureUser("anil@kailash.cf");
                    gru.Users.AddUser(use);
                    ctx.ExecuteQuery();

答案1

得分: 1

你可以在你的组织中使用这个代码(我相信是这样的)

string groupName = "Domain Users";
string domainName = "你的域名";
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);
if (grp != null)
{
    foreach (Principal p in grp.GetMembers(false))
    {
        p.DisplayName //在这里执行你的操作
    }

    grp.Dispose();
    ctx.Dispose();
}

请注意,这段代码用于操作活动目录(Active Directory)。

英文:

You can use this if your organisation have a AD(I believe so)

string groupName = "Domain Users";
string domainName = "your domainName";
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);
if (grp != null)
{
     foreach (Principal p in grp.GetMembers(false))
        {
            p.DisplayName //do your action here
        }


    grp.Dispose();
    ctx.Dispose();
}

答案2

得分: 0

请参考此项目以注册您的应用程序,以便您可以调用 Graph API 以获取来自 AD 的用户。

if (graphServiceClient != null)
{
    var users = await graphServiceClient.Users.Request().GetAsync();
}

在将用户添加到 SharePoint 时,建议使用批处理方式(每次提交 100 个用户以避免问题)。

伪代码示例:

for (var i = 1; i <= users.Count; i++)
{
    User user = ctx.Web.EnsureUser(users[i].Mail);
    gru.Users.AddUser(user);
    if (i % 100 == 0)
    {
        ctx.ExecuteQuery();
    }
}

<details>
<summary>英文:</summary>

Refer [this project][1] to register your app so you could call graph api to get users from AD.

    if (graphServiceClient != null)
                    {
                        var users= await graphServiceClient.Users.Request().GetAsync();

[![enter image description here][2]][2]

When adding the users to SharePoint, would suggest do this batch(submit 100 users in one request to avoid issue).

Fake code:

    for(var i = 1; i &lt;= users.Count; i++)
                        {
                            User use = ctx.Web.EnsureUser(users[i].Mail);
                            gru.Users.AddUser(use);
                            if (i % 100 == 0)
                            {
                                ctx.ExecuteQuery();
                            }
                        }

  [1]: https://github.com/microsoftgraph/console-csharp-connect-sample
  [2]: https://i.stack.imgur.com/yhX4G.png

</details>



huangapple
  • 本文由 发表于 2020年1月7日 00:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定