SPContentTypeUsage.Url property
取得內容型別中的統一資源定位器 (URL)。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public ReadOnly Property Url As String
Get
'用途
Dim instance As SPContentTypeUsage
Dim value As String
value = instance.Url
public string Url { get; }
Property value
Type: System.String
伺服器相對 URL。
備註
網站內容類型,則屬性會傳回 Web 站台的伺服器相對 URL。清單內容類型, Url屬性會傳回清單的根資料夾的伺服器相對 URL。
Examples
下列範例會示範取得內建內容類型Item的使用方式集合的主控台應用程式。應用程式會使用集合中的每個SPContentTypeUsage物件的 [ Url ] 屬性來決定父系內容類型的執行個體的名稱。然後應用程式是列印範圍 (站台或執行個體的清單) 執行個體的名稱和主控台的 [ Url ] 屬性的值。
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();
}
}
}
當應用程式執行對根 Web 站台和一個子網站的網站集合時,它會列印下列 (部分) 輸出到主控台。
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
請參閱
參照
Microsoft.SharePoint namespace