SPContentTypeUsage.Url property

Gets the Uniform Resource Locator (URL) for the content type.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public string Url { get; }

Property value

Type: System.String
A server-relative URL.

For site content types, the property returns a server-relative URL for the Web site. For list content types, the Url property returns a server-relative URL for the root folder of the list.

The following example shows a console application that gets the usage collection for the built-in content type Item. The application uses the Url property of each SPContentTypeUsage object in the collection to determine the name of that instance of the parent content type. Then the application prints the name of the instance, the scope (site or list) of the instance, and the value of the Url property to the console.

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("http://localhost"))
         {
            using (SPWeb rootWeb = siteCollection.RootWeb)
            {
               // Get the content type.
               SPContentType contentType =
                  rootWeb.AvailableContentTypes[SPBuiltInContentTypeId.Item];

               //Get the usage collection.
               IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(contentType);

               foreach (SPContentTypeUsage usage in usages)
               {
                  // Get the name of the content type.
                  string ctName = String.Empty;

                  if (usage.IsUrlToList) // List content type
                  {
                     foreach (SPWeb web in siteCollection.AllWebs)
                     {
                        foreach (SPList list in web.Lists)
                        {
                           if (list.RootFolder.ServerRelativeUrl == usage.Url)
                           {
                              ctName = list.ContentTypes[usage.Id].Name;
                              break;
                           }
                        }

                        web.Dispose(); // Clean up

                        if (ctName != String.Empty)
                           break;
                     }
                  }
                  else // Site content type.
                  {
                     SPWeb web = siteCollection.OpenWeb(usage.Url);
                     ctName = web.AvailableContentTypes[usage.Id].Name;
                     web.Dispose();
                  }

                  Console.WriteLine("\nContent type name: {0}", ctName);
                  Console.WriteLine("This is a {0} content type.", 
                                    usage.IsUrlToList ? "list" : "site");
                  Console.WriteLine("URL: {0}", usage.Url);
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

When the application is run against a site collection with a root Web site and one child site, it prints the following (partial) output to the console.

Content type name: Task
This is a site content type.
URL: /

Content type name: Feature Points of Contact
This is a site content type.
URL: /Subsite

Content type name: Task
This is a list content type.
URL: /Lists/Tasks

Content type name: Feature Points of Contact
This is a list content type.
URL: /Subsite/Lists/Subsite List
Show: