I got a request from the customer to change the background color of a WSS / Portal page depending on the login rights. This example delivers an example which will do this. But the same example can be used to override any other style defined in SPS.css and OWS.css.
The Following control is written using Visual Studio:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
namespace
Customer.SharePoint.WebParts
{
/// <summary>
/// Summary description for BodyColor.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:BodyColor runat=server></{0}:BodyColor>")]
public class BodyColor : System.Web.UI.Control
{
private bool IsEditable
{
get
{
SPWeb web = SPControl.GetContextWeb(Context);
if (web == null)
{
return false;
}
SPUser user = web.CurrentUser;
bool hasRight = web.Permissions.DoesUserHavePermissions(SPRights.ManageWeb);
return hasRight;
}
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
string color = IsEditable ? CustomerConfiguration.EditableBackColor : CustomerConfiguration.NonEditableBackColor;
if (color != "")
{
output.WriteLine("<style>body{ background:" + color + "; }</style>");
}
}
}
}
The control checks the rights of the current user and sets the background color to one of the defined colors in the web.config. Sign the compiled assembly and place it in the GAC. Perform the following changes in the default.aspx file found under the specific template for which you want to override styles.
<!-- _lcid="1033" _version="11.0.5510" _dal="1" -->
<!-- _LocalBinding -->
<%@ Page language="C#" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=11.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="uc1" NameSpace="Customer.SharePoint.WebParts" Assembly="Customer.SharePoint.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c97bf533679d2f6e" %>
<html dir="ltr" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<HEAD>
<META Name="GENERATOR" Content="Microsoft SharePoint">
<META Name="ProgId" Content="SharePoint.WebPartPage.Document">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<META Name="CollaborationServer" Content="SharePoint Team Web Site">
<META HTTP-EQUIV="Expires" content="0">
<Title ID=onetidTitle>Home - <SharePoint:ProjectProperty Property="Title" runat="server"/></Title>
<script src="/_layouts/<%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%>/owsbrows.js"></script>
<Link REL="stylesheet" Type="text/css" HREF="/_layouts/<%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%>/styles/ows.css">
<!--mstheme--><SharePoint:Theme runat="server"/>
<META Name="Microsoft Theme" Content="default">
<META Name="Microsoft Border" Content="none">
<script><!--
if (browseris.mac && !browseris.ie5up)
{
var ms_maccssfpfixup = "/_layouts/<%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%>/styles/owsmac.css";
document.write("<link rel='stylesheet' Type='text/css' href='" + ms_maccssfpfixup + "'>");
}
//--></script>
<style>
v\:* { behavior: url(#default#VML) }
o\:* { behavior: url(#default#VML) }
.shape { behavior: url(#default#VML) }
</style>
<uc1:BodyColor runat="server" id="bc"></uc1:BodyColor>
<link type="text/xml" rel='alternate' href="_vti_bin/spdisco.aspx" />
</HEAD>
<body marginwidth="0" marginheight="0" scroll="yes">
...
</body>
</html>
When the page is requested the control will override the style by writing a style tag.