SPContentTypeUsage.Url 属性
获取内容类型的统一资源定位器 (URL)。
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public ReadOnly Property Url As String
Get
用法
Dim instance As SPContentTypeUsage
Dim value As String
value = instance.Url
public string Url { get; }
属性值
类型:System.String
相对于服务器的 URL。
备注
网站内容类型的属性返回网站的相对于服务器的 URL。对于列表内容类型,则Url属性返回的列表的根文件夹的相对于服务器的 URL。
示例
下面的示例演示的控制台应用程序获取内置内容类型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();
}
}
}
应用程序运行时针对网站集的根网站和一个子网站,以此方式打印到控制台以下 (部分) 输出。
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