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

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

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?

  1. using System;
  2. using System.Security;
  3. using Microsoft.Online.SharePoint.TenantAdministration;
  4. using Microsoft.SharePoint.Client;
  5. namespace Basic_Site_Subsite_Group_User_Creation
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. // Opens the Admin URL
  12. using (ClientContext ctx = new ClientContext("https://developer19.sharepoint.com/sites/Created_with_Communication_site"))
  13. {
  14. // Authenticating with Tenant Admin
  15. SecureString password = new SecureString();
  16. foreach (char c in "password".ToCharArray())
  17. password.AppendChar(c);
  18. ctx.Credentials = new SharePointOnlineCredentials("kailash@kailash.cf", password);
  19. Group gru = ctx.Web.SiteGroups.GetByName("subsite1");
  20. User use = ctx.Web.EnsureUser("anil@kailash.cf");
  21. gru.Users.AddUser(use);
  22. ctx.ExecuteQuery();
  23. }
  24. }
  25. }
  26. }
英文:

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?

  1. using System;
  2. using System.Security;
  3. using Microsoft.Online.SharePoint.TenantAdministration;
  4. using Microsoft.SharePoint.Client;
  5. namespace Basic_Site_Subsite_Group_User_Creation
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. //Opens the Admin URL
  12. using (ClientContext ctx = new ClientContext("https://developer19.sharepoint.com/sites/Created_with_Communication_site"))
  13. {
  14. //Authenticating with Tenant Admin
  15. SecureString password = new SecureString();
  16. foreach (char c in "password".ToCharArray())
  17. password.AppendChar(c);
  18. ctx.Credentials = new SharePointOnlineCredentials("kailash@kailash.cf", password);
  19. Group gru = ctx.Web.SiteGroups.GetByName("subsite1");
  20. User use = ctx.Web.EnsureUser("anil@kailash.cf");
  21. gru.Users.AddUser(use);
  22. ctx.ExecuteQuery();

答案1

得分: 1

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

  1. string groupName = "Domain Users";
  2. string domainName = "你的域名";
  3. PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
  4. GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);
  5. if (grp != null)
  6. {
  7. foreach (Principal p in grp.GetMembers(false))
  8. {
  9. p.DisplayName //在这里执行你的操作
  10. }
  11. grp.Dispose();
  12. ctx.Dispose();
  13. }

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

英文:

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

  1. string groupName = "Domain Users";
  2. string domainName = "your domainName";
  3. PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
  4. GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);
  5. if (grp != null)
  6. {
  7. foreach (Principal p in grp.GetMembers(false))
  8. {
  9. p.DisplayName //do your action here
  10. }
  11. grp.Dispose();
  12. ctx.Dispose();
  13. }

答案2

得分: 0

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

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

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

伪代码示例:

  1. for (var i = 1; i <= users.Count; i++)
  2. {
  3. User user = ctx.Web.EnsureUser(users[i].Mail);
  4. gru.Users.AddUser(user);
  5. if (i % 100 == 0)
  6. {
  7. ctx.ExecuteQuery();
  8. }
  9. }
  1. <details>
  2. <summary>英文:</summary>
  3. Refer [this project][1] to register your app so you could call graph api to get users from AD.
  4. if (graphServiceClient != null)
  5. {
  6. var users= await graphServiceClient.Users.Request().GetAsync();
  7. [![enter image description here][2]][2]
  8. When adding the users to SharePoint, would suggest do this batch(submit 100 users in one request to avoid issue).
  9. Fake code:
  10. for(var i = 1; i &lt;= users.Count; i++)
  11. {
  12. User use = ctx.Web.EnsureUser(users[i].Mail);
  13. gru.Users.AddUser(use);
  14. if (i % 100 == 0)
  15. {
  16. ctx.ExecuteQuery();
  17. }
  18. }
  19. [1]: https://github.com/microsoftgraph/console-csharp-connect-sample
  20. [2]: https://i.stack.imgur.com/yhX4G.png
  21. </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:

确定