Monthly Archives: February 2012

Chapter 4-3: Page Layouts

Field Controls

Another element you can place in content place holders in a page layout are field controls. Field controls are determined by the site columns which are established through the content types. For instance, if you use an article page layout, you will only see the field controls that are available to the article page layout content type. For now, we will walkthrough how to add field controls to the code of page layouts through Designer. Later, we will dive into how to add a custom field control by using a custom content type. For sanity sake, just know that field controls are derived from the site columns in content types.

Begin by navigating to the page layout gallery and making a new page layout. Call it whatever you like but make sure that you set the “Content Type Group” to “Page Layout Content Types” and then set the “Content Type Name” to “Article Page”. By setting the page layout to be based on this content type, you are exposing all the site columns that are available in that content type to the page layout for use.

Chapter 4 3: Page Layouts

 

 

 

 

After you have your new page layout created, open the page in Designer. A quick way to do this is to find the name of the newly created page layout in the gallery, clicking the dropdown arrow, and choosing “Edit in Microsoft SharePoint Designer”.

Chapter 4 3: Page Layouts

 

 

 

Click “OK” if a warning box appears, and then click “Yes” if you’re asked to edit in advanced mode. If you can’t access Designer through the browser, open Designer manually, click the “Page Layouts” folder in the navigation, click the title of the newly created page layout, and then click “Edit File”. Again, click “Yes” if asked to open it in advanced mode.

Chapter 4 3: Page Layouts

 

 

 

Once you have the page open for editing, add the content field that you set earlier by first clicking somewhere in the content area with the place holder id of “PlaceHolderMain” and hit enter to place a line break. In the “Insert” tab in the ribbon is a dropdown called “SharePoint”. Clicking this and then clicking the link at the bottom that says “Show Toolbox” will open up the toolbox on the right side of the screen. Scroll to the bottom of the toolbox and you will see a “SharePoint Controls” section with a subsection called “Content Fields”.

Chapter 4 3: Page Layouts

 

 

 

 

 

 

 

 

 

 

 

 

 

Find the field control called “Page Image”, click and drag it directly to the line break you just placed. When you do so, a line of code is automatically generated that looks like this:

Chapter 4 3: Page Layouts

 

 

Inside this <SharePointWebControls> tag is something called a “FieldName”. This is a unique identifier which is called a GUID. Since GUID’s are unique, yours will not match the one in the sample above. This number is what identifies that image field in the page layout.

After you’ve placed the “Page Image” content field, drag the “Image Caption” field from the tool box and drop it immediately after the line of code that was generated for the page image. This will place another web control called a RichHtmlField and it will have a different GUID than the previous one.

Chapter 4 3: Page Layouts

 

 

Save your file and return to your browser. Create a new page using this page layout by going to “Site Actions” ⇒ “View all Site Content” ⇒ “Create” ⇒ click the “Page” category on the left and select “Publishing Page” on the right and click “Create”. Doing so will open up a page creation settings box where you will type a title, description, URL, and also select the page layout that it will use. Select the page layout you created earlier and click “Create”.

Chapter 4 3: Page Layouts

 

 

 

 

 

 

Once your page is created, you will see the two fields you just placed in the page layout appear. From here, you can place your pictures, add your captions, and click “Save and Close”.

Chapter 4 3: Page Layouts

 

 

 

 

 

 

 

Now every time you use this page layout when creating a new page, it will have an area for a picture and an area for a picture caption. So while my page layout is called Zebra.aspx, I could use it on any new page and put any type of image in it and it will keep the same controls on the page. If you don’t use a control and it stays blank, after you save and close the file, the area will not appear. It still exists if you edit the page but you won’t see it unless it holds data.

Chapter 4-2: Page Layouts

Page Layout Code

Code of page layouts is best viewed and modified in SharePoint Designer. To find a page layout that is either OOTB or one that you created previously through the page layout gallery in the browser, simply click the “Page Layouts” section in the navigation bar in Designer. To open a file, click the title of the page layout and then click the “Edit file” link. Ensure that you enable “Advanced mode” if prompted to choose to do so after you edit the file. You also have the ability to create a new page layout using designer if you so choose.

When you look at the code of a page layout in SharePoint Designer, you will notice that the first series of statements are definition directives that define the programming language.

Chapter 4 2: Page Layouts

There is no mention of master pages in the page layout code since the master page is on the outside of the page layout elements in the rendering hierarchy. After the language definitions, SharePoint controls are registered. After registering these controls, there are areas wrapped in a <asp:Content> tag that are tied to the content placeholder in the master page. Notice that the ContentPlaceHolderID attribute will have a name that matches a content place holder in the master page. This is how the master page tells the page layout where to do its business.

Chapter 4 2: Page Layouts

 

 

The content controls that can be placed inside the content place holders can be text, HTML, CSS, ASP.NET controls, field controls, Web Parts, or Web Part zones. If you place anything else here, or if you place these things outside a content zone, SharePoint will throw an error.

 

Chapter 4-1: Page Layouts

Page layouts pair with master pages to give the user the templates that control the look and feel of a SharePoint site. While master pages provide controls such as navigation, company logos, and search functionality across the site, the page layouts contain other layout elements such as Web Parts and field controls. If you need a refresher on how the page layout fits into the page rendering hierarchy, refer to the overview in chapter 1.

As discussed previously, page layouts are stored in the same document library as master pages called the “Master Page and Page Layout Gallery”. As with most document libraries, the gallery supports versioning and workflows.

Inside the document library, page layouts use a content type to dictate what goes in each area in the page layout. Some page areas might be strictly for images while others offer more robust functionality such as rich html fields. Content types are discussed in more depth in a later section of this chapter.

Out-of-the-Box Page Layouts

There are a number of preconfigured, OOTB page layouts that come standard with SharePoint. To view all the available page layouts, go to root ⇒ “Site Actions” ⇒ “Site Settings” ⇒ “Page layouts and site templates” under the “Look and Feel” section of the settings page. On this page, you can determine which site templates and page layouts are available to use, determine if a page inherits its page layout from its parent or not, and you can also set the default page layout. If you create any custom page layouts, you can enable them to be visible in the page layouts dropdown in the ribbon by selecting it and clicking the “Add” button if it doesn’t appear by default.

Note: This screen does not change a page’s page layout. It does dictate which page layouts are available for use though and can also establish which page layouts are applied to created pages by default.

Chapter 4 1: Page Layouts

 

 

 

 

 

Depending on the type site template you are using, certain page layouts may or may not be available by default. For a detailed description of site templates, please refer to the SharePoint Server 2010: Power User Guide, which is another manual in the ICC Training Series.

Working with Page Layouts

Changing a Page’s Page Layout

Changing a page’s page layout is easy. To do so, simply navigate to the page you want to change and click “Edit”. Then click “Page Layout” in the “Page” tab in the ribbon to see a dropdown menu of the available page layouts. Simply select a page layout and it will be applied to the page.

Page Layout Gallery

The page layout gallery is another area that you can use to view all the available page layouts as well as perform other functions, such as starting a new page layout. Since page layouts are very similar to master pages, they are paired with them in the “Master page and page layouts” gallery. To refresh how to get to this area, go to “Site Actions” ⇒ “Site Settings” ⇒ and click the “Master pages and page layouts” link in the “Galleries” section. This page will display in tabular form all the available master pages and page layouts available for use in your site.

Creating a New Page Layout through the Gallery

To create a new page layout, in the page layout gallery, select the “Documents” tab in the ribbon. Here you will see an option called “New Document” that has a dropdown arrow. Click the dropdown arrow and select the “Page Layout” option. This brings up the page layout menu where you can associate content types, give the page layout a title, description, and URL, as well as set up variation labels. We will discuss content types shortly, but just note that working with content types is best to be done through the browser and not through designer. It will be explained why this is later but since it is best to work with content types in the browser, it is often easier to create the page layout through the browser as well and then do the code modifications in Designer later. For now, select your settings and click “OK”.

Chapter 4 1: Page Layouts

 

 

 

 

 

 

 

 

 

 

 

Downloading and Uploading Page Layouts

If you want to pull a page layout down locally and edit it using a text-editor, you can select it in the gallery and click the “Documents” tab in the ribbon ⇒ “Download a Copy”. To upload a modified page layout, choose the “Upload Document” button in the same ribbon tab. While it is possible to use the upload/download options, it is far easier to just edit the document in Designer using the built-in connectivity to save and preview changes.

Chapter 3-5: Master Pages

Content Place Holders

Navigating through the code, there is one or more areas called content place holders. The code for content placeholders looks like this:

<asp:ContentPlaceHolder id=”Main” runat=”server”>

</asp:ContentPlaceHolder>

