共用方式為


SPContentTypeUsage.Url Property

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

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public ReadOnly Property Url As String
    Get
'Usage
Dim instance As SPContentTypeUsage
Dim value As String

value = instance.Url
public string Url { get; }

Property Value

Type: System.String
A server-relative URL.

Remarks

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.

Examples

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.

Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using siteCollection As SPSite = New SPSite("https://localhost")
         Using webSite As SPWeb = siteCollection.RootWeb

            ' Get the content type.
            Dim contentType As SPContentType = _
               webSite.AvailableContentTypes(SPBuiltInContentTypeId.Item)

            ' Get the usage collection.
            Dim usages As IList(Of SPContentTypeUsage) = _
                                SPContentTypeUsage.GetUsages(contentType)

            For Each usage As SPContentTypeUsage In usages

               ' Get the name of this instance.
               Dim ctName As String = String.Empty

               If usage.IsUrlToList Then ' List content type
                  For Each web As SPWeb In siteCollection.AllWebs

                     For Each list As SPList In web.Lists
                        If list.RootFolder.ServerRelativeUrl = usage.Url Then
                           ctName = list.ContentTypes(usage.Id).Name
                           Exit For
                        End If
                     Next list

                     web.Dispose() ' Clean up

                     If ctName <> String.Empty Then
                        Exit For
                     End If

                  Next web

               Else ' Site content type.
                  Dim web As SPWeb = siteCollection.OpenWeb(usage.Url)
                  ctName = web.AvailableContentTypes(usage.Id).Name
                  web.Dispose()
               End If

               Console.WriteLine(vbCrLf + "Content type name: {0}", ctName)
               Console.WriteLine("This is a {0} content type", _
                                 IIf(usage.IsUrlToList, "list", "site"))
               Console.WriteLine("URL: {0}", usage.Url)

            Next usage

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub

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

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("https://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

See Also

Reference

SPContentTypeUsage Class

SPContentTypeUsage Members

Microsoft.SharePoint Namespace

Other Resources

Introduction to Content Types

Site and List Content Types