http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2011/11/23/sharepoint-2010-cookbook-how-to-create-custom-page-layout-for-publishing-site-using-visual-studio-2010.aspx
SharePoint – Creating Page Layouts for Custom Content Types Posted by: Sumit Maitra , on 1/18/2011, in Category SharePoint
Views: 74098
Abstract: In my previous article Custom Content Types in SharePoint, I demonstrated how we can create custom content types in SharePoint. However we left one piece open where we couldn’t see the data that was entered in the list through the default content type. We had to go to List->Edit Properties to edit/update or view the values. In this article we’ll see how we can define custom page layouts for Custom Content Types in SharePoint
In my previous article Custom Content Types in SharePoint, I demonstrated how we can create custom content types in SharePoint. However we left one piece open where we couldn’t see the data that was entered in the list through the default content type. We had to go to List->Edit Properties to edit/update or view the values. In this article we’ll see how we can define custom page layouts for Custom Content Types in SharePoint. We will quickly recap the creation of a new Content Type. This time we’ll use a Publishing Content Type as the parent type and the site will be using a ‘Publishing’ site template.
Creating a Custom Publishing Content Type
1. Follow steps 1-4
2. In Step 5 where we’ve to make sure of the following under “Parent Content Type:”
a. “Select Parent content type from:” is set to ‘Publishing Content Type’
b. “Parent Content Type:” is set to ‘Page’
These two settings are important else you won’t be able to use the content type in a custom the page layout (created through SharePoint Designer).
Click OK and add the required columns to the content type as defined in Step 6 in my previous article.
You will notice a few extra columns in the default content type this time, because our content type is inheriting from “Page” Content Type. You can keep them because they are optional. Best part about the custom page layout is you can choose to show/hide the fields you want for the document.
Once we are done adding columns for our ‘Functional Specification’ the list of columns should look something like the list below.
Note: It’s important for the parent site to use a Publishing Site template. Else custom page layouts cannot be created through designer.
Creating Custom Page Layout
To create a custom page layout we’ll use SharePoint Designer. It’s a freely downloadable tool from Microsoft. You can download the 32 bit version or the 64 bit version
Step 1: Start SharePoint Designer.
Step 2: Select ‘Sites’ and click on ‘Open Site’.
Step 3: Give the site name and click Open. You’ll be prompted for credentials. Provide credentials of a site collection admin user.
Step 4: Under ‘Site Objects’ in the left navigation pane, select ‘Page Layouts’
Step 5: Click on ‘New Page Layout’ button in the ‘Page Layouts’ ribbon. You will get a dialog asking you to select the content type. Here we will select the custom content type we created. Provide a filename in the URL Name field and a page title in the Title field. Click OK to continue.
Step 6: In a few seconds, the skeleton layout page (aspx) will be created and opened in SPD. It’s a standard ASPX page. If you are not too much into web development, just consider it as a well formed xml. All you care about is putting your content in <asp:Content …></asp:Content> tags.
The thing of most interest for us is the Toolbox on the right. Expand the SharePoint Controls pane and you’ll see ‘Page Fields’ and ‘Content Fields’ section. Here you’ll see the Content Type columns we had defined have been all pulled in.
Step 7: Drag and Drop all the fields you need on the ASPX page inside the <asp:Content></asp:Content> tags. Unlike me if you are an HTML/CSS guru, here is where you get creative. In the end, the page should have all the content type fields you need and at a very minimum it will be good to include all the mandatory ones. My best effort looks as follows:
Note: Observe when you drop the fields you have GUID only for the FieldName, so you better label them either before or immediately after you’ve dropped the field, else you’ll forget which field you dropped.
Step 8: Save the page (trusty old Ctrl+S) and switch to the ‘Page Layouts’ tab. Here you’ll see the ‘FunctionalSpectLayout.aspx’ has a checkmark against its name.
Step 9: Right click on ‘FunctionalSpecLayout.aspx’ and select ‘Check In’. You will be prompted with a dialog as follows:
Make sure you select ‘Publish a major version’ because this is our first publish. Click OK.
Step 10: Clicking on OK will give you the following prompt
Click on ‘Yes’ and wait for SPD to start your default browser. If asked for credentials provide credentials for a Site Collection Admin. A page similar to the following will open up.
Step 10: Pull down the menu for the ‘FunctionalSpecLayout.aspx’ by click on the down arrow. Select ‘Approve/Reject’ from the menu.
Step 11: It will pop-up a window with the following options. Select Approved, provide comments and hit OK.
Our Custom layout is all but done. Now, let us test it out.
Step 12: Select ‘Site Actions’ > ’Site Settings’. Under ‘Site Administration’ select ‘Site Libraries and Lists’. Click on ‘Customize “Pages”’ link. It will take us to the Page Library settings (shown below) where we’ll add our content type as one of the supported content types.
Step 13: Click on ‘Add from existing site content types’ to add our custom content type.
Step 14: In the Above screen select our custom content type ‘Functional Specifications Content Type’ and ‘Add’ it to the ‘Content Types to add’ list. Click OK.
Step 15: Click on ‘Change new button order and default content type’ to disable rest of the templates.
Click on the ‘Title, description and navigation’ select ‘Yes’ for visibility in quick launch.
That’s about it. We are all set to test our custom page layout for our very own content type.
Step 16: In the quick launch bar on the left, click on Pages to go the page library. You’ll see a page called default. Ignore this.
Click on Documents ribbon and Select ‘New Document’.
Step 17: You’ll be presented with Create new page screen. Fill in the ‘Title’ and ‘Description’. Change ‘URL Name’ if required.
Keep the Page Layout as functional specifications…
Click Create.
Step 19: You will navigate back to the Page library and our first functional spec will be ready for editing
Step 20: Click on the Name to see the custom page layout.
Ooohh so pretty!!!! (mimicking my 4 year old)
Now as you see SharePoint is saying the page is checked out and editable. Then why are all the controls read-only? One last step to go.
Step 21: Select the Page ribbon and click on Edit button.
OMG where did my pretty page go! Well, we forgot to tell SharePoint not to display labels for the fields in our page layout. But we will ignore that for a moment and wonder at SharePoint’s versatility.
See how the Contacts have picker controls; Dropdowns come in for Choice and Rich text editors (that can pretty much do all the formatting you can do in Word) for Rich HTML fields. All this and “look mama no code”!
Disabling Content Type Field Labels in Edit Mode
We saw in Step 21 above how in Edit mode our custom page had duplicate labels. One provided by us in the layout page and another that corresponded to the Field Name of the Content Type. Ideally we would want complete control on our layout so we would like to hide the Column Name that comes automatically. To do this we go back to SharePoint designer and do the following steps.
Step 1: Select FunctionalSpecLayout.aspx from the Page Layouts folder and click on Edit. Say ‘Yes’ for all popups.
Step 2: For each field that we dropped add an attribute DisableInputFieldLabel with value ‘true’.
Step 3: Once all fields have been updated with the new attribute, Save the file.
Step 4: Click on ‘Page Layouts’ in the left pane. On the right pane, select ‘FunctionalSpecLayout.aspx’.
Step 5: In check-in confirmation dialog, check it in as a minor version.
Step 6: Navigate back to the Page library and edit the page. Confirm that the duplicate labels are now gone.
Conclusion
In this article we saw how a publishing site can be used to create custom content type and then define a custom page layout for the content type. So far we’ve not written a single line of code for all this work.
Next step in our learning process is to use the SharePoint workflow to take our document through an approval process. Even more interesting is development of a SharePoint feature that will convert this editable page into a non-editable document of record.
VERSION 2
VERSION 2
SharePoint 2010 – Create Page Layout based on a custom Content Type in Visual Studio 2010
I will describe how I created a Page Layout based on a custom Content Type with SharePoint 2010 (beta2).
I will call my Content Type: PicturePageContentType
To do this:
- Go to: Site Settings –> Site content types
- Next, click Create
In the following screen you insert information about the content type.
An import thing to do is let it inherit from the right parent content type.
This needs to be from the group: Publishing Content Types and the parent needs to be Page.
So far nothing special, next I added some non existing columns.
On the following page click Add from new site column.
Define in the screen the follows up the columns that you would like to add to the your content type (Page Layout in the end).
After you added all the columns you needed you can overview it when you’re looking at you content type. You’ll see that I added Page Content, this was one from the already existing site columns.
Note: If you added a from column to your page you cannot delete it:) If you want to get rid of it you need to delete the whole content type.
In this part I’m using Visual Studio for this. It’s not the most common way to do it but I want to stay in control of my own files without uploading/approving etc. it in the web browser.
You can do it trough the browser by going to Master pages and page layouts in Site Settings.
Then add a new Page Layout based on the content type, but I’m doing it the hard way.
First of all if you going to do this in Visual Studio you’ll need this extension:
http://visualstudiogallery.msdn.microsoft.com/en-us/990bd0c8-a55b-4643-a35d-88136594c3d9
Now create a SharePoint Project in Visual Studio.
In the Server Explorer you’ll see your SharePoint Connections, then browse al the way to your Content Type you just created.
Right click on it –> Create Page Layout
Note: This option will only be available if you installed the extension. This option will be disabled if you didn’t selected Page as parent of your Content Type.
Keep this file open we’ll save it after we created a Module.
Right click on you SharePoint project –> Add –> New Item –> SharePoint 2010 –> Module
I called the Module: PageLayoutModule
Delete the Sample.txt from the module.
Now save the Page Layout file into the Module, this is a bit strange. To save it to the Module you just created navigate in your save window to your project directory on your local disk and then save it in the Module directory. Now it’s not visible yet in your solution so navigate to the directory again but this time in your Windows Explorer. Right click on your pagelayout.aspx file –> Copy. Finally right click on your Module in the Solution Explorer –> Paste.
Note: If someone knows a more handy way to do this please tell me.
First thing is to edit it like this (except the filename):
The following step is to add some properties to the File. These properties are:
The Property PublishingAssociatedContentType needs special attention.
The first one (bold) is the name of the Content Type in my case: PicturePageContentType
The second one (bold and underlined) is the Content Type ID.
To get the Content Type ID you can right click on the Content Type in the Server Explorer and then click Properties. In the properties window that popups you can find the the Content Type ID.
Now that we have our Content Type ID we can fill it in into the property value. Your Elements.xml should look something like this.
Content Type
First thing to do is to define a content type.I will call my Content Type: PicturePageContentType
To do this:
- Go to: Site Settings –> Site content types
- Next, click Create
In the following screen you insert information about the content type.
An import thing to do is let it inherit from the right parent content type.
This needs to be from the group: Publishing Content Types and the parent needs to be Page.
So far nothing special, next I added some non existing columns.
On the following page click Add from new site column.
Define in the screen the follows up the columns that you would like to add to the your content type (Page Layout in the end).
After you added all the columns you needed you can overview it when you’re looking at you content type. You’ll see that I added Page Content, this was one from the already existing site columns.
Note: If you added a from column to your page you cannot delete it:) If you want to get rid of it you need to delete the whole content type.
Create the Page Layout
In this part I’m using Visual Studio for this. It’s not the most common way to do it but I want to stay in control of my own files without uploading/approving etc. it in the web browser. You can do it trough the browser by going to Master pages and page layouts in Site Settings.
Then add a new Page Layout based on the content type, but I’m doing it the hard way.
First of all if you going to do this in Visual Studio you’ll need this extension:
http://visualstudiogallery.msdn.microsoft.com/en-us/990bd0c8-a55b-4643-a35d-88136594c3d9
Now create a SharePoint Project in Visual Studio.
In the Server Explorer you’ll see your SharePoint Connections, then browse al the way to your Content Type you just created.
Right click on it –> Create Page Layout
Note: This option will only be available if you installed the extension. This option will be disabled if you didn’t selected Page as parent of your Content Type.
Keep this file open we’ll save it after we created a Module.
Right click on you SharePoint project –> Add –> New Item –> SharePoint 2010 –> Module
I called the Module: PageLayoutModule
Delete the Sample.txt from the module.
Now save the Page Layout file into the Module, this is a bit strange. To save it to the Module you just created navigate in your save window to your project directory on your local disk and then save it in the Module directory. Now it’s not visible yet in your solution so navigate to the directory again but this time in your Windows Explorer. Right click on your pagelayout.aspx file –> Copy. Finally right click on your Module in the Solution Explorer –> Paste.
Note: If someone knows a more handy way to do this please tell me.
Elements.xml
To deploy everything right there need to be some information added to the Elements.xml.First thing is to edit it like this (except the filename):
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="PageLayoutModule" Url="_catalogs/masterpage"> <File Path="PageLayoutModule\Picturepagecontenttype.aspx" Url="Picturepagecontenttype.aspx" Type="GhostableInLibrary"> </File> </Module> </Elements> |
<Property Name="Title" Value="Page Layout with picture" /> <Property Name="MasterPageDescription" Value="Page Layout with picture" /> <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" /> <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/WelcomeSplash.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/WelcomeSplash.png" /> <Property Name="PublishingAssociatedContentType" Value=";#PicturePageContentType;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F200 7948130EC3DB064584E219954237AF3900A2F715DA6BDAD944B8D20AFA28B7980E;#" /> |
The first one (bold) is the name of the Content Type in my case: PicturePageContentType
The second one (bold and underlined) is the Content Type ID.
To get the Content Type ID you can right click on the Content Type in the Server Explorer and then click Properties. In the properties window that popups you can find the the Content Type ID.
Now that we have our Content Type ID we can fill it in into the property value. Your Elements.xml should look something like this.
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="PageLayoutModule" Url="_catalogs/masterpage"> <File Path="PageLayoutModule\Picturepagecontenttype.aspx" Url="Picturepagecontenttype.aspx" Type="GhostableInLibrary"> <Property Name="Title" Value="Page Layout with picture" /> <Property Name="MasterPageDescription" Value="Page Layout with picture" /> <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" /> <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/WelcomeSplash.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/WelcomeSplash.png" /> <Property Name="PublishingAssociatedContentType" Value=";#PicturePageContentType;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F200 7948130EC3DB064584E219954237AF3900C10F04A5A63B6E40B4347CDDAF7A0585;#" /> </File> </Module> </Elements> |
No comments:
Post a Comment