如何列出一个Office365域下所有拥有One Drive的用户?

How to list all the users with One Drive in one Office365 domain?

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

MSDN 上有 PowerShell script 解决方案,我们可以只用 C# 代码实现吗?

基于来自 MSDN 的 PowerShell Script,我想出了如何在 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);
    
    }