Click here to Skip to main content
15,893,381 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
24 Jul 2010Public Domain1 min read 35.5K   218   5   2
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:

C#
// 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!

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAwesome Pin
jkuter19-Jul-11 6:09
jkuter19-Jul-11 6:09 
GeneralThanks Pin
gojics13-Jul-11 16:47
gojics13-Jul-11 16:47 

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.