如何在一个Office365域中列出具有One Drive的所有用户?

时间:2015-10-27 14:11:03

标签: c# office365 onedrive csom

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

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

1 个答案:

答案 0 :(得分:1)

基于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);
    
    }