Thursday 9 January 2014

Provisioned Site Page

What am I talking about? well once in a while you may want to create a Site Page with a codebehind. Why? you may ask, well that's beyond the scope of this post (fancy way of saying I don't care or I don't know).

Anyway to get started create a SharePoint Project, I'm going to call mine spm.SitePageExample

next I'm going to specify my project to be deployed as a farm solution

Now the first thing I'm going to do before I forget is add a reference to The "Microsoft.SharePoint.Publishing "Extension and the "System.Web" Framework, to do this first right click on references in your solution explorer and click add reference...

With the reference Manager open, make sure under assemblies on the left you have Extensions selected, then check off "Microsoft.SharePoint.Publishing", and "System.Web" and finally click OK.

in your solution explored under References you should see "Microsoft.SharePoint.Publishing" added to the list.

with that out of the way, select your project and hit ctrl+shift+a to add a new item, in the Add New Item window make sure you have sharepoint 2010 items selected on the left and pick the module out of the options.

with that added you should have three files in your module:

  • Elements.xml
  • Sample.txt
  • SharePointProjectItem.spdata

SharePointProjectItem is a hidden file; generally you’ll never have to use it for anything other than troubleshooting. To view the spdata file you have to click show all files icon in your solution explorer.  

Open Up the Sample.txt file and paste the following:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI"
    Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" masterpagefile="~masterurl/default.master" CodeBehind="MySitePage.aspx.cs"
         Inherits="$SharePoint.Type.df45d565-73d2-447f-a463-5a362d2fc50d.AssemblyQualifiedName$" %>

<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
       <SharePoint:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
</asp:Content>

<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">
    <div>Hello World</div>
    <div>$SharePoint.Type.df45d565-73d2-447f-a463-5a362d2fc50d.AssemblyQualifiedName$</div>
</asp:Content>


with that pasted in I want you to pay extra attention to the highlighted parts. namely the codeBehind attribute on the Page and the to instances of the GUID df45d565-73d2-447f-a463-5a362d2fc50d this is going to make reference to our code behind. Make sure that all the alpha characters are all in lower-case.

if you would like more information on Sharepoint place holders, check out this. it'll give you a list of SharePoint specific place holders and an explanation of them.

Anyway now that we've pasted this lovely script into our sample.txt page let's go ahead and rename it MySitePage.aspx.

with that complete we now need to create that codebehind page we kept talking about, namely MySitePage.aspx.cs, to do this click on your module in your solution explorer and hit ctrl+shift+a again. This time in the left hand column make sure you have Visual C# items selected then select the Class item, give it the appropriate name (MySitePage.aspx this time) and hit OK.

now your solution explored should fall into place nicely.

there's some more wire up that needs to be completed in your codebehind but this what you should be starting with

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace spm.SitePageExample.CustomSitePage
{
    class MySitePage
    {
    }
}


what we are going to have to do is add the following two using statements.
  • using System.Runtime.InteropServices;
  • using Microsoft.SharePoint.Publishing;


add a Guid attribute to your class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Publishing;

namespace spm.SitePageExample.CustomSitePage
{
    [Guid("df45d565-73d2-447f-a463-5a362d2fc50d")]
    class MySitePage
    {
    }
}


inherit from PublishinLayoutPage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Publishing;

namespace spm.SitePageExample.CustomSitePage
{
    [Guid("df45d565-73d2-447f-a463-5a362d2fc50d")]
    public class MySitePage : PublishingLayoutPage
    {
    }
}


with that complete the next thing we have to do is open up the elements file and set up our deployment.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="CustomSitePage">
    <File Path="CustomSitePage\MySitePage.aspx"
          Url="CustomSitePage/MySitePage.aspx" />
  </Module>

</Elements>

above is what we get out of the box, change it to
<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="CustomSitePage" Url="$Resources:cmscore,List_Pages_UrlName;" Path="CustomSitePage">
    <File Path="MySitePage.aspx" Url="MySitePage.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
      <Property Name="Title" Value="My Site Page" />
      <Property Name="ContentType"
                Value="$Resources:cmscore,contenttype_pagelayout_name;" />
      <Property Name="PublishingPreviewImage"
                Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/DefaultPageLayout.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/DefaultPageLayout.png" />
      <Property Name="PublishingAssociatedContentType"
                Value=";#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D;#"/>
    </File>
  </Module>
</Elements>


Now go to your feature, make sure your module is included and it can be site, or web scoped. deploy and you should be done.