in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Ton Stegeman [MVP] weblog

I have moved my blog to http://www.tonstegeman.com/blog. If you have a blogpost at sharepointblogs that does not display all the content on the right hand side, please go to the new blog. It has all the posts that are on this blog as well. I you have any feedback, please send me a message through the contact form.

Getting the associated page layouts from a SharePoint document library

In MOSS 2007 you can associate a page layout with a content type. The MOSS publishing feature uses this mechanism to select the content type for the new page that you create by using the “Create Page” option from the Site Actions menu. By selecting a page layout, you directly select the content type for your content page.

In code I needed to find out what Page Layouts are associated with the content types that are associated with a document library. In this example I created a very simple WinForms application that lists the available page layouts for the content types associate with the “Pages” library in the “News” site of a publishing portal.

This document library has these content types associated:

    Pagelayouts1

By navigating to the Master Page gallery, we can find out what page layouts are associated to the “Article Page” content type:

      Pagelayouts2

In this case we have 5 page layouts associated to our content type. This is exactly what we want to achieve, but now in code. The first thing to do is get the SPList object for the document library:

    SPSite site = new SPSite(textBoxWeb.Text);
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists[textBoxLibrary.Text];

The namespace Microsoft.SharePoint.Publishing contains a class called “PublishingWeb”. By using this line of code we can get a reference to the publishing web:

    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);

Now we can iterate through the associated content types and find the page layouts associated to the content type using the GetAvailablePageLayouts method. The thing to remember here first is that  the content type associated to the document library is not the content type that we associated to the page layout. When a content type is associated to a list, SharePoint automatically generates a new content type that inherits from the content type we choose and associates that to the list. So instead of trying to find what page layouts using the content type, we need to use the parent of our content type. The output log of our code below demonstrates this. First the code:

    foreach (SPContentType contentType in list.ContentTypes)
    {
        // Log details of content type
        textBoxResult.Text += 
            string.Format("Content type: {0}\r\n", contentType.Name);
        textBoxResult.Text += 
            string.Format("    Id=: {0}\r\n", contentType.Id);
 
        // Try to find associated page layouts and log number found
        PageLayout[] layouts = 
            publishingWeb.GetAvailablePageLayouts(contentType.Id);
        textBoxResult.Text += 
            string.Format("    Associated Page layouts: {0}\r\n", layouts.Length.ToString());
 
        // Log details of content type parent
        textBoxResult.Text += 
            string.Format("    Parent=: {0}\r\n", contentType.Parent.Name);
        textBoxResult.Text += 
            string.Format("    ParentId=: {0}\r\n", contentType.Parent.Id);
 
        // Try to find associated page layouts for parent and log number found
        layouts = publishingWeb.GetAvailablePageLayouts(contentType.Parent.Id);
        textBoxResult.Text += 
            string.Format("    Associated Page layouts: {0}\r\n", layouts.Length.ToString());
 
        // Log the page layouts
        foreach (PageLayout layout in layouts)
            textBoxResult.Text += string.Format("        Page layout: {0}\r\n", layout.Name);
    }

This results in this output for the Article Page content type:

Content type: Article Page
    Id=: 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D0075C2CA1B0566054FAB8DFC025454149B
    Associated Page layouts: 0
    Parent=: Article Page
    ParentId=: 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D
    Associated Page layouts: 5
        Page layout: PageFromDocLayout.aspx
        Page layout: ArticleLeft_copy(1).aspx
        Page layout: ArticleLeft.aspx
        Page layout: ArticleRight.aspx
        Page layout: ArticleLinks.aspx

By looking at the Id you can see that a new content type called Article Page is attached to the Pages library and we can find the page layouts by using the Parent. 

Comments

No Comments

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts