SPContentType.ParentWeb Property
Gets the parent Web site for the content type.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
The following example is a console application that enumerates all lists in a site, printing the name of each list to the console. In addition, the example code enumerates each list’s content types, printing the name of each content type to the console as well as a server-relative URL for the content type’s parent Web.
using System; using Microsoft.SharePoint; namespace MyTest { class ConsoleApp { static void Main(string[] args) { Console.WriteLine(); SPSite oSPSite = new SPSite("http://localhost"); SPWeb oSPWeb = oSPSite.OpenWeb(); SPListCollection oListCollection = oSPWeb.Lists; foreach (SPList oList in oListCollection) { Console.WriteLine("List title: " + oList.Title); Console.WriteLine(); foreach (SPContentType oContentType in oList.ContentTypes) { Console.WriteLine(" Content type name: " + oContentType.Name); SPWeb oParent = oContentType.ParentWeb; Console.WriteLine(" Content type's parent Web: " + oParent.ServerRelativeUrl); Console.WriteLine(); oParent.Dispose(); } Console.WriteLine(); } oSPWeb.Dispose(); oSPSite.Dispose(); Console.Write("Press ENTER to continue..."); Console.ReadLine(); } } }
Some of the output printed to the console might look like this:
List title: Announcements Content type name: Announcement Content type's parent Web: / Content type name: Folder Content type's parent Web: / List title: Calendar Content type name: Event Content type's parent Web: / List title: Links Content type name: Link Content type's parent Web: / Content type name: Folder Content type's parent Web: /