Click here to Skip to main content
Click here to Skip to main content

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

, 24 Jul 2010 Public Domain
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;) .

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!

License

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

Share

About the Author

FREVDS

Germany Germany
No Biography provided

Comments and Discussions

 
QuestionAwesome Pinmemberjkuter19-Jul-11 6:09 
GeneralThanks Pinmembergojics13-Jul-11 16:47 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 24 Jul 2010
Article Copyright 2010 by FREVDS
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid