0

How to get a list of all users in a certain Farm in SharePoint by code? Is there any away to do that without must iterate every SPgroup in every SPWeb in every WEApp?

  • 1
  • You should be able to get all users in a farm from the user profiles. msdn.microsoft.com/en-us/library/office/jj163182.aspx – Christoffer Mar 19 '15 at 10:23
  • "You should be able to get all users in a farm from the user profiles": that is wrong. You may have users without user profile even if the User Profile Service is installed, and we don't know from the question, if it is installed and if the AD-sync is configured for all AD users. But one may have SharePoint users from a non-AD-based claims provider as well. The other suggestion (the one from @Varun Verma) is restricted to a single web application, does not cover the whole farm. – pholpar Mar 19 '15 at 10:29
  • For this purpose, I think varun Verma Solution could be a good one. Because I don't need any user who is out of profiler. But I see running my that it does't output every userProfie into sharepoint. Do you if there any MAX filter in it? – hello B Mar 19 '15 at 10:58
  • You mean the solution proposed by @user19952, don't you? I'm not aware of any max. filter there, but could you please share the code you have? The user profiles you do see on the UI, should be all returned by code. However, if there is no automatic synchronization with the full AD, there migth be users without profile. – pholpar Mar 19 '15 at 11:09
0

static void Main(string[] args) {

    using (SPSite site = new SPSite(ConfigurationManager.AppSettings["server"]))
    {
        ServerContext context = ServerContext.GetContext(site);
        UserProfileManager profileManager = new UserProfileManager(context,true);

        foreach (UserProfile profile in profileManager)
        {
            Console.WriteLine(profile[PropertyConstants.AccountName]);
        }
        Console.Read();
    }
}
|improve this answer|||||
0

You should not iterate through members of the SPGroup objects and SPWeb objects.

You should iterate through the web applications, their site collections and the users of each site collections as shown in the code below:

var webServices = SPFarm.Local.Services.Where(s => s is SPWebService).Select(s => s as SPWebService);

foreach (SPWebService service in webServices)
{
  foreach (SPWebApplication wa in service.WebApplications)
  {
    Console.WriteLine("=== Web application: {0}", wa.Name);
    var siteIds = wa.Sites.Select(s => s.ID);
    foreach (var siteId in siteIds)
    {
      using (SPSite site = new SPSite(siteId))
      {
        Console.WriteLine("------ Site Url: {0}", site.Url);
        foreach (SPUser user in site.RootWeb.SiteUsers)
        {
          Console.WriteLine(user.LoginName);
        }
      }
    }
  }
}

Note, that it displays users for the Central Administration web application, and for each MySite as well (if you have any). I don't know if it is desired.

|improve this answer|||||
  • UserProfiler result output could be duplicated or more?? – hello B Mar 19 '15 at 11:10
  • Sorry, I don't understand your question, and what you mean on 'UserProfiler'. There is nothing to do with the User Profiles in my answer. If you meant users, yes, my code can (and probably will) output the login names duplicated. If one does not like that, can create a List<String> and add the login name to the list only if it is not already contained in the list. – pholpar Mar 19 '15 at 11:17
  • Thank you so much. Another question if you can help me. Is there any way to get (from SPUser obj) all permission taht it has on every SecureObject in the farm? – hello B Mar 19 '15 at 11:41
  • Direct from the SPUser you cannot access this info. However, if you iterate through all of the securable objects in your farm, it should be possible. Since this might require a really long time, I would not recommend this approach, only for reporting processes running in the background. – pholpar Mar 19 '15 at 11:53
0

Try below Code

 using ( SPWeb site = new SPSite(SPContext.Current.Web))
                {
                    SPServiceContext serverContext = SPServiceContext.GetContext(site);
                    UserProfileManager userProfileManager = new UserProfileManager(serverContext);
                    Console.WriteLine(userProfileManager.Count);
                    Console.ReadLine();

            }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.