Tuesday, February 19, 2013

Building custom SharePoint 2010 Site Pages using Visual Studio (Part I)

Hi everyone. I decided today to write some instructions on how to create Site Pages that you can customize via Visual Studio (VS). Including the ability to execute server-side scripting. As I couldn't find a good resource on the internet for this I decided to do it myself.

First you need to create the custom site page. You could go through and build the page completely within SharePoint Designer (SPD) but, this will at least give us a good starting point for VS.

  1. Launch Internet Explorer (IE) and navigate to the Site where you wish to create your custom site page. In my case this will be http://localhost/mycustomsite.
  2. Select Site Actions | Edit in SharePoint Designer.
  3. Select File | Add Item | New Page from Master | v4.master (Default) | Create. When it asks you, change the name to MyCustomPageTemplate and in my case I am using the Site Pages library. But, you can use any Wiki Page Library you have setup as well.
  4. Click OK.
  5. Give it a second or two and when the alert comes up just click Yes.

    NOTE: At this point I am assuming C-Sharp (C#) but if you are a Visual Basic (VB) programmer you can just change the language setting in the page tag as is appropriate.
  6. Switch the page to Design view.
  7. Locate the PlaceHolderMain(Master) content place holder. It is the open area in the middle of the page for those not familiar. In the menu for it, click Create Custom Content. That menu can be accessed by clicking the little drop-down arrow right on the component itself.
  8. You can now click into the PlaceHolderMain(Custom) area, notice it says custom now, and it can be editted. Type some placeholder information like "My Content Goes Here".
  9. Switch the page to Code view.
  10. Hit [CTRL+A] to highlight all the contents of the page and [CTRL+C] to copy it into your clipboard.
  11. Save the page and close SPD.
Now we are ready to get into VS to finish our page up. We are going to use a Module to provision the page(s) in SharePoint. To provision a file into a particular document library (e.g., Site Pages) you need to create a module with the same name as the document library. You then add the files you want provisioned and fix up the Categorical Abstract Machine Language (CAML) file so they load the way you want them too.

  1. Launch VS and create a new Empty SharePoint Project. Let's call it the CustomPages project for now.
  2. Right-click the CustomPages project and select Add | New Item | Module.
  3. Name the module SitePages or whatever your library is and click Add.
  4. In the CustomPages project, select File | New File... | Web | Visual Basic or C# | Web Form and click Open.
  5. Right-click inside the contents of the file (anywhere on the screen should be fine) and select Move Default1.aspx into | CustomPages. Navigate to your SitePages module and save the file there with a name of MyCustomPage.aspx or whatever you want to call it.

    NOTE: Make sure that the new page shows up in your Solution Explorer window under the SitePages folder. If it doesn't you may need to drag-and-drop it there.
  6. Highlight all of the contents of the MyCustomPage.aspx and replace with the contents on your clipboard.
  7. Expand the Features folder and double-click Feature1 to open its settings.
  8. Set the feature title to CustomPages (or whatever makes sense) and set an appropriate description.
  9. Switch back to your solution explorer. Go ahead and delete the Sample.txt document found in your SitePages module folder.
  10. Open the Elements.xml document for editting. Remove the SitePages/ from the Url. The Path attribute should be left alone.
  11. Add the attribute Type with the value GhostableInLibrary to the File element you just modified. Also, add a Url of SitePages to the Module element. Should look like something this:
     
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        <Module Name="SitePages" Url="SitePages">
            <File Path="SitePages\MyCustomPage.aspx"
                     Url="MyCustomPage.aspx"
                     Type="GhostableInLibrary" />
        </Module>
    </Elements>
     
  12. Save all of your changes.
You should now be able to Build and then Deploy. Congratulations, you now have a Site Page constructed in VS and deployed to your site.

In my next blog post I'll describe how to mark your page as safe and show how to enable code-behind for these Site Pages.