Custom SharePoint development
usually requires the creation of custom content types. While the process of
defining, packaging and deploying content types as solutions (.wsp) is
simplified in Visual Studio 2010, I have encountered issues while upgrading modified content types
during the development process. The problem is basically caused when a content type is still being used somewhere in SharePoint. Determining exactly where with the standard SharePoint web interface can be time consuming and occasionally not possible.
Below are a couple of console applications I created to address these issues. The complete Visual Studio 2010 project and source code is available for download at https://barkes@bitbucket.org/barkes/sharepoint-utilities
Below are a couple of console applications I created to address these issues. The complete Visual Studio 2010 project and source code is available for download at https://barkes@bitbucket.org/barkes/sharepoint-utilities
SPEnumContentTypes.cs
using System; using System.Collections; using Microsoft.SharePoint; namespace Barkes.SharePoint.CmdLine { class SPEnumContentTypes { static void Main(string[] args) { if (args.Length == 1) { string url = args[0]; WriteContentTypes(url); } else { WriteUsage(); } } /// <summary> /// Show the command usage on the console. /// </summary> private static void WriteUsage() { Console.WriteLine(); Console.WriteLine("Barkes SharePoint Utilities"); Console.WriteLine(); Console.WriteLine("SPEnumContentTypes url"); Console.WriteLine(); Console.WriteLine("SPEnumContentTypes http://myserver/mysite"); Console.WriteLine(); } private static void WriteContentTypes(string url) { using (SPSite site = new SPSite(url)) { using (SPWeb web = site.OpenWeb()) { // Create a sortable list of content types ArrayList list = new ArrayList(); foreach (SPContentType ct in web.AvailableContentTypes) list.Add(ct); // Sort the list on group name list.Sort(new CTComparer()); // Write the header Console.WriteLine("\nSite: {0}", url); Console.WriteLine("Total Content Types: {0}", list.Count); Console.WriteLine("\n{0,-35} {1,-12} {2}", "Site Content Type", "Parent", "Content Type ID"); for (int i = 0; i < list.Count; i++) { SPContentType ct = (SPContentType)list[i]; if (i == 0 || ((SPContentType)list[i - 1]).Group != ct.Group) { Console.WriteLine("\n{0}", ct.Group); Console.WriteLine("-------------------------------"); } Console.WriteLine("{0,-35} {1,-12} {2}", ct.Name, ct.Parent.Name, ct.Id); } } } } // Implements the Compare method from the IComparer interface. // Compares two content type objects by group name, then by content type Id. class CTComparer : IComparer { int IComparer.Compare(object x, object y) { SPContentType ct1 = (SPContentType)x; SPContentType ct2 = (SPContentType)y; // First, compare group names int result = string.Compare(ct1.Group, ct2.Group); if (result != 0) return result; // If the names are the same, compare the IDs return ct1.Id.CompareTo(ct2.Id); } } } }
SPGetSupportedContentType.cs
using System; using System.Collections.Generic; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace Barkes.SharePoint.CmdLine { class SPGetSupportedContentTypes { static void Main(string[] args) { if (args.Length == 2) { string url = args[0]; string ctName = args[1]; WriteSupportedContentTypes(url, ctName); } else { WriteUsage(); } } /// <summary> /// Show the command usage on the console. /// </summary> private static void WriteUsage() { Console.WriteLine(); Console.WriteLine("Barkes SharePoint Utilities"); Console.WriteLine(); Console.WriteLine("SPGetSupportedContentTypes url content_type_name"); Console.WriteLine(); Console.WriteLine("SPGetSupportedContentTypes http://myserver/mysite MyContentType"); Console.WriteLine(); } private static void WriteSupportedContentTypes(string url, string ctName) { try { using (SPSite site = new SPSite(url)) { SPWebCollection webs = site.AllWebs; Console.WriteLine("ctype.Id\tctype.Name\tlist.ID\tlist.DefaultViewUrl"); foreach (SPWeb web in webs) { foreach (SPList list in web.Lists) { foreach (SPContentType ctype in list.ContentTypes) { if (ctype.Name == ctName) { Console.WriteLine("{0}, {1}, {2}, {3}", ctype.Id, ctype.Name, list.ID, list.DefaultViewUrl); Console.WriteLine(); } } } web.Dispose(); } } } catch (Exception ex) { Console.WriteLine("An error occurred: "); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } } }
No comments:
Post a Comment