Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET

Ghosting the Unghostable

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Dec 2009CPOL2 min read 24.8K   80   9   1
This example shows how to ghost non-ghostable files in SharePoint as well as how to relocate existing ghosted content.

Introduction

This article shows a way to remap a ghosted file to a new location, and also shows how to ghost a non-ghostable file, such as one originally created via SharePoint designer. This assumes a basic knowledge of SharePoint and the concept of ghosted and unghosted files.

This is actually a trick which overwrites the existing file with a new ghosted version. To actually modify a non-ghostable file so that it becomes ghostable is near on impossible without committing the cardinal sin of modifying the content database directly. An alternative is to delete the original and deploy the ghosted version in its place; however, problems arise with layouts (for example) that may still be referenced by existing content. I found the following technique overcomes these issues and allows files to be swapped out with a ghosted version seamlessly.

Note: if your desire is simply to reghost an existing file, then I recommend one of the many existing tools available, such as gl-reghostfile.

The Solution

In this example, we will remap the ArticleLeft layout from the SharePoint publishing template to an updated version deployed via our sample feature. This changes the location of the ghosted file to that of our feature, and allows us to deploy subsequent changes to the ArticleLeft layout by upgrading the new solution. The sample adds the word MODIFIED to the page title of the template.

The first step is to save out the existing file from the SharePoint database using the SharePoint designer. This is then added to a feature in the usual way as if you were provisioning it for the first time. In the case of layouts, be sure to also provision the properties from the original file; otherwise, the new layout will not behave correctly.

XML
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Url="_catalogs/masterpage" Path="Layouts">
    <File Url="ArticleLeft_Ghosted.aspx" 
         Type="GhostableInLibrary" IgnoreIfAlreadyExists="true">
      <Property Name="Title" 
         Value="Modified Article page with image on left" />
      <Property Name="MasterPageDescription" 
         Value="The modified article page with image on left contains 
                an image field and a rich text field." />
      <Property Name="ContentType" 
         Value="Page Layout" />
      <Property Name="PublishingPreviewImage" 
         Value="/_catalogs/masterpage/en-US/Preview Images/ArticleLeft.png, 
                /_catalogs/masterpage/en-US/Preview Images/ArticleLeft.png" />
      <Property Name="PublishingAssociatedContentType" 
         Value=";#Article Page;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007
                948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D;#" />
    </File>
  </Module>
</Elements>

The next step is to rename the file so that it can initially be deployed alongside the the existing layout. In the example included, I have named the file ArticleLeft_Ghosted.aspx.

Lastly, we add a feature receiver with the following code to overwrite the target file with the new ghosted version. Note: this code has been simplified for brevity; see the sample for full code.

C#
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;

    PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(site.RootWeb);

    // get reference to newly provisioned file
    SPFile file = site.RootWeb.GetFile("/_catalogs/masterpage/ArticleLeft_Ghosted.aspx");
    if (file != null)
    {
        // overwrite the old layout with the ghosted version
        file.MoveTo("/_catalogs/masterpage/ArticleLeft.aspx", true);
        file.Update();
    }
}

Once overwritten, the new version of the file remains ghosted. In the case of layouts and master pages, all existing content will begin to use the ghosted version. The key to the technique is the MoveTo command which allows any referenced layout to be overwritten without breaking references to existing content.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
Mike Carlisle - Technical Architect with over 20 years experience in a wide range of technologies.

@TheCodeKing

Comments and Discussions

 
GeneralExcellent solution - thanks a heaven Pin
Member 928381920-Jul-12 7:24
Member 928381920-Jul-12 7:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.