Sharepoint & HttpModule Fehler … wasssss läuft da schief!?!

 In SharePoint

Sooo my job was to create a HttpModule fore Sharepoint 2010 to swich to a alternate masterpage.

And gues what? I didn’t work with the Samples I found in the internet from Microsoft and communities.

I got error messages, failures and finaly of course frustration.

But there is way to get around and to fix the problem with the masterpage switching.

The problem is, the standard samples are nice and everywhere in the internet, but not meant to be used in Sharepoint Publishing Portal

(I wonder why in samples is never explained under whitch condition they don’t work. Thats typical for MS)

So here is my code for the HttpModule

(take care about the reformated quotes, they don’t copy & paste sooo good)

using System;

using System.Web;

using System.Web.UI;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Publishing;

using System.Configuration;

 

namespace MySharepoint.MasterPageHttpModule

{

 

public class MasterPageHttpModule : IHttpModule

  {

 

 ///<summary> 

/// first called code of the module 

///</summary> 

///<param name=”context”></param>

 

public void Init(HttpApplication context)

  {

  context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

  }

  ///<summary>

  /// check if in the current context is a page

  /// I gues that it is also possible that the page object is null and httpcontext call is fired for something else

  ///</summary>

  ///<param name=”sender”></param>

  ///<param name=”e”></param>

  void context_PreRequestHandlerExecute(objectsender, EventArgs e)

  {

  Page page = HttpContext.Current.CurrentHandler as Page;

  if (page != null)

  {

  if(page is PublishingLayoutPage || page is TemplateRedirectionPage)

  {

   HttpCookie cookie = null;

  cookie = HttpContext.Current.Request.Cookies.Get(“MasterpageCookie”);

  if (cookie != null)

  {

  if (cookie.Value.ToString() == “alternate”)

  {

   //page.MasterPageFile would work if it wouldn’t be meant for a publishing page and specially: in the page_PreInit

  // this I use to show for a different root landing page, because this module is designed to handle mobile devices and the

  // standard desktop page is to overloaded formobiles

  if (HttpContext.Current.Request.Url.ToString() == “https://myserver/pages/default.aspx”)

  {

  HttpContext.Current.Response.Redirect(“alternateroot.aspx”);

  }

  // change on the fly the used master page to mobile master page

  SPContext.Current.Web.CustomMasterUrl = “/_catalogs/masterpage/AlternateMasterPage.master”;

  }

  else

  {

  // default master is active if currently loaded is the alternate-root page, redirect to default root page

  if (HttpContext.Current.Request.Url.ToString().Contains(“alternateroot.aspx”)

  {

  HttpContext.Current.Response.Redirect(“https://myserver/pages/default.aspx”);

  }

  }

  }

  }

  }

  }

  ///<summary>

  /// doesn’t work because the page PreInit is never fired within a publishing portal

  ///</summary>

  ///<param name=”sender”></param>

  ///<param name=”e”></param>

  void page_PreInit(object sender, EventArgs e)

  {

  Page page = sender as Page;

  if (page != null)

  {

  // Nice method like in all examples of MS … but doesn’t help for Publishing Portals

  }

  }

 

  public void Dispose()

  {

  }

  }

}

 

Here are for example my links to switch with the HTTP module the cookie

 

<A title=“Switch to alternate masterpage” onclick=“createCookie(‘MasterpageCookie’,’alternate’,30);window.location.reload();” href=“#”>Switch the Masterpage</A>

<A title=“Switch to default masterpage” onclick=“eraseCookie(‘MasterpageCookie’);window.location.reload();” href=“#”>Switch to default Masterpage</A>

Here is for example the stuff for cookie handling

//alternate master page switch cookie stuff, the days parameter means for how long the cookie should be valid*/

function createCookie(name,value,days) {

  if (days) {

  var date = new Date();

  date.setTime(date.getTime()+(days*24*60*60*1000));

  var expires = “; expires=”+date.toGMTString();

  }

  else var expires = “”;

  document.cookie = name+“=”+value+expires+“; path=/”; 

}

 

function readCookie(name) {

  var nameEQ = name + “=”;

  var ca = document.cookie.split(‘;’);

  for(var i=0;i < ca.length;i++) {

  var c = ca[i];

  while (c.charAt(0)==) c = c.substring(1,c.length);

  if (c.indexOf(nameEQ) == 0)return c.substring(nameEQ.length,c.length);

 

  }

  return

null;

}

 

function

eraseCookie(name) {

  createCookie(name,“”,-1);

}

/*ende*/

Neueste Beiträge
Sharepoint Konferenz Wien 2014