SharePoint world of ECM and Information Management

July 2008 - Posts

ASP.NET Theme across all pages with XMLHttpRequest calls

ASP.NET Themes is very powerful feature, but some limitations exist when you are trying to apply Themes dynamically.

For the single page the way to apply theme is to use

Page.Theme = “<themeName>”;

inside  the OnPreInit event, but if you try to apply theme for all pages across your site you have two solutions:

  1. add this code to each of the pages
  2. apply theme for master.page – and it’s where problem starts

ASP.NET master pages don’t support applying themes dynamically – only static support for the all site pages by setting attribute inside web.config.

But what to do if you need control which theme to apply and you don’t want to add code to every page of your site?!

The solution for this to create custom HttpModule, where apply theme for Page class inside PreRequestHandlerExecute event.

See the following code-snippet of the ThemeHttpModule

   1: public void Init(HttpApplication context)
   2: {
   3:     context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
   4: }
   5:  
   6: void context_PreRequestHandlerExecute(object sender, EventArgs e)
   7: {
   8:     HttpContext context = HttpContext.Current;
   9:     if (!(context.Handler is Page))
  10:     {
  11:         return;
  12:     }
  13:  
  14:     Page page = (Page)context.Handler;
  15:     
  16:     if ((page != null))
  17:     {
  18:         string themeName = "Default";  // get theme name from somewhere
  19:         page.Theme = themeName;
  20:     }
  21: }

Done. Now you have flexible control which theme to apply in realtime, extracting you theme names from config or from database.

But there is one case, I’d like point you attention – XMLHttpRequest calls.

If you have AJAX XMLHttpRequest direct calls - this code won’t work, because server returns you only portion of page, not the full page with header and body. And you can’t apply theme on partial pages.

To solve this you need to check if it’s partial request or not, inside your HttpModule. In case of ASP.NET AJAX you could use ScriptManager.IsInAsyncPostBack, but for the direct XMLHttpRequest  calls you need to check the header for the Content-Type you are used when create the MSXML object.

Something like this

   1: // Don't apply theme for XMLHttpRequest calls, because you are getting part of the page, not the full page
   2: if (! string.IsNullOrEmpty(context.Request.ContentType))
   3: {
   4:     return;
   5: }

Now your themes will be applied only on pages, not the partial AJAX calls.

 

Mirror: ASP.NET Theme across all pages with XMLHttpRequest calls

Posted: Mon, Jul 28 2008 1:43 by Michael | with 2 comment(s)
Filed under: ,
Moving ASP.NET master.page to SharePoint

In these days SharePoint become more and more popular. There are number of web project which starts directly in SharePoint, but however a lot of project remains out the SharePoint scope, and exists as pure ASP.NET application.

The one of the common task of migrating ASP.NET application to SharePoint is changing the site landing page to become SharePoint page. But it’s where the problem starts. ASP.NET applications tend to consume different modern tools and technologies like ASP.NET MVC, SilverLight, URL Rewriting and others which seems to be very common in pure ASP.NET development, but could be the potential problems in SharePoint world.

There are three thing you need to concentrate on

1) master.page

  • Remove AutoEventWireup="true" attribute. SharePoint doesn’t support it on all pages;
  • User controls are not supported on master.page, so move them to CONTROLTEMPLATES folder and change references
  • Check that all classes you inherit from has the namespaces

2) CSS

  • SharePoint doesn’t support CSS validation on 100%, so I can hardly recommend something specific here. But what did helped me a lot id debuging and CSS customization is just to have the “clean” CSS file, which resets all customization http://meyerweb.com/eric/tools/css/reset/

3) Web.config

  • You need to make sure that you are using the right siteMap defaultProvider (<siteMap section>) from your ASP.NET web.config, otherwise all your SiteMap elements will be considered as Sharepoint Publishing classes elements (if u have MOSS), which are differs from asp.net SiteMap classes
  • If you are using URL rewriting or other specific modules you need to create the <system.webServer> section in SharePoint config file and add them there.
  • You need to add all your DLLS to the list of SafeControls

4) Code

  • You need to have [assembly: AllowPartiallyTrustedCallers()] in your dlls.
  • DLLs must be signed
  • All classes must have namespaces

 

Mirror: Moving ASP.NET master.page to SharePoint

2008 MVP Award

Hurray, got the 2008 MVP Award today! They re-awarded me again, since year 2005 in [.NET/C#] area for my achievements in “online consulting” ;)

But this year I’m Australian MVP!

 

Thinking about changing the area and become SharePoint MVP in the next year :)

 

Mirror: 2008 MVP Award

Posted: Wed, Jul 2 2008 10:05 by Michael | with 1 comment(s)
Filed under: ,