65.9K
CodeProject is changing. Read more.
Home

How to Programmatically Create Ghosted Files in SharePoint 2007 or 2010 using the Object Model API Only

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2 votes)

Jul 24, 2010

Public Domain

1 min read

viewsIcon

35690

downloadIcon

219

Provisioning of ghosted (uncustomized) files that are rooted in the File system rather than the content Database.

Introduction

I wanted to do manual provisioning of files for a SharePoint project when it became clear that neither SiteDefinitions nor consistently creating new features would do the job of maintaining a product over several update cycles. However, creating files using the SharePoint object model API only allows for uploading new content files into the database, not for registering ghosted files that are rooted in the file system (12/Template or 14/Template for 2010).

I searched the web for solutions but couldn't find any really promising one. Some directly hacked the content-database using SQL, some leveraged OWSSRC-RPC calls or used the WebServices. None was supported (mine isn't either, although much less critically a hack ;).

Method

I had to find a different method to do it and so I went to Reflector checking out what SharePoint internally would do and ended up calling the same stuff through Reflection.

This is basically it: By creating XML for a dummy module as it would appear in a feature and invoking EnableModuleFromXml, plus some checks & co.

I thought maybe someone might find it useful. Code can be compiled for V12 and V14 object models and runs on SharePoint 2007 and 2010. This is early stage and hardly tested, so please check it out for yourself if it suits your needs.

Using the Code

There is a code file attached in which you find an Utility class in case you ever need to do these things (Note: I give absolutely no warranties for any kind of problems you might incur using it).

This will reduce the issue to a one-line call, which you have four of in the API from the utility class:

// Example program (adjust URLs to your needs and make sure template 
// files are available in the 12/14 hive): 

using (SPSite site = new SPSite("http://yoursharepointserver/yourwebsite"))
using (SPWeb web = site.OpenWeb())
{
    SPList list = web.GetList(web.Url + "/Lists/SampleList"); 
    SPDocumentLibrary doclib = web.GetList(web.Url + "/Documents") as SPDocumentLibrary; 

    FileProvisionUtility.GhostListForm(@"FEATURES\yourfeature\Files\DispForm.aspx", 
	list, FileProvisionMode.OverwriteExistingIfNotCustomized);

    FileProvisionUtility.GhostLibraryForm(@"FEATURES\yourfeature\Files\DispForm.aspx", 
	doclib, FileProvisionMode.OverwriteExistingIfNotCustomized);

    FileProvisionUtility.GhostLibraryFile
	(@"FEATURES\yourfeature\Files\HelloGhostWorld.txt", doclib, "", 
	FileProvisionMode.OverwriteExistingIfNotCustomized);

    FileProvisionUtility.GhostFile(@"FEATURES\yourfeature\Files\NewHomePage.aspx", 
	web, "", "default.aspx", false, 
	FileProvisionMode.OverwriteExistingIfNotCustomized); 
} 

Enjoy!