1

I am unable to access the sharepoint users and groups using below code to access all the users and groups of site collections of web application including their sub-sites and child sub-sites

Can any one do let me know what's wrong in my below code and how can I solve it

Guid siteId = SPContext.Current.Site.ID;

    SPSecurity.RunWithElevatedPrivileges(delegate(){
                    try
                    {
                        using (SPSite _site = new SPSite(siteId))
                        {
                            SPSiteCollection tmpRootColl = _site.WebApplication.Sites;
                            foreach (SPSite tmpSite in tmpRootColl)
                            {
                                //  using (SPWeb elevatedWeb = _site.OpenWeb(CurrentWeb.ID))
                                //foreach (SPWeb elevatedWeb in _site.AllWebs)
                                foreach (SPWeb elevatedWeb in tmpSite.AllWebs)
                                { 
                                }
                              }
                          }
                       }
0

you're trying to loop through out web applications with current site's application pool account.

SPSecurity.RunWithElevatedPrivileges uses current site (in your case SPContext.Current.Site) application pool account while you're trying to access farm level web applications.

Check if your site application pool account has enough rights to access other web applications. Usually this is SP_farm account.

Check if following code can help you,

SPFarm farm = SPFarm.Local; 
SPWebService service = farm.Services.GetValue<SPWebService>(""); 
foreach (SPWebApplication webApp in service.WebApplications)
{
   //Your Code here    
}

Andrew

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.