Matt Matt - 2 months ago 13x
C# Question

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

We're using the SharePoint Client Object Model SDK to access Office 365, there's no API to get all the users who has one drive. How can we do that?

There'r is a PowerShell script solution on MSDN, can we implement it with only C# code?


Based on the PowerShell Script from MSDN, I figured it out how to do it in C#:

  1. On command line, run WSDL.exe to generate the proxy code for the user profiler service:

    wsdl  /username:aaaaa /password:ppppp
  2. Add the generated file "UserProfileService.cs" to the project
  3. The following code will list all the users with 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;
        int nextIndex = -1;
        nextIndex = Int32.Parse(userProfileResult.NextValue);                           
        userProfileResult = uprofService.GetUserProfileByIndex(nextIndex);