Restricting page layout use in sub-sites

原文链接:http://pointstoshare.spaces.live.com/Blog/cns!AEC42F315B4528B0!2931.entry

 

This is one that I had no idea was even possible, but apparently you can restrict which page layouts a site in SharePoint can use, no matter what page layouts are available in the root sites gallery. The way this became apparent to me is that the OOTB search centre web site will only you to create pages using the 4 OOTB search related page layouts. If you create a new page layout that uses the same content type (Welcome Page for those who are interested) it still won't show up on the list of allowed page layouts when you create a page in search centre.

So to add a page layout to the allowed page layouts collection, use some code that goes like this (This is the code I used to add a page layout to the search centre site):

   1: using (SPSite site = new SPSite("http://[servername]/searchcenter")) 
   2: { 
   3:     using (SPWeb web = site.OpenWeb()) 
   4:     { 
   5:         PublishingSite psite = new PublishingSite(site); 
   6:         PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web); 
   7:         PageLayoutCollection allLayouts = psite.GetPageLayouts(false); 
   8:         PageLayout[] allowedLayouts = pweb.GetAvailablePageLayouts(); 
   9:  
  10:         PageLayout[] newPageLayotus = new PageLayout[allowedLayouts.Length + 1]; 
  11:         for (Int32 i = 0; i < allowedLayouts.Length; i++) 
  12:         { 
  13:             newPageLayotus[i] = allowedLayouts[i]; 
  14:         } 
  15:         newPageLayotus[newPageLayotus.Length - 1] = allLayouts["/_catalogs/masterpage/[page layout name].aspx"];
  16:         pweb.SetAvailablePageLayouts(newPageLayotus, false); 
  17:         pweb.Update(); 
  18:     } 
  19: } 

The code above uses the PublishingSite and PublishingWeb classes, so be sure to reference Microsoft.SharePoint.Publishing to your code. So basically what we are doing here is to get an array of the currently allowed page layouts in the search centre, as well as a collection of all the page layouts in the site collection. We then build a new array that has one additional item in it and for the extra item we specify a specific page layout from the all page layouts collection. We then set the new array to be the available page layouts for the search centre publishing web object, and update the web to commit the changes.

So this same principal could apply to any number of scenarios. You can tell if a PublishingWeb object is allowing all page layouts through the IsAllowingAllPageLayouts property. My idea here is that in a feature receiver you could control what page layouts are used in a site, then tie that feature to a site definition (either in a custom definition or through feature stapling if changing the OOTB site definitions is your thing). This way you can provide some more control over the look and feel of a publishing site. Very cool stuff in my opinion.

 

posted on 2008-11-05 23:55  王丹小筑  阅读(225)  评论(0)    收藏  举报

导航