Monday, May 2, 2011

SharePoint:Create a New Page Layout in SharePoint 2007 - Feature-Based

So the requirement presented to me for a client was to take an existing page layout and change a few of the properties to allow it to use some custom styles implemented at the site collection level. To do this, I needed to create a new page layout based on an existing layout, make this part of a feature and pull in all the bits and pieces necessary to deploy this as a feature. Finally, I had to deploy it to the client's testing environment.
Here is how I did it...
First, I was instructed that the Search Main Page was to be used as the starting point. A developer on my team told me the best way to go would be to load up SharePoint Designer, navigate to the appropriate document library and export the file. This didn't work. My requirement was to have the search web part already on the page, the same as the existing search main page. SharePoint Designer killed this web part tag so it came up empty.
Getting a copy of the page layout
1. Better is to go to the site collection you're starting with
2. Select "Site Actions" -> "Site Settings" -> "Modify All Site Settings"
3. Under "Galleries", select "Master pages and page layouts"
4. Find the page layout from the list and open its context menu, select "Send to" -> "Download a Copy"
5. Save the file to the solution location. We won't be using Visual Studio because its not necessary for this evolution.
6. Once the file is saved, reopen the context menu for the page layout file and select "View Properties".
7. Click on the Preview Image and when it appears, right click and save the image to the same location as the aspx file.
Now you have the basic aspx files you will modify and put back into SharePoint as a feature. Open the aspx page in an appropriate editor. I'm assuming you understand enough about aspx to do what you need to do. Make the appropriate modifications and save the file.
IMPORTANT NOTE: If you are like me, you have a desire to 'clean up' the file declarations so you can see what's going on. Don't do this. I haven't figured out why but when I introduced hard returns in the second line (the really long one) of declarations between <% %> sections, it bombed when I reimported it into SharePoint. A guy on the team here thinks there may be a checksum on that line to head off tampering. Dunno but I know when I leave it it works, when I touch it it bombs.
You can also use any editor (Paint.Net works) to change the page layout to look the way you want it to. Change the folder of this file to your feature folder + "\Images". It's required for the page layouts and master pages.
So you should have two files and a subfolder for your feature ready to go.
Now we need to set up the feature to deploy it.
1.
Create two new files in any editor: "feature.xml" and "layoutfiles.xml"
2. Here are the contents of my feature.xml file:
 <Feature  Id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
     Title="Custom Formatted Search Page Layout by Jim"
     Description="Search page with customized style inclusions"
     Version="1.0.0.0"
     Scope="Site"
     Hidden="FALSE"
     xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
   <ElementManifest Location="layoutfiles.xml"/>
  </ElementManifests>
 </Feature>
2.1. Id is a new GUID you generate from Visual Studio.
2.2. Scope means the level where this will be used, not the level at which it becomes available for items below it. So "Site" scope means it will be installed at the site collection level and used at the site level.
3. Here are the contents of my layoutfiles.xml file:
 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="CustomSearchMain" Url="_catalogs/masterpage" Path="" RootWebOnly="TRUE">
   <File Url="CustomFormattedSearch.aspx" Type="GhostableInLibrary">
    <Property Name="Title" Value="$Resources:spscore,SearchMainTitle;" />
    <Property Name="MasterPageDescription" Value="$Resources:spscore,SearchMainDescription;" />
    <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
    <Property Name="PublishingAssociatedContentType" Value=";#$Resources:cmscore,contenttype_welcomepage_name#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D4;#" />
    <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/Preview Images/customsearchpage.gif,     ~SiteCollection/_catalogs/masterpage/Preview Images/customsearchpage.gif" />
    <AllUsersWebPart WebPartZoneID="TopZone" WebPartOrder="1">
     <![CDATA[
                     <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
                         <Assembly>Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
                         <TypeName>Microsoft.SharePoint.Portal.WebControls.SearchBoxEx</TypeName>
                         <Title>$Resources:spscore,SearchBoxWP_Title;</Title>
                         <Description>$Resources:spscore,SearchBoxWP_Desc;</Description>
                         <FrameType>None</FrameType>
                         <AllowMinimize>true</AllowMinimize>
                         <AllowRemove>true</AllowRemove>
                         <IsVisible>true</IsVisible>
                         <Width>420px</Width>
                         <GoImageUrl xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/gosearch.gif</GoImageUrl>
                         <GoImageUrlRTL  xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/goRTL.gif</GoImageUrlRTL>
                         <GoImageActiveUrl xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/gosearch.gif</GoImageActiveUrl>
                         <GoImageActiveUrlRTL  xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/goRTL.gif</GoImageActiveUrlRTL>
                         <TextBoxWidth xmlns="urn:schemas-microsoft-com:SearchBoxEx">280</TextBoxWidth>
                         <SearchResultPageURL xmlns="urn:schemas-microsoft-com:SearchBoxEx">results.aspx</SearchResultPageURL>
                         <AdvancedSearchPageURL xmlns="urn:schemas-microsoft-com:SearchBoxEx">~Site/$Resources:cmscore,List_Pages_UrlName;/advanced.aspx</AdvancedSearchPageURL>
                         <ShowAdvancedSearch xmlns="urn:schemas-microsoft-com:SearchBoxEx">true</ShowAdvancedSearch>
                         <DropDownMode xmlns="urn:schemas-microsoft-com:SearchBoxEx">HideScopeDD</DropDownMode>
                         <ShouldTakeFocusIfEmpty xmlns="urn:schemas-microsoft-com:SearchBoxEx">true</ShouldTakeFocusIfEmpty>
                     </WebPart>
                     ]]>
    </AllUsersWebPart>
   </File>
  </Module>
  <Module Name="LayoutPreviewImages" Url="_catalogs/masterpage/Preview Images" Path="Images" RootWebOnly="TRUE">
   <File Url="customsearchpage.gif" Type="GhostableInLibrary"/>  
  </Module>
 </Elements>
3.1. I took most of this from a coleague and from the net. The CData portion gives you the embedded web part already in the top web part zone.
3.2. Notice the gif image parts of the xml. These are whatever the new name is for the gif you edited earlier.
3.3. Notice also the part that says: "contenttype_welcomepage_name". This means that your layout will appear under the "Welcome Page" templates when you go to use it in your site collection. I haven't found much about this on the web yet but I believe if you search on content types and master page libraries, you may find additional options.
Your feature is complete!
Now its time to deploy.
1. Copy your aspx, gif (in its folder) and 2 xml files to the server where you want to deploy and into a new temporary folder.
2. Copy from the temp location to the 12 hive \Template\Features\ folder.
3. Open a command line and run: stsadm.exe -o installfeature -name MyNewPageLayoutFeature -force
3.1. Use the -force option if you've already installed previously.
3.2. the -name value is the name of the folder where the your feature now exists in the 12 hive.
4. Execute iisreset
5. Browse the the appropriate site collection on your server and back to "Site Actions" -> "Site Settings" -> "Modify All Site Settings"
6. Under "Site Collection Administration" select "Site Collection Features"
7. You should see your new feature/page layout. Click to activate.
Use your new layout.
Now, when you go to create a new page, you should see your additional layout. When you select it, you should get your custom layout gif to show users what it will look like.
I can imagine really intricate uses of this for creating custom CRM dashboards or for magically including SilverLight applications in your system. Good Luck!

No comments:

Post a Comment

SharePoint - Cannot convert a primitive value to the expected type 'Edm.Double'. See the inner exception for more details If y...

Ad