2

In MOSS 2007 (the same in SharePoint 2010), I have created a custom content type "CustomPage" which inherits from "Page" in a new content type group "My Custom Columns".

Then I have added some custom site columns to CustomPage.

I have then created new content types "CustomArticle", "CustomNews", "CustomHome" which all inherit from CustomPage.

I then associate my page layouts with the relevant content types using the "Associated Content Type" field.

If I now create a new site and then a new page under that site e.g. create NewSite/Pages/NewPage.aspx with the "CustomArticle" content type, I would expect to be able to switch between my custom content types. However, if I go to the properties of the page and try to change the content type from the drop-down menu, I get the options of "Page", "Article Page", "Welcome Page" or "CustomArticle" (which is the one currently applied).

When I view the List settings for the "Pages" library underneath NewSite, I can see that my custom content types have NOT been inherited from the top level.

How do I ensure that all my custom content types are always available in libraries so that users can switch between page layouts with custom content types?

Thanks very much in advance for your help!

2 Answers 2

1

YOu can use a normal feature element with a ContentTypeBinding in it. It takes the list url and the content type id as attributes, the example below uses a resource for the pages url, so this will work for a site in any language, you can include as many binding as you want:

<ContentTypeBinding ContentTypeId="CTYPEID" ListUrl="$Resources:osrvcore,List_Pages_UrlName;" />

Furthermore, you bind a page layout to a specfic content type like so:

<File Url="AgoraPageContact.aspx" Type="GhostableInLibrary" >
  <Property Name="Title" Value="$Resources:YOURRESOURCEFILE,YOURTITLERESOURCEKEY;" />
  <Property Name="MasterPageDescription" Value="$Resources:YOURRESOURCEFILE,YOURDESCRIPTIONRESOURCEKEY;" />
  <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
  <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/WelcomeLinks.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/WelcomeLinks.png" />
  <Property Name="PublishingAssociatedContentType" Value=";#$Resources:YOURRESOURCEFILE,YOURCTYPENAME;;#CTYPEID;#" />
</File>

<!-- NOTICE THE DOUBLE ;; in the PublishingAssociatedContentType property! -->
0

You need to associate your custom Content Types with the Pages library.

Try something like this in a Feature Receiver:

private void ModifyPages(SPWeb web)
{
    SPList list = web.Lists["Pages"];
    SPContentTypeId[] ids = new SPContentTypeId[] { ContentTypeId.CustomArticle, ContentTypeId.CustomNews, ContentTypeId.CustomHome };
    foreach (SPContentTypeId id in ids)
    {
        SPContentType type = web.AvailableContentTypes[id];
        ReplaceAllContentTypesOnList(list, type);
    }
    list.Update();
}

// source: Microsoft.SharePoint.Publishing.Internal.ProvisioningHelper.ReplaceAllContentTypesOnList
private void ReplaceAllContentTypesOnList(SPList list, SPContentType contentType)
{
    List<SPContentType> types = new List<SPContentType>();
    foreach (SPContentType current in list.ContentTypes)
    {
        if (current.Id.Parent != contentType.Id)
        {
            types.Add(current);
        }
    }
    list.ContentTypes.Add(contentType);
    foreach (SPContentType type in types)
    {
        list.ContentTypes.Delete(type.Id);
    }
}
4
  • Thanks for your solution, so is there abosulutely no way of achieving this OOTB without writing any code?
    – user653408
    Commented Mar 10, 2011 at 14:09
  • 1
    It can be done manually. Go to Library Settings > Advanced Settings, click Yes for Allow management of content types, and click OK. Then in the Content Type section click Add from existing site content types in order to add your own Content Types. Commented Mar 10, 2011 at 14:46
  • The method @Rich states in his comment is the standard method of associating content types with a List. It's in the documentation.
    – James Love
    Commented Mar 10, 2011 at 15:46
  • 1
    StackOverflow is a programming help site. This question might be more appropriate at SuperUser.com or SharePointOverflow.com if you don't want an answer that involves code.
    – Tom Resing
    Commented Mar 11, 2011 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.