Content placeholders are, in essence, containers that signify where to render sections of content. They get their content from the content controls that are placed in page layouts and content pages. The most used place holder is the PlaceHolderMain which exists on every page layout. These are different from the required placeholder because these areas are modifiable and you can elect to add as many as your business needs dictate. These place holders render page layout or content items such as Web Part zones, field controls, or any other content items or areas. The “ID” attribute is used to determine the identity of a given place holder. Going back to the sample code from above, the id=”main” is what is referenced in the master page using the matching ID attribute to generate the content. On the page layout or content page, the source would look like this:

<asp:Content ID=”mainArea” ContentPlaceHolderID=”main” Runat=”Server”>

Here’s where you put the content or controls.

</asp:Content>

Content place holders are the glue that holds together the page rendering hierarchy discussed in the first chapter. They are how the link is made and how the master page tells the page layout and content where to display.

Working with Master page’s Navigation Code

Navigation has its own dedicated chapter later in this guide that discusses the ins and outs of the various navigation elements. However, this section will discuss the code that goes into controlling the top navigation bar that is placed in a master page. As an example, we will look at the top navigation code that is included in v4.master. OOTB, it looks like this:

<SharePoint:AspMenu ID=”TopNavigationMenuV4” Runat=”server” EnableViewState=”false” DataSourceID=”topSiteMap”

AccesKey=”<%$Resources:wss,navigation_accesskey%>” UseSimpleRendering=”true” UseSeperateCss=”false”

Orientation=”Horizontal” StaticDisplayLevels=”2” MaximumDynamicDisplayLevels=”1” CssClass=”s4-tn”/>

This AspMenu control has various attributes that describe various aspects of the top navigation. The “DataSourceID” is what dictates which site map provider is used based on what is declared earlier in the master page site map provider declaration. The “Orientation” can be either vertical or horizontal. The “CssClass” declares which wrapper CSS class surrounds the top navigation for styling purposes. The “MaximumDynamicDisplayLevels” tells how many layers deep of nesting will be applied to the drop down or fly out effects in the navigation. The “StaticDisplayLevels” number tells how many layers will be displayed by default as static links. The other attributes can be researched further on their functionality using a common search engine. One final attribute to note is the “UseSimpleRendering” option which, when set to “true”, enables the use of simplified HTML rendering which has much cleaner, smaller code that is easier to style.

Chapter 3-4: Master Pages

Reviewing the Code

We will begin our review of master page code by opening v4.master in Designer. The code begins with declarations that state the language the code is written, which server controls and assemblies are registered, as well as declarations for JavaScript and CSS.

Chapter 3 4: Master Pages

 

 

 

After the standard declarations in red, the next important pieces of the master page code are the required placeholders. We will discuss what content placeholder’s do shortly but the next page contains a list of the required placeholders needed in master pages and what they do.

Chapter 3 4: Master Pages

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
*Table data was pulled from Microsoft’s MSDN page at: http://msdn.microsoft.com/en-us/library/ee539981.aspx

These lines of code are required when making a new master page. In some instances, if these lines aren’t present, the site will break. If you have an area that you will not need that is in the list of required code, instead of deleting the code, you could simply hide it by setting the visible attribute to false. It would look like this:

Chapter 3 4: Master Pages

 

 

If there is a need to produce a new master page from scratch, there are “Starter” master pages available free for download from the community at http://startermasterpages.codeplex.com. It’s recommended that if you do want to make a new master page based on an OOTB master page, copy the master page and rename the file instead of modifying an existing OOTB master page. Changing any OOTB features will render your product unsupportable from Microsoft and may lead to complications when they role out service packs down the road.

Note: If you have an error such as a missing placeholder in a custom master page, SharePoint will automatically revert your master page to a functional OOTB master page by default. So if when modifying a master page’s code you notice that the master page gets changed in the browser when you refresh it, chances are that you have an error in your master page code that is causing SharePoint to safe guard the page by defaulting to v4.master.

The Ribbon

The ribbon is declared in a master page over the course of 300 lines of code or so. While at first glance this may seem intimidating, in actuality it is quite easy to copy a ribbon out of the v4.master and place it into a custom master page. To pinpoint the start and end of the code, copy everything from the <div> with an ID of “s4-ribbonrow” down to just before the <div> with an ID of “s4-workspace” in the v4.master code into your new master page. For more information on different things to do with the ribbon, such as hiding it from anonymous users or how to turn off automatic ribbon positioning, refer to the chapter on ribbons later in this guide.

Favicons

Favicons are the small custom images that appear in bookmark menus and next to the URL in the browser. It is possible to declare these images in the master pages through a small line of code. It looks like this:

<SharePoint:SPShortcutIcon runat=”server” IconUrl=”type/Your/Path/Here.ico” />