April 06, 2009

How to access the User Information List programmatically

To acces the hidden User Information List list of a SharePoint site (only accessible by administrators), the best way is to use the SiteUserInfoList property of the SPWeb object as in the following example:

using (SPSite mySiteCollection = new SPSite("http://myserver/myspsite"))
{
    using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))
    {
        SPList usersList = mySite.SiteUserInfoList;
    }
}

Note that there is another way to acces this list but I don't recommend it because it can throw an exception in a couple of situations:

using (SPSite mySiteCollection = new SPSite("http://myserver/myspsite"))
{
    using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))
    {
        SPList usersList = mySite.Lists["User Information List"];
    }
}

1 comment:

Silvia Jacinto said...

Every time I visit your blog it really completes my day, and hey its not a joke. I am telling the truth. Thank you for always inspiring us and for writing a very touching article.

zandra

www.n8fan.net