Tuesday, December 30, 2008

Enforcing Required Fields on SharePoint Server 2007 Publishing Pages via Content Types and Page Layouts

Applies to:
  • Office SharePoint Server 2007
Additional Tools Used:
  • Office SharePoint Designer 2007

Overview

Office SharePoint Server 2007 supports the creation of content types. Content types bundle metadata, security and retention policies, and workflow. An item saved in a SharePoint list – a document library, for example – can be assigned a predefined content type. This allows us to move beyond managing items by file format (.doc, .pdf, etc.) and focus on the intent of the content.
Let's say, for example, that we are creating an intranet site that will be the location to which company operating procedures are published as web pages. When someone writes a new policy, we want to be sure and collect some specific information like what the policy impacts, when it goes into effect, and who approved it. When we attach define metadata on libraries designed to store standard documents, client applications like Word and PowerPoint will help us out with the collection of the data. When we're trying to do this with a web page, we need to do a little more work.

Define a Content Type

The first thing we need is a content type with which to work. In this example, we'll create a content type defining a standard operating procedure for the fictitious Litware, Inc. We'll ignore workflow and other policies and focus on a few pieces of metadata.
To begin, we will add the site column definitions that will make up our content type. Adding a site column allows us to define a column once, and reuse it in any number of lists. Go to the Site Settings page of the top-level site collection.

Under Galleries, click on Site Columns. We'll add a new column by clicking Create.

Add a column titled "Procedure Name".

Define a new group named "Litware SOP" in which to organize the new columns.

Finally, mark the field as required. This is a critical step as we'll depend on SharePoint enforcing this setting.

Follow the same steps as above to create three more columns as detailed below:
Column Name Information Type Values Category Required
Procedure Category Choice Cashier Audit
Daily Opening
Daily Closing
Medical Emergency
Litware SOP Yes
Owning Department Choice Finance
Food and Beverage
Merchandising
Operations
Litware SOP Yes
Procedure Full HTML Content Litware SOP No
On each of the Choice columns, be sure to clear the default value. We don't want any accidental entries being made.
We now have four columns that can be used in our content type.

Return to the Site Settings page for the top-level site collection. Chose Site Content Types under Galleries.

Select Create. We're going to add a Litware SOP content type. Our intention is for these procedures to be web-based, so we'll inherit from the existing Page content type.

The list of columns you see in our new Content Type comes from the Page content type.

We now want to add the columns we previously defined for the Litware SOP. Click Add from Existing Site Column.

Select Litware SOP to narrow our choices and add the four columns.

You'll get a warning about the Procedure column, since this is an HTML field that most desktop applications won't be able to write to.

That's fine for our scenario. Hit OK and continue. When finished, we now have a defined Content Type. Now, we need a page layout to support editing.

Creating the Page Layout

Our users need access to our columns in order to fill them out. We need to create a page layout that includes fields that map to our columns. This is where we will make sure that required columns are completed before they are saved to a list.
Open SharePoint designer, and open the top-level site collection from File -> Open Site…
When the site is open, click File -> New -> SharePoint Content

In the New dialog box, choose SharePoint Publishing from the left column and Page Layout from the right. Choose Litware Content Types as the Content Type Group, and Litware SOP as the Content Type Name. Fill in the URL Name and Title as below:

Click OK.
We now have a blank page that we can use to layout our fields. Notice in the toolbox that the site columns we defined are available to place on the page.

To aid formatting, I'll insert a table with one column and three rows. Remember, this is an ASP.NET page, so anything we'd put in a normal web application can go here!

Now, drag the Procedure Name field onto the first row of the layout table, and Procedure Category onto the second row. In the third row, we're going to do something a little different.

EditModePanel

Let us assume, for our example, that the Owning Department field should not be viewed by just anybody. Maybe it is a value that is only important as a workflow variable, or simply used in managing the content in the library. It should only be visible to people who are editing the page. In order to do this, we'll place the Owning Department inside of an EditModePanel control.

This is a SharePoint Server control that does not render its contents unless the page is in edit mode. Add the EditModePanel to the third row of our table. You will want to adjust its width attribute to 100% so that it fills the row. You can now add the Owning Department field inside of the EditModePanel.
Now you can add the Procedure field. Make sure that it is placed outside of our little layout table.
This isn't a fancy layout, but it is enough for this example. Hit Save, and switch back over to your SharePoint site.

Adding the Content Type to a Site

Because we derived our Litware SOP content type from the existing Page content type, we constrained ourselves. We need to use our content type with a site template that has Web Content Management (WCM) turned on. WCM adds a Pages library to the site, and turns on a few extra features. If your site was created using one of the Publishing Site templates, then you're all set. I'm going to assume that we don't already have a publishing site available and need to create one. From the top-level site collection, choose Create Site from the Site Actions menu.

Name the site SOP, and choose the template, "Publishing Site with Workflow"

Once the site is created, click on "View All Site Content" in the Site Actions menu. Choose the Pages library, and click on Document Library Settings.

The Pages library already supports managing multiple content types, so we don't have to do any extra work. Other lists would have required us to go to the Advanced Settings page and turn on management of content types.
We're going to add our Litware SOP content type to the list of content types supported by the Pages library. Click on "Add from existing site content types".

Select Litware SOP from the list, click Add, and then OK. Back in the Pages library, click the drop-down menu arrow next to New, and select Litware SOP from the menu.

Now we'll create a page titled Open Doors. Be sure to choose "(Litware SOP) New Standard Operating Procedure" as the Page Layout.

Select your newly created page from the Library, and place the page in edit mode by selecting "Edit Page" from the Page Editing Toolbar.

What you should see now is a nice, blank page just like we had lain out in SharePoint Designer:

Remember, we marked the Procedure Name, Procedure Category, and Owning Department columns as required. To test that, go ahead and click the Check in to Share Draft button on the Page Editing Toolbar. You should see some lovely red messages like below:

We know that works… go ahead and fill in some values and check in the page. Notice that the value for Owning Department does not display. Remember that we put this inside an EditModePanel so only page editors would see it!

Conclusion

That's it! We used Site Columns in SharePoint to define required information. We added those columns to a new Content Type and built a Page Layout to support entering data into those columns. We also made some of that data visible only during page editing by using a SharePoint EditModePanel.


Print this post

No comments: