another technical blog...technically

Tuesday, September 23, 2014

Restrict content types and related page layouts on list pages new button

This is one of the most recurrent tasks... dealing with pages list.
You created your pages list content types, associated page layouts to those content types, and customers want to use only those content type and page layouts or simply.
Ok, let's assume you have Example 04 deployed (if  it's your first time on this blog, download VLibs here), this is what you see in Pages list.

The key to accomplish this task is divide-et-impera, so what about the new page menu?
Work on this property
IList<SPContentType> contentTypeInstances2Show = ...
pagesList.RootFolder.UniqueContentTypeOrder = contentTypeInstances2Show;
pagesList.RootFolder.Update();
Assigning a  SPContentType list to UniqueContentTypeOrder, we give the order of the content type available for creating, and we can also exclude some of them from the menu.

The second step is to make a selection of page layouts available, the method is:
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web); 
PageLayout[] pageLayouts = publishingWeb.GetAvailablePageLayouts();

PageLayout[] pageLayouts2Show = ...
publishingWeb.SetAvailablePageLayouts(pageLayouts2Show .ToArray(), false);
So you have first to select content type related page layouts, just querying the publish website.
And this is what we will get
New document menu

Available page layouts

If you need the code, as usual this is the link. Go get it! and have a look at Example 06 ;) .
written in: Milano, Italia

2 comments:

Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?