Second Life of a Hungarian SharePoint Geek

December 15, 2009

Inheritable properties of the publishing sites

Filed under: Publishing, SharePoint — Tags: , — Peter Holpar @ 17:29

You can customize the outlook of the site by setting the master page and / or the CSS file. If you worked with „standard” (collaboration) sites, you know that you can do that by setting the MasterUrl, CustomMasterUrl and AlternateCssUrl, all of them are String properties of the SPWeb class.

There are a few posts on the web about how to do that using these properties:

Programmatically Inherit Master Page and CSS in MOSS 2007

Setting SharePoint Web Master Pages Programmatically

I really don’t want to repeat their content, I suggest you instead to read them first, but the main point is that if you want to set the properties not only the actual web instance, but all of the subwebs either, it is a bit cumbersome, since you have to set the values one-by-one, iterating through the webs.

One thing I don’t find on the web that it’s a bit tricky to set the equivalent properties for publishing webs. If you convert your SPWeb instance to a PublishingWeb, then you get the tools that make it possible to push the settings down to all of the subwebs.

While the properties are defined as String for the SPWeb, the equivalent properties of the PublishingWeb are derived from InheritableStringProperty. I should note here, that although the getter methods of these properties utilize a more typical subclass of the InheritableStringProperty class (the internal MasterUrlProperty, CustomMasterUrlProperty, AlternateCssUrlProperty, also mentioned in the second post linked above), both the properties and the related private fields in the PublishingWeb are from type of the parent class InheritableStringProperty. I don’t really see why it is better than having these types as public, and using the direct type, for example MasterUrlProperty. Maybe to show a bit of simplicity to the user of the API.

InheritableStringProperty is derived from the InheritableProperty<String>  where the abstract class InheritableProperty<TValueType> has some really nice members for us.

In this post I will write about two of its methods, the SetValue and SetInherit methods.

Both of these methods have several (partly internal) overloads, I found that it is the most useful to work with the overloads that have two parameters, the second one is of Boolean type, when using from a console application and not from the web UI.

All of these methods have the return type of LongRunningOperationJob.

So what these methods are good for?

SetValue specifies the property value (for example, the master page URL) in the first method parameter, and is able to force the new value to all of the subwebs by setting the second parameter to true.

SetInherit specifies if the property value should be inherited from the parent web (it is set by the first method parameter), and is able to force the new value to all of the subwebs by setting the second parameter to true. Since the root web has no parent web, you will get an exception if you try to call SetInherit on the root web having the first parameter set to true.

Let’s see a practical example. The following code snippet will set a new value for the master page URL of a publishing site and force all of the subwebs to inherit the new value. It is much simpler than iterating through all of the webs under the current one.

  1. if (PublishingWeb.IsPublishingWeb(web))
  2. {
  3.     PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
  4.     InheritableStringProperty masterUrl = pubWeb.MasterUrl;
  5.     LongRunningOperationJob longRunningSetValue = masterUrl.SetValue("/_catalogs/masterpage/yourmasterpage.master", true);
  6. }

How do these methods relate to the user interface in the site settings?

You find a UI similar to this one when you set the master page of the site:

image

The following section describes the options on this page and the matching calls to do the same from code.

PublishingWeb.MasterUrl property

Inherit site master page from parent of this site – SetInherit(true, shouldInherit)

Specify a master page to be used by this site and all sites that inherit from it. – SetValue(newValue , shouldInherit)

Where newValue is the URL of the master page selected in the dropdown and shouldInherit is true if the following checkbox is selected:

Reset all subsites to inherit this Site Master Page setting

PublishingWeb.CustomMasterUrl property

Inherit system master page / Alternate CSS URL from parent of this site – SetInherit(true, shouldInherit)

Select a system master page for this site and its subsites to inherit. – SetValue(newValue , shouldInherit)

Where newValue is the URL of the master page selected in the dropdown and shouldInherit is true if the following checkbox is selected:

Reset all subsites to inherit this system master page setting

PublishingWeb.AlternateCssUrl property

Inherit Alternate CSS URL from parent of this site – SetInherit(true, shouldInherit)

Specify a CSS file to be used by this publishing site and all sites that inherit from it. – SetValue(newValue , shouldInherit)

Where newValue is the URL of the selected CSS file and shouldInherit is true if the following checkbox is selected:

Reset all subsites to inherit this alternate CSS URL

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: