locked
Create a page layout template page and use this template page to create pages in a wiki library RRS feed

  • Question

  • Hi,

    I created a custom content type with parent type as publishing content types and created a layout page based on this content type. Now i need to add pages to the wiki library which should be same as the template page with a user specified name.

    Thanks

    Wednesday, April 4, 2012 7:06 AM

Answers

  • If you are creating your application using Visual Studio then first create content type for the publishing page. Now, create new sharepoint project and make sure it is sandbox solution. Now add module to this project and change .txt file to .aspx page. In this .aspx page add following code

    <%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"> //Add your content type fields here <div id="MainContentDiv" style="background-color:white; width:800px; margin-left:20px; margin-top:20px"> <div style="font-family:'Times New Roman', Times, serif; font-size:24px; font-style:normal; font-weight:bold"> <SharePointWebControls:TextField ID="TextField1" FieldName="ReportT" runat="server"/></div> <div id="UMDNSDiv"> <div style="font-family:'Times New Roman', Times, serif; font-size:14px; font-style:normal; margin-top:25px; text-align:justify; display:block; ">UMDNS Information:</div> <div style="font-family:'Times New Roman', Times, serif; font-size:14px; font-style:normal; text-align:justify; display:block; "> <PublishingWebControls:RichHtmlField ID="RichHtmlField1" FieldName="UMDNSI" runat="server" AllowImages="true" /></div> </asp:Content> <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server"> My Page </asp:Content> <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" > My Page </asp:Content>

    Now change Elements.xml file as shown below:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="YourModuleName" Url="_catalogs/masterpage">
        <File Path="YourModuleName\abc.aspx" Url="abc.aspx" Type ="Ghostable" >
          <Property Name="PublishingAssociatedContentType" Value=";#NameOfContentType;#YourContentTypeID;#" />
          <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
          <Property Name="Title" Value="Publish Page Layout Template" />
        </File>
    </Module>
    </Elements>

    Now deploy this project. It will add page layout to 'Master pages and page Layouts' in your site.

    Next step is use your template to create page in your doc library. You can do this by following code

    using (SPWeb web = workflowProperties.Web)
                    {
                        using (SPSite site = web.Site)
                        {
    				PublishingPage newPage;
    SPList list = web.Lists["Wiki Pages"];
                            	String url = list.RootFolder.ServerRelativeUrl.ToString();
                            	PublishingSite pubSite = new PublishingSite(site);
    				//store your Page Layout Name
                            	string pageLayoutName = "abc.aspx";
    string destPage = “destination.aspx”; //Page name which you want to create from the template
    string layoutURL = site.ServerRelativeUrl + "/_catalogs/masterpage/" + pageLayoutName;
                            	PageLayout layout = pubSite.PageLayouts[layoutURL];
    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web)
    newPage = publishingWeb.GetPublishingPages().Add(destPage, layout);
    newPage.Title = “xyz”;
    newPage.ListItem["Content Type field"] = “Sample Text”;
    newPage.Update();
    			}
    		}
    

    Hope it can help you.


    AshitaP

    • Proposed as answer by AshitaP Wednesday, April 4, 2012 7:04 PM
    • Marked as answer by Qiao Wei Friday, April 13, 2012 11:55 AM
    Wednesday, April 4, 2012 7:03 PM
  • You can create a custom .aspx page and upload it under "Master pages and page layouts".

    Eventually you'll have to add your custom page under "Page layouts and site templates" if you checked "Pages in this site can only us the following layouts", if you checked "Pages in this site can use any layout" you don't have to do something.

    Now, when you create a new page and you edit this page, you can choose under Page > Page Layout your custom layout

    • Proposed as answer by endeka Wednesday, April 4, 2012 12:40 PM
    • Marked as answer by Qiao Wei Friday, April 13, 2012 11:55 AM
    Wednesday, April 4, 2012 12:40 PM

All replies

  • you can create pages, download this pages add it to site definition. Provision page layouts files

    http://platinumdogs.wordpress.com/2010/02/25/sharepoint-create-a-publishing-page-in-a-site-definition-using-a-custom-page-layout/

    Regards,
    Milan Chauhan

    Wednesday, April 4, 2012 8:45 AM
  • Milan,

    Thanks for your answer.

    But my requirement is to create the layout page in a wiki library.

    Wednesday, April 4, 2012 10:57 AM
  • You can create a custom .aspx page and upload it under "Master pages and page layouts".

    Eventually you'll have to add your custom page under "Page layouts and site templates" if you checked "Pages in this site can only us the following layouts", if you checked "Pages in this site can use any layout" you don't have to do something.

    Now, when you create a new page and you edit this page, you can choose under Page > Page Layout your custom layout

    • Proposed as answer by endeka Wednesday, April 4, 2012 12:40 PM
    • Marked as answer by Qiao Wei Friday, April 13, 2012 11:55 AM
    Wednesday, April 4, 2012 12:40 PM
  • If you are creating your application using Visual Studio then first create content type for the publishing page. Now, create new sharepoint project and make sure it is sandbox solution. Now add module to this project and change .txt file to .aspx page. In this .aspx page add following code

    <%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"> //Add your content type fields here <div id="MainContentDiv" style="background-color:white; width:800px; margin-left:20px; margin-top:20px"> <div style="font-family:'Times New Roman', Times, serif; font-size:24px; font-style:normal; font-weight:bold"> <SharePointWebControls:TextField ID="TextField1" FieldName="ReportT" runat="server"/></div> <div id="UMDNSDiv"> <div style="font-family:'Times New Roman', Times, serif; font-size:14px; font-style:normal; margin-top:25px; text-align:justify; display:block; ">UMDNS Information:</div> <div style="font-family:'Times New Roman', Times, serif; font-size:14px; font-style:normal; text-align:justify; display:block; "> <PublishingWebControls:RichHtmlField ID="RichHtmlField1" FieldName="UMDNSI" runat="server" AllowImages="true" /></div> </asp:Content> <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server"> My Page </asp:Content> <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" > My Page </asp:Content>

    Now change Elements.xml file as shown below:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="YourModuleName" Url="_catalogs/masterpage">
        <File Path="YourModuleName\abc.aspx" Url="abc.aspx" Type ="Ghostable" >
          <Property Name="PublishingAssociatedContentType" Value=";#NameOfContentType;#YourContentTypeID;#" />
          <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
          <Property Name="Title" Value="Publish Page Layout Template" />
        </File>
    </Module>
    </Elements>

    Now deploy this project. It will add page layout to 'Master pages and page Layouts' in your site.

    Next step is use your template to create page in your doc library. You can do this by following code

    using (SPWeb web = workflowProperties.Web)
                    {
                        using (SPSite site = web.Site)
                        {
    				PublishingPage newPage;
    SPList list = web.Lists["Wiki Pages"];
                            	String url = list.RootFolder.ServerRelativeUrl.ToString();
                            	PublishingSite pubSite = new PublishingSite(site);
    				//store your Page Layout Name
                            	string pageLayoutName = "abc.aspx";
    string destPage = “destination.aspx”; //Page name which you want to create from the template
    string layoutURL = site.ServerRelativeUrl + "/_catalogs/masterpage/" + pageLayoutName;
                            	PageLayout layout = pubSite.PageLayouts[layoutURL];
    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web)
    newPage = publishingWeb.GetPublishingPages().Add(destPage, layout);
    newPage.Title = “xyz”;
    newPage.ListItem["Content Type field"] = “Sample Text”;
    newPage.Update();
    			}
    		}
    

    Hope it can help you.


    AshitaP

    • Proposed as answer by AshitaP Wednesday, April 4, 2012 7:04 PM
    • Marked as answer by Qiao Wei Friday, April 13, 2012 11:55 AM
    Wednesday, April 4, 2012 7:03 PM