vrijdag 16 oktober 2009

Problem when deleting SharePoint content types

We all had this problem at some time: you made some content types either using the GUI or the feature, used the content type all over the place and when, for some reason, you want to delete the content type it's impossbible because somewhere in all these sites, the content type is still in use.
Of course you don't want to search for this manually, so I made a little console application which does the job for you.
I know this solutions already exist but most of the time the require you to install a wsp. This console application you can use immediatelly, does not require any installing and you can easily use it on different servers.

Usage:
FindContentType [Site collection URL] [Content type name]

It will go through the complete site collection and give you the URL's of all lists that still use this content type. It will also create a log file which will contain these URL's for more easy copy pasting.

Here's the code:

static void Main(string[] args)
{

try
{

if (args.Length != 2)
{

Console.WriteLine("Usage: [Site Collection URL] [Content Type Name]");

}
else
{

FindContentTypes(args[0], args[1]);

}

Console.WriteLine("Finished all tasks. Press any key to close ...");
Console.ReadKey();

}
catch (Exception ex)
{

Console.WriteLine(ex.Message + ex.StackTrace);

}

Environment.Exit(0); // Clean exit

}

private static void FindContentTypes(String scUrl, String contentTypeName)
{

TextWriter tw = new StreamWriter("log.txt", true);
tw.WriteLine("Starting new sessions: " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));

using (SPSite site = new SPSite(scUrl))
{

SPWebCollection webCollection = site.AllWebs;

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

using(SPWeb web = webCollection[i])
{

for (int j = 0; j < web.Lists.Count; j++)
{

Console.WriteLine("Processing: " + web.Url + "/" + web.Lists[j].RootFolder.Url);

for (int x = 0; x < web.Lists[j].ContentTypes.Count; x++)
{

if(web.Lists[j].ContentTypes[x].Name.ToLower() == contentTypeName.ToLower())
{

Console.WriteLine("Content type '" + contentTypeName + "' is still being used in "
+ web.Url + "/" + web.Lists[j].RootFolder.Url);
tw.WriteLine("Content type '" + contentTypeName + "' is still being used in "
+ web.Url + "/" + web.Lists[j].RootFolder.Url);

}

}

}

}

}

}

tw.Close();

}

Geen opmerkingen:

Een reactie posten