views:

24

answers:

2

We are creating several (19) sites to be hosted in Sharepoint 2007 and want to use Google Analytics. The first bit was easy, simply putting the Google Analytics javascript in the Sharepoint master page and all worked as expected.

However, we want each of the 19 sites to generate seperate statistics and this is done by having a different id for each site. My first thought was to store the id in each site's web.config and change the master page to read this for the javascript:

<%= ConfigurationManager.AppSettings["google.analytics.key"]%> 

Unfortunately, Sharepoint doesn't like this and says: Code blocks are not allowed in this file.

My question is:

Is there a way to read appSettings from web.config in a Sharepoint master page? or

Is there a better, Sharepoint-way of getting site-specific information into a master page? or

Am I stuck with using seperate master pages for each of the sites?

+1  A: 

SharePoint by default doesn't allow code in master pages, then you will need to mark the masterpages as being safe for code. In your web.config you will have the following setting:

<SharePoint>
<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" totalFileDependencies="50" AllowPageLevelTrace="false">
  <PageParserPaths>
  </PageParserPaths>
</SafeMode>

You will need to change it to include:

<PageParserPaths>
    <PageParserPath VirtualPath="/_layouts/masterpage/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>
</PageParserPaths>

This will tell SharePoint to allow code in the masterpage. You can use the same idea to allow code in other SharePoint locations.

The other option is to use codebehind, but I'm assuming based on your questions that you don't want to do this.

Estyn
That did it! Thanks! Had to change the VirtualPath to "/_catalogs/masterpage/*" because ours is custom, but it works like a charm now!
Ragoczy
+4  A: 

What you proposed is extremely unsafe, as it allows any contributor using sharepoint designer to add any code to the master page, this is never a good idea. I would suggest using a server control in any case, this will take only a few minutes to develop but would save you a lot of headache afterwards

Vladi Gubler
Thanks for the warning. Because we have only two developers working in Sharepoint, we'll use the web.config solution initially, but we'll look into server controls for the future.
Ragoczy