MOSS2007 – Where are my content types being used?

Recently someone emailed me asking if there was a way to find out what content types various lists and libraries were using in MOSS2007. After a little investigation I decided to write some C# code. If you take a look at the object model you will notice that you have hierarchy that you can traverse down to find this information. In the code I have used I am using the following:

SPSite Class – The root Site (Portal)

SPWeb Class – To get details about the current site (Portal)

SPWebCollection Class – List all sub sites that exist at the current parent site (Portal)

SPListCollection Class – List all the Lists and Libraries within the various sites under the portal

SPContentTypeCollection Class – List all the content types that are associated with the selected list

 

As you can see there is a very nice structure available for us to traverse down. The code is not the most elegant and has no error handling and I am sure there are better ways to write it but you get the idea. I created a test console application for this to simply output to the screen. Anyway the code:

SPSite siteCollection;

SPWeb webSite;

static void Main(string[] args)

{

GetCurrentMOSSContext(args[0].ToString());

}

static void GetCurrentMOSSContext(string PortalURL)

{

siteCollection = new SPSite(PortalURL.ToString());

      webSite = siteCollection.AllWebs["/"];

 

      System.Console.WriteLine(webSite.Url.ToString());

}

 

static void GetAllSubSitesListsAndContentTypesUnderContext()

{

SPWebCollection subSites = siteCollection.AllWebs;

 

for (int i = 0; i < subSites.Count; i++)

{

System.Console.WriteLine(“…” + subSitesIdea.Title.ToString() + ” – Site”);

 

            SPListCollection lists = subSitesIdea.Lists;

            for (int j = 0; j < lists.Count; j++)

{

System.Console.WriteLine(“……” + lists[j].Title.ToString() + ” – List”);

 

                  SPContentTypeCollection types = lists[j].ContentTypes;

                  for (int k = 0; k < types.Count; k++)

                  {

System.Console.WriteLine(“………” + types[k].Name.ToString() + ” – Content Type”);

                  }

}

}

}

 

As you can see the code is very simple and works like a treat. Be aware that this code only works for root site collections. You would need to change it to make it work when trying to check a site collection that had the following format of URL: http://[Server]/[Sites]/[Site]

Once it is run the output is simliar to this:

http://intranet.labs.local

….Document Center – Site

……..Announcements – List

…………Announcement – Content Type

…………Folder – Content Type

……..Documents – List

…………Document – Content Type

…………Folder – Content Type

……..Master Page Gallery – List

…………Master Page – Content Type

…………Folder – Content Type

……..Tasks – List

…………Task – Content Type

…………Folder – Content Type

 

Anyway hopefully you get the idea. Big Smile

Leave a Reply


MOSS2007 – Where are my content types being used?

Recently someone emailed me asking if there was a way to find out what content types various lists and libraries were using in MOSS2007. After a little investigation I decided to write some C# code. If you take a look at the object model you will notice that you have hierarchy that you can traverse down to find this information. In the code I have used I am using the following:

SPSite Class – The root Site (Portal)

SPWeb Class – To get details about the current site (Portal)

SPWebCollection Class – List all sub sites that exist at the current parent site (Portal)

SPListCollection Class – List all the Lists and Libraries within the various sites under the portal

SPContentTypeCollection Class – List all the content types that are associated with the selected list

 

As you can see there is a very nice structure available for us to traverse down. The code is not the most elegant and has no error handling and I am sure there are better ways to write it but you get the idea. I created a test console application for this to simply output to the screen. Anyway the code:

SPSite siteCollection;

SPWeb webSite;

static void Main(string[] args)

{

GetCurrentMOSSContext(args[0].ToString());

}

static void GetCurrentMOSSContext(string PortalURL)

{

siteCollection = new SPSite(PortalURL.ToString());

      webSite = siteCollection.AllWebs["/"];

 

      System.Console.WriteLine(webSite.Url.ToString());

}

 

static void GetAllSubSitesListsAndContentTypesUnderContext()

{

SPWebCollection subSites = siteCollection.AllWebs;

 

for (int i = 0; i < subSites.Count; i++)

{

System.Console.WriteLine(“…” + subSitesIdea.Title.ToString() + ” – Site”);

 

            SPListCollection lists = subSitesIdea.Lists;

            for (int j = 0; j < lists.Count; j++)

{

System.Console.WriteLine(“……” + lists[j].Title.ToString() + ” – List”);

 

                  SPContentTypeCollection types = lists[j].ContentTypes;

                  for (int k = 0; k < types.Count; k++)

                  {

System.Console.WriteLine(“………” + types[k].Name.ToString() + ” – Content Type”);

                  }

}

}

}

 

As you can see the code is very simple and works like a treat. Be aware that this code only works for root site collections. You would need to change it to make it work when trying to check a site collection that had the following format of URL: http://[Server]/[Sites]/[Site]

Once it is run the output is simliar to this:

http://intranet.labs.local

….Document Center – Site

……..Announcements – List

…………Announcement – Content Type

…………Folder – Content Type

……..Documents – List

…………Document – Content Type

…………Folder – Content Type

……..Master Page Gallery – List

…………Master Page – Content Type

…………Folder – Content Type

……..Tasks – List

…………Task – Content Type

…………Folder – Content Type

 

Anyway hopefully you get the idea. Big Smile

Leave a Reply