Monday, December 21, 2009

Pass locale of current web site (SPWeb) in Sharepoint into javascript

Technique described below can be actually used for regular ASP.Net applications when you need to pass some server variable into javascript code – i.e. in general it is not Sharepoint-specific. Nevertheless I used it recently to pass locale of current SPWeb into external .js file.

We often use server variables in javascript code when we need to determine ClientID of some server control in javascript code embedded into page:

   1: var txtEmail = document.getElementById("<%= this.txtEmail.ClientID %>")
Unfortunately this approach can’t be used when javascript code is located in external .js file (btw this is recommended approach for web applications development – do not mess layout and javascript code) because .js file is not processed by ASP.Net by default.

The main idea for external .js file is quite simple – use global variable (of course you can use namespaces as well, but in this post I will use global variable for simplicity) which will be available in .js file and which will keep locale of current SPWeb:

   1: <%@ Import Namespace="Microsoft.SharePoint" %>
   2: <script type="text/javascript">
   3:     var globalLocale = "<%= SPContext.Current.Web.Language %>";
   4: </script>

To use this variable in .js file the following function was added:

   1:  
   2: getLocaleOfCurrentSiteOrEmpty = function() {
   3:     var localLocale = "";
   4:     try {
   5:         localLocale = globalLocale;
   6:     }
   7:     catch (e) {
   8:         localLocale = "";
   9:     }
  10:     return localLocale;
  11: }

So those pages which don’t have globalLocale defined will have empty value and then use some default settings.

Keeping in mind DRY principle we didn’t want to copy and past definition of globalLocale variable in each page where it is required. In order to avoid duplication several approaches can be used:

  • include definition of globalLocale variable using server-side include. I.e. you need to move definition into separate .ascx file (extension is not important actually) and then include it in your pages:
       1: <!--#include virtual="GlobalLocale.ascx"-->
    This appoach works only for application pages – i.e. pages which are stored on filesystem in 12/template/layouts folder. It doesn’t work for publishing pages and master pages because server-side includes are not allowed in pages of this type.
  • use regular user control .ascx file, which contains definition of globalLocale variable, and register it as regular user control wherever you need. This approach works both for application and publishing pages.

We used 2nd approach – but if you are not able to use user controls because of some reasons, 1st way is applicable as well.

4 comments:

  1. Hi,

    I've tried your interesting method and when i insert the first javascript inside an tag, i received following error:

    An error occurred during the processing of /sites/pss/edt/Lists/GoAway/Absence/NewFormEmr.aspx. Code blocks are not allowed in this file.

    Could help me to workaround this error?

    Regards.
    François.

    ReplyDelete
  2. The tag where i've inserted first javascript is asp:content...

    ReplyDelete
  3. The solution was here:
    http://blogs.msdn.com/kaevans/archive/2007/04/26/code-blocks-are-not-allowed-in-this-file-using-server-side-code-with-sharepoint.aspx

    ReplyDelete
  4. hello, François
    Seems like you tried to add it to publishing page. I wrote that this technique is not allowed for publishing and master pages by default because of security reasons. But thanks for your link as it contains workaround which allows to use this approach for publishing pages as well

    ReplyDelete