How to retrieve the contents of a file in SharePoint no matter its location?

Imagine you want to retrieve the contents of a non-binary (text, CSS, XSL, etc.) file stored in SharePoint 2007 and all you know about that file is its location (URL). How would you do it?

The answer is way simpler than you might think: SPWeb.GetFileAsString(string) allows you to obtain the contents of the file at the given URL. But that’s not all. Using SPContext.Current.Site.RootWeb.GetFileAsString you can obtain even files which are stored in subwebs! Now isn’t that just cool?!

Technorati Tags: SharePoint, SharePoint 2007, WSS 3.0, MOSS 2007