/// <summary>
/// Gets the collection of all the sites in the site collection.
/// </summary>
/// <param name="site">A SharePoint site collection</param>
/// <returns>The collection of all the sites in the site collection</returns>
public static SPWebCollection GetSPWebCollectionFromSite(SPSite site)
{
SPUserToken token = site.SystemAccount.UserToken;
SPSite elevatedSiteCollection = new SPSite(site.ID, token);
return elevatedSiteCollection.AllWebs;
}
/// <summary>
/// Gets the collection of all the sites in the current site collection (so this is context specific).
/// </summary>
/// <returns> Collection of all the sites in the current site collection</returns>
public static SPWebCollection GetSPWebCollectionFromCurrentSite()
{
return GetSPWebCollectionFromSite(SPContext.Current.Site);
}
Im now just noticing that we dont check against current user permissions, but so far from our testing this hasnt been a problem.
If you are outside of the SPContext then use this stuff instead. This assumes that you can get the URL for the site, or whatever resource you are trying to access. In our case we are trying to get at a file, so if you pass in the full url of the file, it will pick the correct spsite for you.
Outside a sharepoint context:
String fullURL = "http://mysharepointsite.com/someDocLib/somefile.docx";
using (SPSite templateSiteCol = new SPSite(fullURL)){
using (SPWeb templateWeb = templateSiteCol.OpenWeb()){
}
}
Both of these solutions are used internally for accessing files, but it should apply to other stuff that you need access.
We have tested both pieces of code with alternate access mappings, and all classes of users from visitor on up to site owner.
Comments