Showing posts with label ContentTypes. Show all posts
Showing posts with label ContentTypes. Show all posts

Jan 19, 2012

Creating a Content Type for Custom PageLayouts

Even when you don't use any custom fields on your PageLayouts, and necessary if you do, it is worth its while to create a custom content type (or several, if you need to apply different fields to different PageLayouts) for your PageLayouts for several reasons:

  • custom content type enables grouping the PageLayouts in meaningful groups instead of the Custom group in the PageLayout menu
  • also, for content rollup with CQWP, with custom content type, it is possible to list only the pages that are of the specific content type
  • when using custom fields as well, it is possible to filter the content rollup by the custom field info

Creating the basic Content Type is a quick task. Add a new item to your SharePoint project, of the type Content Type:


Select then "Page Layout" as the base Content Type to inherit from, or one of your existing Content Types:


Edit the Elements.xml; change the Name, Group and Description to your liking.


If you want to create some custom fields for your content types, add the fields to the Elements.xml as Field-elements:


Generate GUIDs for the IDs of the fields.

Then add the field references to the Content Type FieldRefs. After that the Elements.xml should look something like this:


Now, to use this with your PageLayouts, first, set the Content Type of the PageLayout to your custom Content Type in the Elements.xml containing the PageLayout elements by setting the Content Type value and adding a PublishingAssociatedContentType-property with the ContentType ID to the file(s):


If you want to make it easier for the users to fill in the fields, add them to the PageLayout EditPanel:


FieldName gets the GUID ID you generated for the field. 

And maybe some information needs to be visible too, add those fields to the page html as well:


In edit mode, the page looks now like this: 


The Content owner is visible on page (and a bit stylized here):


(Page content from http://slipsum.com, my favorite Lorem Ipsum.)

Aug 26, 2009

Default Content Types - do not touch!

Content Types are a very powerful means of distributing eg. document templates through the whole Site Collection. And if used in a wrong though possible way, they may turn out to be overpowering even.

There was this issue of distributing the company word template to each and every document library in a site collection with already dozens of sites and libraries in it. A custom content type requires a lot of hand work before it is in use in each of the libraries. But since there is a default document content type, why not change the template it uses?

The default Document Content Type is in fact a sort of pseudo top class that doesn't really contain information about templates. The actual templates are defined in site and list definitions, in site definition and site collection onet.xml files. The blank default document templates referred to in the onet.xml file are in the SharePoint HIVE. Theoretically you could replace these files with you're own, but that's a no-no too.

If you happen to try out this in itself easy looking and appealing scenario of modifying the default document type, you will notice that it gives a log of warnings after making the change and it makes you believe that no change really took place, but it actually does seem to work when you go to a document library and click New to create a new deafult type document. The document uses the template that was set in the document type settings.

So what's all this fuss about then? The next time you create a new document library anywhere in the site collection, you will find that you have lost the possibility to change the default document type of the library. There is no more a drop-down with word, excel powerpoint, basic page etc. choice to make. The default document type template overrides this very functionality of the document libraries. It does not affect the use of custom content types, though.

Fortunately this doesn't create a catastrophe and is easy to change back - the same way the situation was first created. You only need to go to the Advanced settings of the Document Content Type and clear out the text box showing the template file information, and save the settings. It gives the same error log, but saves the day.

Thus the conclusions are:
1) do not modify default content types, stick to making your own ones
2) if you want a modified library with modified document content types, either make the modifications to one library and save it as template, or - even better - make your own list definition and/or site definition solution package