Friday, May 9, 2014

Sitecore: Multi-site Setup with Bing Analytics Files (BingSiteAuth.xml)

Recently, we ran into a dilemma involving a Sitecore multi-site environment.  As we know, this means, one instance of Sitecore, one content tree, one code base, one root directory, but multiple sites reading from different nodes in the tree.  Since we only have one code base running on one instance of IIS, how can we possibly have different files for each domain in the root level?

Simply put, Bing analytics requires that each website has a file called "BingSiteAuth.xml" be dropped into the root directory of each website.  This file contains a unique ID number that the Bing search engine uses to identify your site as legitimate.  In a normal single website instance with or without Sitecore, we can just copy this file into the root directory.  Easy enough, problem solved.  But in a multi-site Sitecore setup with one code base shared among all your sites, how can this be achieved?  All your sites would need to have a file with the same file name but since all the sites share a common set of files, all you can have is one copy of that file shared by all your sites.  This is not just a Sitecore dilemma, but a dilemma for all CMS sites that have the multi-site setup option.  Of course if Bing altered their way to make sure every file have unique names, that would be great so you can have 50 files with different names for 50 different sites.  But this is not the case, so we have to find a way to get around it.

One way to achieve this is by creating an Http Handler.  As we all know, an Http Handler basically tells IIS what to do when a file with a specific name or extension is encountered.  For our purposes, we would need to create a handler for the "BingSiteAuth.xml" file name.  Let's begin.

1) Create a new handler class that inherits from System.Web.IHttpHandler:



2) You must implement ProcessRequest and IsReusable methods to satisfy the interface.

3) In ProcessRequest, this is where you do the bulk of the work.  You need to detect the current website and output some text.  We have decided not to stream a file, but rather save the file information in a text field in each of the website home content items.  But since this is a standard Http Handler and not something in Sitecore, we do NOT have access to the Sitecore context because it is not yet resolved at the time the handler is fired.  What do we do now?  Rest assured that even though the context is not available, we can use Sitecore factory methods to pull up site information in multiple steps.  All you have to do is figure out if the "sc_site" parameter is set.  If it is, we have the site name.  If not, we could use the domain name to perform the lookup.  Of course, all this information has to match the <sites> defined in the web.config file.



At this point, we should have the site information.  Now we need to get at the starting node of this site and check the field that contains the text for the BingSiteAuth.xml file.  If the field is not defined or there is no value for the field, we throw a 404 exception.



4) Now we have the working handler but we have to register he handler.  To do that we have to append an entry to the web.config section in two places:

<system.webServer><handlers>

and

<system.web><httpHandlers>



Keep in mind that the same entry is used in both places except the version inside httpHandlers does not have the name attribute.

5) Make sure to modify the template of the home item to include a field called "BingSiteAuth".



6) Rebuild your code and try browsing to:

http://www.mycoolsite.com/BingSiteAuth.xml.  You should see a file with the exact information stored in the field above.






No comments:

Post a Comment