Finding Ghosts: Programmatically Determining Ghosted/Unghosted State


 

First, some terminology.  The terms “ghosted” and “unghosted” get thrown around a lot when talking about SharePoint site definitions, and all too often, they are used incorrectly.  Here’s the deal:


 


Ghosted:  a ghosted page is one that is read directly from the site definition on the filesystem.


Unghosted:  an unghosted page is one that is read from the SharePoint database and *not* a physical file


 


So, how can we determine if a given page is either ghosted or unghosted?  Luckily, there is a single property that will tell us:  “vti_hasdefaultcontent.”  If this property exists on a file, it is ghosted (again, on the filesystem);  if the property does not exist, the file is being served from the database.


 


Here’s the code for a simple command-line utility that will take the URL to a given page and report whether it is ghosted or unghosted:


 


=====


using System;


using Microsoft.SharePoint;


 


namespace GhostCheck


{


public class Checker


{


[STAThread]


static void Main(string[] args)


{


//if vti_hasdefaultocontent = true, it is ghosted;  if unghosted, the prop doesn’t exist


 


if(args.Length == 0 || args[0].ToString().Equals(“/?”))


{


Help();


}


else


{


string sURL = args[0].ToString();


bool hasError = false;


string sGhosted = CheckGhost(sURL, out hasError);


 


if(hasError)


{


Console.WriteLine(“\nError encountered: {0}”, sGhosted);


}


else


{


Console.WriteLine(“\nThe page at {0} is {1}”, sURL, sGhosted);


}


 


}


 


}


 


static  void Help()


{


Console.WriteLine(“\nghostcheck <URL>”);


Console.WriteLine(“\nExample:  ghostcheck http://<myserver>/sites/<site>/Lists/Contacts/NewForm.aspx“);


}


 


static string CheckGhost(string FileURL, out bool ErrorFlag)


{


ErrorFlag = false;


 


SPSite oSite = null;


SPWeb oWeb = null;


SPFile oFile = null;


 


try


{


oSite = new SPSite(FileURL);


}


catch(System.Exception exSite)


{


ErrorFlag = true;


return String.Format(“Error getting Site: {0}”, exSite.Message);


}


 


try


{


oWeb = oSite.OpenWeb();


}


catch(System.Exception exWeb)


{


ErrorFlag = true;


return String.Format(“Error getting Web: {0}”, exWeb.Message);


}


 


try


{


oFile = oWeb.GetFile(FileURL);


}


catch(System.Exception exFile)


{


ErrorFlag = true;


return String.Format(“Error getting File: {0}”, exFile.Message);


}


 


System.Collections.Hashtable oHash = oFile.Properties;


 


if(oHash.ContainsKey(“vti_hasdefaultcontent”))


{


return “ghosted (on file system)”;


}


else


{


return “unghosted (in database)”;


}


}


}


}


 


=====


 


This utility uses the SharePoint object model, and so MUST BE RUN ON THE SERVER, not from a client.


 


Usage:


 


ghostcheck.exe <URL>


 


Example:


 


ghostcheck.exe http://myserver/sites/mysite/Lists/Contacts/NewForm.aspx

Comments (49)

  1. Pol Pit says:

    I ‘m so [url=http://access.122mb.com]lucky[/url] on having what I have!

    Just visit [url=http://access.serverheaven.net]my site[/url]. Just see it! And good luck in yours [url=http://access.122mb.com]search[/url].

  2. sausau says:

    Excellent browsing have the to

  3. gimi says:

    Excellent browsing have the to

  4. gimi says:

    Excellent browsing have the to

  5. solo says:

    Excellent browsing have the to

  6. gimi says:

    Excellent browsing have the to

  7. theital says:

    Excellent browsing have the to

  8. gimi says:

    Excellent browsing have the to

  9. gimi says:

    Excellent browsing have the to

  10. ware,ware says:

    Beautiful site!,Beautiful site!

  11. chat,chat says:

    Very interesting.,Very interesting.

  12. sarde,sarde says:

    Beautiful site!,Beautiful site!

  13. Very interesting.,Very interesting.

  14. Very good website you have here.: Thanks!,Very good website you have here.: Thanks!

  15. Really amazing! interesting site. keep up the good work. Thanks!,Really amazing! interesting site. keep up the good work. Thanks!

  16. Bill says:

    More information, you work, stress, then you focus on a creative. Appraisal discussion, ask figure of his new overcome, or off to describe. Foundation for them out the basic level, this regularly undertake a [url=http://learnin.43i.net ]learning center.[/url].

  17. Lucy! Please call me,Lucy! Please call me

  18. Lucy! Please call me,Lucy! Please call me

  19. Lucy! Please call me,Lucy! Please call me

  20. Britneyhdojx says:

    Very nice! I have some LJ with news, check this out:

    <a href= http://michelas.livejournal.com >My live journal</a>

    <a href= http://homerius.livejournal.com >Lastest news</a>

    <a href= http://johnyknoxw.livejournal.com >My live journal</a>

  21. Britneyyfayv says:

    Very nice! I have some LJ with news, check this out:

    <a href= http://iwantubadlyz.livejournal.com >Newest news</a>

    <a href= http://annakubat.livejournal.com >Check this out</a>

    <a href= http://jackie_simpson.livejournal.com >livejournal</a>

  22. Buy Evista says:

    This is really fresh idea of the design of the site! I seldom met such in Internet. I will visit your website again. Good Work dude!

  23. Frankly, the way things are right now, I’m not sure I’d want to play myself in my very own movie of the week.

  24. Looks great! I found lots of intresting things here. Please more updates.

  25. Looks great! I found lots of intresting things here. Many thanks.

  26. If you listen to the Matrix soundtrack on your Ipod, or perhaps a fun song, your life automatically becomes a movie.

  27. Buy Caverta says:

    Nice design. Please add more smiles to your guestbook 🙂  Please more updates.

  28. Buy Caverta says:

    Frankly, the way things are right now, I’m not sure I’d want to play myself in my very own movie of the week.

  29. Hi Webmaster! It was a pleasure to look through this site! there is a lot of new and fresh ideas)!Thank You

  30. Buy Caverta says:

    Nice design. Please add more smiles to your guestbook 🙂  Please more updates.

  31. Buy Caverta says:

    I wanted to thank you for the time you spent building this page. I will visit your website again. Thank you

  32. Frankly, the way things are right now, I’m not sure I’d want to play myself in my very own movie of the week.

  33. Andy Fitch says:

    A great snippet of code – very helpfull!