如何列出一个Office365域中拥有一个Drive的所有用户

本文关键字:Drive 有一个 拥有 用户 何列出 Office365 一个 | 更新日期: 2023-09-27 18:19:23

我们使用SharePoint客户端对象模型SDK来访问Office 365,没有API来获取拥有一个驱动器的所有用户。我们怎么能做到呢?

MSDN上有一个PowerShell脚本解决方案,我们可以只用c#代码实现它吗?

如何列出一个Office365域中拥有一个Drive的所有用户

基于MSDN的PowerShell脚本,我想出了如何在c#中做到这一点:

  1. 在命令行上,运行WSDL.exe为用户剖析器服务生成代理代码:

    wsdl https://xxxx-admin.sharepoint.com/_vti_bin/UserProfileService.asmx?wsdl  /username:aaaaa /password:ppppp
    
  2. 将生成的文件"UserProfileService.cs"添加到项目
  3. 下面的代码将列出所有使用OneDrive的用户:

    UserProfileService uprofService = new UserProfileService();
    uprofService.Url = adminPortalUrl + "/_vti_bin/UserProfileService.asmx";
    uprofService.UseDefaultCredentials = false;
    Uri targetSite = new Uri(url);
    uprofService.CookieContainer = new CookieContainer();
    string authCookieValue = spCredentials.GetAuthenticationCookie();
    uprofService.CookieContainer.SetCookies(targetSite, authCookieValue);
    var userProfileResult = uprofService.GetUserProfileByIndex(-1);
    long numProfiles = uprofService.GetUserProfileCount(); 
    while (userProfileResult.NextValue != "-1")
    {
     string personalUrl = null;
     foreach(var  u in userProfileResult.UserProfile)
     {
      /* (PersonalSpace is the name of the path to a user's OneDrive for Business site. Users who have not yet created a OneDrive for Business site might not have this property set.)*/
     if (u.Values.Length != 0 && u.Values[0].Value != null && u.Name == "PersonalSpace" )
    {   personalUrl = u.Values[0].Value as string;
            break;
         }
    }
        int nextIndex = -1;
        nextIndex = Int32.Parse(userProfileResult.NextValue);                           
        userProfileResult = uprofService.GetUserProfileByIndex(nextIndex);
    }