Wednesday, February 10, 2010

AllWebs Access Denied

SPSite MySite = new SPSite(Url_string);
SPWeb MyWeb = MySite.AllWebs[Web_string];
SPList MyPictureList = MyWeb.Lists[List_string];

this code will crash for anonymous users or those without enough permissions

instead do (which returns all but the root site):

SPSite MySite = new SPSite(Url_string);
SPWebCollection UserSites = MySite.OpenWeb().GetSubwebsForCurrentUser();
SPWeb MyWeb = UserSites[Web_string];
SPList MyPictureList = MyWeb.Lists[List_string];


or even better:

SPSite MySite = new SPSite(Url_string);
SPWeb MyWeb = MySite.OpenWeb(Web_string);
SPList MyPictureList = MyWeb.Lists[List_string];

Source
http://wssdevelopment.blogspot.com/2008/01/access-denied-when-using-spsiteallwebs.html

No comments:

Post a Comment