Wednesday, June 11, 2014

How to develop and Deploy Custom Page Layouts In SharePoint 2010 using Visual Studio 2010


How to develop and Deploy Custom Page Layouts In SharePoint 2010 
using Visual Studio 2010

Open Visual Studio 2010 as an Administrator -> Select an Empty SharePoint 2010 Project and give it a name such as “MyPageLayout” and then Click Next.

Give the Sit URL for deployment, and select the Solution Type to be Farm Solution as this is a Page Layout that we are trying to Deploy.



Right-click on the Project, and select Add new Item and then select “Content Type” template and give it a name such as “MyPageContentType” and click on OK, it will then ask for selecting the existing layout to create it from the base content Type. Here, select Page and then click on Finish. Elements.xml file in the MyPageContentType opens. You need to create few custom fields / content types to be used in our Custom Page Layout. For this, we need to implement the following code:

Initial Elements.xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39) -->
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900bdfc16ce990c42ce81e64f3ac64e23fb"
               Name="MyPageLayOut - ContentType1"
               Group="Custom Content Types"
               Description="My Content Type"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
    </FieldRefs>
  </ContentType>
</Elements>

You now need to add the following parameters and also tags so that the final Elements.xml will look like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <!-- new site column for custom page layout named Title -->
  <Field ID="{7024BCAE-DBB8-4E09-88E6-49D3B22C9111}" Name="PLTitle" DisplayName="Title" Group="PLContent"
         Type="Text" Required="FALSE" Sealed="FALSE" Hidden="FALSE" /> 
  <!-- new site column for custom page layout named Header -->
  <Field ID="{4837C657-45B0-45A2-A548-945DB9D9189F}" Name="PLHeader" DisplayName="Header" Group=" PLContent "
         Type="HTML" Required="FALSE" Sealed="FALSE" Hidden="FALSE" />
  <!-- new site column for custom page layout named Body -->
  <Field ID="{6C736E06-56C1-4AA1-AB0F-4712FAA2A8C4}" Name="PLBody" DisplayName="Body" Group=" PLContent "
         Type="HTML" Required="FALSE" Sealed="FALSE" Hidden="FALSE" /> 
  <!-- new site column for custom page layout named Footer -->
  <Field ID="{933EB014-B49D-4358-9043-1ECC565D283F}" Name="PLFooter" DisplayName="Footer" Group=" PLContent "
         Type="HTML" Required="FALSE" Sealed="FALSE" Hidden="FALSE" />
 
 
  <!-- Parent ContentType: Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39) -->
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900ec5a019792a349cfaa4c98762427d5ee"
               Name="MyPageLayOut - MyPageContentType" Group="Custom Content Types" Description="My Content Type"
               Inherits="TRUE" Version="0">
    <FieldRefs>
      <FieldRef ID="{7024BCAE-DBB8-4E09-88E6-49D3B22C9111}" Name="PLTitle" />
      <FieldRef ID="{4837C657-45B0-45A2-A548-945DB9D9189F}" Name="PLHeader" />
      <FieldRef ID="{6C736E06-56C1-4AA1-AB0F-4712FAA2A8C4}" Name="PLBody" />
      <FieldRef ID="{933EB014-B49D-4358-9043-1ECC565D283F}" Name="PLFooter" />
    </FieldRefs>
  </ContentType>
</Elements>

NOTE: To Generate IDs, we have to use Create Guid Tool from the Tools menu

Now that we are done with the Content Type that we have to use it in our Page Layout. Now that we will go ahead and create Page Layout. For this, we need to Right-click on Project and click on Module template, give it a name such as “MyModule” and click on OK. This will have 2 files, Elements.xml and sample.txt. Rename the sample.txt as “PublishingPageLayout.aspx”. Now remove the text in the PublishingPageLayout.aspx and add the following code or your own code based on how you want the pagelayout needs to look like.

<%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage" %>
<%@ Register Assembly="Microsoft.SharePoint" Namespace="Microsoft.SharePoint.WebControls" TagPrefix="SharePointWebControls" %>
<%@ Register Assembly="Microsoft.SharePoint.Publishing" Namespace="Microsoft.SharePoint.Publishing.WebControls" TagPrefix="PublishingWebControls" %>

<asp:Content runat="server" contentplaceholderid="PlaceHolderPageTitle">
    <SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
</asp:Content>

<asp:Content runat="server" contentplaceholderid="PlaceHolderPageTitleInTitleArea">
    <SharePointWebControls:TextField ID="TitleField" FieldName="Title" runat="server" />
</asp:Content>

<asp:Content runat="server" contentplaceholderid="PlaceHolderMain">
    <SharePointWebControls:TextField ID="TextField1" FieldName="PLTitle" runat="server"/>
    <br />
    <PublishingWebControls:RichHtmlField ID="RichHtmlField1" FieldName="PLHeader" runat="server"/>
    <br />
    <PublishingWebControls:RichHtmlField ID="RichHtmlField2" FieldName="PLBody" runat="server"/>
    <br />
    <PublishingWebControls:RichHtmlField ID="RichHtmlField3" FieldName="PLFooter" runat="server"/>
</asp:Content>


And now that we have to update the Elements.xml which got created with the Module. Open Elements.xml file from Module, you need to make sure that the following properties are required as shown below:

<Module Name="MyModule" RootWebOnly="TRUE" Url="_catalogs/masterpage">
    <File Path="MyModule\PublishingPageLayout.aspx" Url="MyModule/PublishingPageLayout.aspx"  Type="GhostableInLibrary">
      <Property Name="PublishingAssociatedContentType" Value=";#MyPageContentType;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900ec5a019792a349cfaa4c98762427d5ee;#"/>
      <Property Name="ContentyType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
      <Property Name="Title" Value="My Publish Page Layout"/>
    </File>
  </Module>

Once this is done, make sure to got he Feature1.feature and move the contents Module and Content Type from Items in the Solution section to Items in the Feature Section.



That’s it. Now Build and Deploy the solution.

Monday, June 9, 2014

Master Page Deployment using Visual Studio as a Feature


DEVELOPING AND DEPLOYING MASTER PAGE

Open Visual Studio 2010 -> C# -> SharePoint 2010 -> SharePoint 2010 Empty Project -> <Project Name> eg., MSODBSolution -> Site URL -> Sandbox Solution

Now Right-click on Project <MSODBSolution> and select New Item -> Select Module from the existing SharePoint Site templates -> Give module a name eg., MSODBModule

You will see the Features folder is updated and You will also find 2 files in Module template added to Solution Explorer. They are: Elements.xml and Sample.txt.

o    Modify Sample.txt to a master page eg., MSODB.master and copy the code from v4.master and paste it in this MSODB.master file.

o    Update the Elements.xml file and make sure that the URLs, Paths and List property values are updated as follows:
                        <Module Name="MSODBModule" List ="116" RootWebOnly="TRUE" Url="_catalogs/Masterpage">
                                                <File Path="MSODBModule\MSODBMain.master" Url="MSODBModule/MSODBMain.master" />
                                </Module>

Now open Feature1 and make sure that the scope was set to web (will be based on your requirement) and also make sure that the contents from Items in the Solution are moved to Items in the Feature section


THATS IT.... BUILD (CTRL + SHIFT + B) & DEPLOY (CTRL + F5)


You can now see a New Solution got created under Solutions pages in your deployed website -> You can check this by going to Site Actions -> Site Settings -> Solutions (Under Gallery section)

For Applying this master page, you now need to go to Site Actions -> Site Settings -> Master Page (Under Look and Feel section) and then select the newly deployed custom Master page.

You can modify the code in the master page as required.

Followers