Opening the current SPWeb
I used to open the current SPWeb like so:
However, if you access a SharePoint site with a URL that isn't in the alternate access mappings of the server, then the above code will create an error.
OR
if you try catch it:
To avoid this error, you can get the default site and web by using the ID instead of the URL:
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
}
}
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
}
}
{
using (SPWeb web = site.OpenWeb())
{
}
}
However, if you access a SharePoint site with a URL that isn't in the alternate access mappings of the server, then the above code will create an error.
|
OR
if you try catch it:
The Web application at http://[invalid aam site] could not be found. Verify that
you have typed the URL correctly. If the URL should be serving existing content,
the system administrator may need to add a new request URL mapping to the
intended application.
To avoid this error, you can get the default site and web by using the ID instead of the URL:
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
}
}
Comments
Post a Comment