CSC510238. Missing SPUrlZone parameter in SPSite constructor.

Description

The SharePoint object model always creates new SPSite objects with the zone set to Default, even if the code that's creating the object is running under a SharePoint extended web application associated with a zone other than Default.

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{ 
   SPUrlZone zone = site.Zone;
   //Logic that depends on Zone
   //Oops! Zone is always 'Default'
}

The issue is that when you run this code, you will always get the Zone with the value "Default"—which is fine as long as you are running the code in the URL of a SharePoint web application which is in the "Default" zone. The problem is that even if you use the URL of a web application which is in the Internet Zone (or indeed, any other zone) the code will still return the Default zone.

Resolution

You have to specify SPUrlZone while creating new instance of the SPSite via new SPSite(Guid, ...) constructor.
To know the zone of a request you can use the SPContext.Current.Site.Zone class. This returns an enum of SPUrlZone which represents wether its a Default zone, Intranet Zone, Internet Zone, Custom Zone or Extranet Zone.

using (SPSite site = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.Zone))
{
   SPUrlZone zone = site.Zone;
   //Logic that depends on Zone
   //Zone will contain the correct Zone depending upon the Context
}

Links

SPSite Constructor
Translating URLs using Alternate Access Mappings from code

Last edited Jun 9, 2014 at 9:12 PM by dvd73, version 6

Comments

No comments yet.