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"];
    }
}

No comments: