Patrick's SharePoint Blog

SharePoint's Booming world

Posts Tagged ‘Branding’

Personal Edit Mode Panel

Posted by Patrick Boom on February 24, 2011

Sometimes you encounter stuff in SharePoint that makes you go, ‘huh’? While I was working on the publishing features of SharePoint 2010, a client requested to have custom area on their Intranet where end-users could place their own web parts. Quite easy to achieve by just creating a WebPartZone and have the AllowPersonalization property set to true. So far as so good, but end-users now had to use the Welcome menu (the menu on the top right with your name) to enter the Personal Edit mode. But why not through a button, close to the zone itself that you can also style the way you want?

In short, you only need to call the javascript that is also called if you use the Welcome menu. This javascript is called ChangeLayoutMode(bool personalEdit). If you pass true, the interface is set into personal edit mode, which allows for editing of the WebPart zone with AllowPersonalization set to true. Passing false will set the interface into full edit mode, provided you have permissions of course.

Great, but we also want the button or link to change once in this mode to close this interface and switch to normal view again. Guess what? No such method or button. You close it by navigating away from the page and going back or reload it without QueryString parameters.

Ok, there should be an easier way to do this right? Asking end-users to just navigate away seems odd, so we need to provide a button that does just that, but looks like closing the edit mode. SharePOint comes with the EditModePanel out of the box and although its behavior has been changed slightly since SharePoint 2007, I want something similar for the personal edit mode. That is, just a control that we can include in the master page that shows its contents based on the edit mode active.

Let’s start coding 😉

Setting up the solution

Open Visual Studio 2010 and create a new empty SharePoint project. We will not add any features in this solution. Instead, we just add three classes to the root of the project. I called them PersonalModePanel, PersonalModeDisplay and PersonalModePanelDesigner. The first class will be our control. The second class will contain an enumeration for the different display modes. The last class will be the designer class to enable design time support.


PersonalModeDisplay.cs

Let’s start with the easiest one, although none of the classes are difficult. The PersonalModeDisplay is an enumeration that defines the display modes that my control supports. In short, there are only two:

  • Display
  • Edit

The first one is the normal state where the UI is not in edit mode, at least not for the personal sections. The second is his counterpart, where the UI is personal edit mode. See the class below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Boom.ServerControls {

   public enum PersonalModeDisplay
{
        Display=1
     
, Edit=2
   }
}

PersonalModePanelDesigner.cs

The PersonalModePanelDesigner class will provide the designer instructions on how the render the control at design time. It inherits from ContainerControlDesigner and just override the FrameCaption and FrameStyle properties. See below:

using System.Security.Permissions;
using Microsoft.SharePoint.Security;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Globalization; 

namespace Boom.ServerControls {

[PermissionSet(SecurityAction.LinkDemand, Name = “FullTrust”), SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public class PersonalPageModePanelDesigner : ContainerControlDesigner {
   // Fields
   private Style frameStyle;

   // Properties

   public override string FrameCaption {
      [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
      get {
         return “PersonalModePanelFrame”;
      }
   }

   public override Style FrameStyle
{
   [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
   get {
      if (this.frameStyle == null) {
         this.frameStyle = new Style();
         this.frameStyle.Font.Name = “Tahoma”;
         this.frameStyle.Font.Size = new FontUnit(“XSmall”, CultureInfo.CurrentUICulture);
         this.frameStyle.BackColor = Color.LightBlue;
         this.frameStyle.ForeColor = Color.Black;
   }

   return this.frameStyle;
}
}
}
}

PersonalModePanel.cs

So, now on to the real code. At this point I might disappoint you, as again, nothing fancy going on in here. First the code:

using System.ComponentModel;
using System.Web.UI;
using System.Web;
using Microsoft.SharePoint.Security;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web.UI.WebControls.WebParts;
using System;

namespace
Boom.ServerControls
{
   [Designer(typeof(PersonalPageModePanelDesigner))
   , ParseChildren(false)
   , ToolboxData(“<{0}:PersonalModePanel runat=server></{0}:PersonalModePanel>”)
   , AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)
   , SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)
   , AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)
   , SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
   public class PersonalModePanel : Panel, INamingContainer, IParserAccessor {
      // Fields
      private bool shouldRender;

      // Methods
      [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
      protected override void AddParsedSubObject(object obj) {
         this.CalculateShouldRender();
         if (this.shouldRender) {
            base.AddParsedSubObject(obj);
         }
      }

      private
void CalculateShouldRender()
{
         try {
            HttpRequest request = HttpContext.Current.Request;
            if (request.QueryString.Get(“PageView”) == “Personal”) {
               // At least in edit mode for personal sections.
              if (this.PersonalModeDisplay == PersonalModeDisplay.Edit)
                 this.shouldRender = true;
              else
                 this.shouldRender = false;
            }
            else if (this.PersonalModeDisplay == PersonalModeDisplay.Display) {
               this.shouldRender = true;
            }
            else {
               this.shouldRender = false;
            }

            this.Visible = shouldRender;
        }
        catch (Exception ex) {
           this.Visible = true;
        }
        } 

       [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
    public override void RenderBeginTag(HtmlTextWriter writer) {
       if (!this.SuppressTag) {
          base.RenderBeginTag(writer);
       }
    }

    [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
    public override void RenderEndTag(HtmlTextWriter writer) {
       if (!this.SuppressTag) {
          base.RenderEndTag(writer);
       }
   }

   // Properties
   [Category(“Appearance”), DefaultValue(1)]
   public PersonalModeDisplay PersonalModeDisplay {
      get {
         object obj2 = this.ViewState[“PersonalModeDisplay”];
         if (obj2 != null) {
            return (PersonalModeDisplay)obj2;
         }
         return PersonalModeDisplay.Edit;
      }
      set {
         this.ViewState[“PersonalModeDisplay”] = value;
      }
   }

   [Category(“Appearance”), DefaultValue(false)]
   public bool SuppressTag {
      get {
         object obj2 = this.ViewState[“RenderTag”];
         return ((obj2 != null) && ((bool)obj2));
      }
      set {
        this.ViewState[“RenderTag”] = value;
      }
   }
}
}

First, in the class attributes we specify the designer for our control that is of type PersonalModePanelDesigner obviously. We also add some toolbox data, which defines the tag that will be rendered by the intellisense in Visual Studio, in this case <{0}:PersonalModePanel>. The ‘{0}’ just indicates that the designer will insert the TagPrefix here that you will set when registering the control. We will do that later. The rest are security headers that I have just taken from the EditModePanel class.

The class further contains several properties PersonalModeDisplay and SuppresTag that will also be provided to the intellisense and are therefore attributes to our control tag. The real code is located in the CalculateShouldRender method, where we define whether or not the contents of the control should be rendered based on the settings of the control.

CalculateShouldRender method

My first approach was to get the Page object of this page and request the WebPartManager to provide the PersonalizationScope of the page. Turns out that requesting this.Page returns null in the stage of the page lifecycle we are in, so that was a dead end. Instead, I used the querystring method where I just look for the PageView=Personal parameter to be present. If so, the contens should be rendered only if the PersonalModeDisplay is set to Edit. In all other cases, it should not render. Finally, I assign the shouldRender variable to the visible property of the class. And we are done!

Deploy and use the code

Now that we have our control, we can use it in our page designs and layouts. Deploy the assembly to your server using the WSP and open a page layout in SharePoint designer. Register the control in the page by providing the following tag at the top of the page:

<%@ Register Tagprefix=”Boom” Namespace=” Boom.ServerControls” Assembly=”Boom.ServerControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2847780e0448016d6″ %>

When done, we can insert our control in the page, for example like this:

<Boom:PersonalModePanel runat=”server” PersonalDisplayMode=”Display” SuppressTag=”true”>This is only shown in Display mode</Boom:PersonalModePanel>

<Boom:PersonalModePanel runat=”server” PersonalDisplayMode=”Edit” SuppressTag=”true”>This is only shown in Edit mode</Boom:PersonalModePanel>

 Conclusion

So now we have a control that can show different contents based on the personal edit mode of an end-user. For example, we can provide some more instructions in such a panel that is only shown when he or she enters their personal edit mode. Or we can provide different controls to style and control the editing experience. It is just handy 😉 Let your imagination do the talking and use this wherever you like.

Get the VS solution here.

Thanks for making it this far 😉 Leave some comments if you have time!

Advertisements

Posted in SharePoint 2010 | Tagged: , , , | 1 Comment »

Styling the Welcome menu

Posted by Patrick Boom on February 22, 2011

Sometimes I wonder why MS makes some things so hard to do. During the styling of our project, we wanted to move the Welcome menu, also known as the user menu or Personal Action menu to another location on the page.

The control is easy to find and is basically a user control located in the ControlTemplates folder in the 14 hyve. However, that control only defines the menu structure of the control, not the appearance. I started to worry a little, as that probably means that the appearance of the control is defined in code.

Posts by Erik Svenson and Shehan Peruma showed how to change the link color. But I hope that is not the only stuff you can change in the control. How would you ever integrate this into a new publication design?

Hence, I decided to call my favourite friend called Reflector again and dive into the code of SharePoint. Turns out the behaviour of the control is determined by two classes in the CSS, called ms-SPLink ms-SpLinkButtonActive ms-welcomeMenu and ms-SPLink ms-SpLinkButtonInActive ms-welcomeMenu, controlling the active and inactive state respectively.

Basically, the most important part to look for in the Corev4.css (in the Themable folder for publishing sites) is the ms-welcomeMenu class and the classes derived from that. Controlling the font color, family, background and other options is quite easily done through these classes:

.ms-welcomeMenu {
   padding:2px 5px 3px;
   margin:0px 3px;
   font-size:1em;
   font-family:Verdana,sans-serif;
   border:0px solid transparent;
   display:inline-block;
}
.ms-welcomeMenu a:link {
   color:#00259b;
}
.ms-welcomeMenu a:hover {
   text-decoration:none !important;
}
.ms-welcomeMenu.ms-SpLinkButtonActive {
/* [ReplaceColor(themeColor:”Dark2-Lighter”)] */
/* [RecolorImage(themeColor:”Dark2-Lightest”,includeRectangle:{x:0,y:489,width:1,height:11})] */
   background: url(../img/backgrounds/bg-sprite-tabbar.png) 100% 0 no-repeat;
/* [ReplaceColor(themeColor:”Dark2″,themeShade:”0.8″)] */
  background-color:#21374c;
}
.ms-welcomeMenu.ms-SpLinkButtonActive a:link {
/* [ReplaceColor(themeColor:”Light1″,themeTint:”0.9″)] */
   color:#00259b;
}

What I did in above CSS override is changing .ms-welcome a:link and .ms-welcome a:hover to change the font color of the inactive state.
To change to color of the font as soon as the mouse is over it, add the style ms-welcomeMenu.ms-SpLinkButtonActive a:link.

Secondly, I have overridden the background in .ms-welcomeMenu.ms-SpLinkButtonActive to show a custom made image. If IU left it, it would show the dark background of the top bar, which is something I do not want on a white background ;-).

By default, the ms-welcomeMenu has a transparent background in the inactive state, as it should be and therefore blends in into its container. Remember though that you should override these classes in a separate CSS file and be sure it is loaded last (or at least after Corev4.css) in the master page. Also, you cannot remove any styles, just override the settings already made.

Finally, you can do the same for the SiteActions menu. You can do that by looking for the classes called .ms-siteactionsmenu.



Posted in SharePoint 2010 | Tagged: , , , , | 11 Comments »

Branding SharePoint Sites, how to get started

Posted by Patrick Boom on February 17, 2011

Many of us out there have encountered it before; How do I make SharePoint look the way I want it to, without destroying any of the functionality that comes out of the box? In my current project, we face the same challenges that leave us sometimes puzzled on why Microsoft has chosen certain design approaches.

One of those things is the use of table style design, especially in the WebPartZone and WebPartChrome controls. It really frustrates designing based on DIV’s, which is the standard nowadays.

The second thing is the complete overload on CSS that SharePoint applies to pages to handle the styling of the various elements. In one way, this allows for very detailed control over UI elements, but in the other hand makes it almost impossible to apply a generic style across all pages and controls.

Fortunately, I was not the only one facing this issue. So here are some of the links I found very useful when starting to design Intranet en Internet web sites based on SharePoint 2010.

To start off, Andrew Connell and Randy Drisgill provided a quite extensive article on how to approach Branding with SharePoint 2010. This article is excellent and will provide you a flying start in any design and helps to understand the various elements contained in a SharePoint master page and the styles that are applied to them.

When web designers start developing their front-end design their first course of action is to reset all the css styles that are applied by default. Doing that on SharePoint will, well, confuse SharePoint to say it mildly. Kyle Schaeffer has developed a reset css specific for SharePoint 2010 that gives front-end designers a head start in applying their custom css on SharePoint.

CSS is basically the keyword in styling SharePoint. Not by introducing your own classes, but understanding the 200+ classes Microsoft introduced themselves and which does what. One of the absolute guru’s on this front is Heather Solomon that also provided a complete reference to all the styles that are applied to certain UI elements and what they said. A must read for any developer that needs to style SharePoint. Unfortunately, only for SharePoint 2007 now, but I am sure she will also provide one for SharePoint 2010.

Master pages and Page Layout form the basics on any SharePoint site and customizing can be challenging. Andrew and Randy provide a good starter.master that has all the essential elements in place and also contains good comments on which section does what. Couple of things I encountered that are crucial:

  • Do not remove the s4-workspace, s4-bodyContainer and MSO_ContentDIV divs.
    Doing so will really mess up your user experience with SharePoint, especially when entering the edit modes.
  • When going for a fixed width site, which is quite common these days, apply the s4-nosetwidth class to your workspace div. This will instruct the javascript behind the scenes not to touch the widths set in the CSS definitions.
  • Use the s4-notdlg class for any element you do not want to show in the modal dialogs of SharePoint. Failing to do so will show your beautiful header in full power within small dialog boxes.
  • Accept that SharePoint controls output their HTML in a certain way. Yes, some use tables to structure it, deal with it. If have played around with ControlAdapters and basically found that they do more damage than they solve. For one thing, because a lot of the html rendering methods of controls are a) internal and b) sealed, there is no way of reaching it, only to completely override it. Thing is, they introduce the object DOM in the HTML that you will then omit…and shake up SharePoint.

In short, styling SharePoint can be a tedious task. With these links, I hope you guys get in the right direction quick. I wished I knew them before finding out the hard way 😉

CU

Posted in SharePoint 2010 | Tagged: , , , | 1 Comment »

 
%d bloggers like this: