Friday, April 30, 2010

Determine whether SPWeb inherits AlternateCssUrl from parent site or not programmatically

Sometimes we need to programmatically determine whether SPWeb has its own alternate css or inherits alternate css from its parent site. In UI we can find this option “Inherit Alternate CSS URL from parent of this site”  in Site Settings > Master page (it is available for publishing sites only):

image

If we will look at SPWeb class we will find that it has public property AlternateCssUrl of string type:

   1: public class SPWeb : IDisposable, ISecurableObject
   2: {
   3:     ...
   4:     public string AlternateCssUrl
   5:     {
   6:         get { ... }
   7:         set { ... }
   8:     }
   9: }

As “Master page” page is available only for publishing sites, my first idea was to check PublishingWeb class for boolean property like InheritAlternateCssUrl (similiar to InheritGlobalNavigation and InheritCurrentNavigation). But there is no such property in PublishingWeb. Instead, it also has AlternateCssUrl property as SPWeb. But instead of string type PublishingWeb . AlternateCssUrl property has InheritableStringProperty type:

   1: public sealed class PublishingWeb
   2: {
   3:     ...
   4:     public InheritableStringProperty AlternateCssUrl
   5:     {
   6:         get { ... }
   7:     }
   8: }

In turn InheritableStringProperty class has boolean IsInheriting property. So in order to determine whether web site inherits alternate css URL from parent site or not we should obtain PublishingWeb object and check its AlternateCssUrl.IsInheriting property:

   1: SPWeb web = ...;
   2:  
   3: var publishingWeb = PublishingWeb.GetPublishingWeb(web);
   4:  
   5: if (publishingWeb.AlternateCssUrl.IsInheriting)
   6: {
   7:     // alternate css is inherited
   8: }
   9: else
  10: {
  11:     // alternate css is not inherited
  12: }

Using this technique you will be able to determine alternate css inheritance programmatically.

No comments:

Post a Comment