Get a List using its ID
Asked Answered
P

1

7

I'm trying to get a list using it's ID, however it's not working and I can't work out why

This works without a problem...

using (SPSite site = new SPSite("http://example.org/sites/specific/staffhandbook"))
using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Documents"];
        // process...
    }

So should this, but it doesn't?

using (SPSite site = new SPSite("http://example.org/sites/specific/staffhandbook"))
using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["29540646-bcab-4beb-8a91-648c1f3178b8"];
        // process...
    }
Poach answered 28/7, 2010 at 14:51 Comment(0)
H
14

The SPListCollection accepts either an Int32 (an index), a String (the name of the list), or a Guid (the identifier), so the above, you'd need to do:

Guid guid = new Guid("29540646-bcab-4beb-8a91-648c1f3178b8");
SPList list = web.Lists[guid];
Hypozeugma answered 28/7, 2010 at 14:56 Comment(1)
doh! even when I was looking at an example I still didn't see it, thanks :-)Poach

© 2022 - 2024 — McMap. All rights reserved.