SharePoint - Change CSS based on user's domain

Asked By Chester Thomas on 24-Nov-11 04:18 AM
Hi experts!

I have a site collection which has 5 domains that have trusts and are able to access the different sites. How can I set it up such that when a user from domain 1 accesses the site then stylesheet 1 is used, user from domain 2 - stylesheet 2, domain 3 - stylesheet 3 and so on....Am I able to specify it in the master page or programmatically somewhere?

Thanks.

Chester
Jitendra Faye replied to Chester Thomas on 24-Nov-11 04:23 AM

You can use different master pages to accomplish this.

Code a default against one master page (or set of masters) and then change the master page programmatically at runtime based on the host domain.

To do this you must set the page's MasterPageFile property during the OnPreInit phase of the page's lifecycle. (After that it is not allowed because the master has been loaded.)

The master page that is selected can have drastically different layout and reference completely separate CSS files.

Chester Thomas replied to Jitendra Faye on 24-Nov-11 04:26 AM
Thanks, do you have an example for me to use? I am not so sure how to do it.
Jitendra Faye replied to Chester Thomas on 24-Nov-11 04:44 AM

The Steps to Create a Custom httpModule for Changing master page for a logged in user are :


1. Create a new Class Library project in Visual Studio name it as CustomhttpModule

2. Add the below code in your class file.

using System;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;

namespace SwitchMasterPage{
public class CustomHttpModule : IHttpModule
{

public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}

void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
// register handler for PreInit event
page.PreInit += new EventHandler(page_PreInit);
}
}

void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;

if (page != null)
{
SPSite site = SPContext.Current.Site;

using (SPWeb web = site.OpenWeb())
{
if (web.CurrentUser != null)
{
SPGroupCollection userGroups = web.CurrentUser.Groups; // Check all the groups user belong to
foreach (SPGroup group in userGroups)
{
if (group.Name.Contains(“OurCustomgroupName”)
// Switch the master page.

page.MasterPageFile = “/_catalogs/masterpage/MyCustom.master”;

}}}

}}
public void Dispose() { /* empty implementation */ }
}
}

it’s important to remember that an HttpModule cannot be deployed in a WSS farm for an individual site collection. Instead, an HttpModule must be configured as an all-or-nothing proposition at the Web application level.

3. Now, sign the project and build it.


4. Drag and Drop the signed assembly in GAC.


5. Next, we need to register this CustomhttpModule in our SharePoint webconfig. To do this add the below under <httpModules> tag in your web app’s web.config fie.

<add name=”CustomHttpModule” type=”SwitchMasterPage.CustomHttpModule,  SwitchMasterPage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7ebdb1031dfc1e406″/>

Hope this will help you.

dipa ahuja replied to Chester Thomas on 24-Nov-11 06:52 AM
Hi.. You can create css file in asp.net  and even you can use here a better concept that is themes.
A theme is contain two types of file 1. css and 2. skin . You can choose css by using the cssClass property and Skin for SkidID
 
To add css file is easy. Just Add new item->Css file
 
add some classes to the css file. You can even write the css class without creating new file, and inside the
 
<style type="text/css">
 
.div1
{
z-index:1;
position:absolute;
}
</style>
 
► applying css file is also easy just by the
 
for html control
 
<div class="div1">
 
for server control
<asp:Image ID="Image3" CssClass="imgBig" runat="server" ImageUrl="~/1.bmp" />
 
► Using SkinFile
 
    
You can implement thems by adding new .skin file in your website:
 
1. Add one skin file in your website:
2. It will ask you to add in App_Themes folder say yes
3. Add the code in your skin file
Just for beginning add the code :
 
<asp:button runat="server" BackColor="lightblue" ForeColor="black" />
 
And save it. Now in you webpage in Page Directive add the Theme property = skinfile ex:
 
<%@ Page Theme="SkinFile" Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs"Inherits="Default5" %>
 
Add one button in your webpage. And Run the page. You will get the effect of button as you have provided in the theme file
 
► Apply skin from code behind
 
protected void page_PreInit(object sender, EventArgs e)
  {
    Page.Theme = "SkinFile";
  }