• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » Backend (Server-Side) » Loop Through SharePoint Farm To Find A List

Loop Through SharePoint Farm To Find A List

By Ehi Kioya Leave a Comment

The idea here might look really crazy. But on a few occasions, I’ve had to loop through an entire SharePoint Farm in search of a list with a unique title (and then create the corresponding SPList object).

One time I’ve had to do this was when passing control to an aspx page in the layouts folder from a modal dialog that was just closed.

A common way I would normally get my SPList object is to create it from an SPWeb object using spweb.Lists.TryGetList(). In this particular case, however, the SPContext object held no useful information that could help me easily grab a relevant SPSite or SPWeb object.

So here’s how I did it:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPServiceCollection services = SPFarm.Local.Services;
    foreach (SPService curService in services)
    {
        if (!(curService is SPWebService))
            continue;
        SPWebService webService = (SPWebService)curService;
        foreach (SPWebApplication webApp in webService.WebApplications)
        {
            for (int i = 0; i < webApp.Sites.Count; i++)
            {
                using (SPSite site = webApp.Sites[i])
                {
                    using (SPWeb masterWeb = site.RootWeb)
                    {
                        SPList list = null;
                        list = masterWeb.Lists.TryGetList("Your List Title");
                        if (list != null)
                        {
                            // Now you can work with the SPList object here.
                        }
                    }
                }
            }
        }
    }
});

It’s very possible that there is an easier way of handling this situation and achieving the same results. Do you know a simpler method? Please post it in your comment below.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: Backend (Server-Side), News, SharePoint Tagged With: Programming, SharePoint

About Ehi Kioya

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

23,567
Followers
Follow
30,000
Connections
Connect
14,568
Page Fans
Like
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • Fix For “Function create_function() is deprecated” In PHP 7.2 Fix For "Function create_function() is deprecated" In PHP 7.2
    As of PHP 7.2 create_function() has been deprecated because it uses eval(). You should replace it with an anonymous function instead.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • About
  • Contact

© 2022   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy