One of these days I had to do some bugfixing on a Sharepoint project which used the Microsoft.SharePoint.Linq.DataContext class.
While trying to access a SPList belonging to Site Collection another Site Collection I always had an “Invalid Url Exception” right from the contructor, and other times I had exceptions when accessing the objects.
var context = new DataContext(otherSiteColUrl);
The problem is that inside, the constructor, the URL is only taken into account if we have no HttpContext. If we do have one, the context URL (which means, the current site url) the custom url passed on the constructor is ignored.
The workaround is to “unset” the http context and then set it back.
This way the url we pass on the contructor is taken into account:
//save the context var context = HttpContext.Current; HttpContext.Current = null; var context = new DataContext(otherSiteColUrl); ..... //put the context back HttpContext.Current = content;
Altough this is not a pretty solution, this solves the problem.