如何:删除托管属性
通过企业级搜索管理对象模型中的 [Schema] 对象可以访问为共享服务提供程序 (SSP) 的搜索服务配置的托管属性。有关 Schema 对象的详细信息,请参阅管理元数据。
以下过程介绍在控制台应用程序中如何使用企业级搜索管理对象模型,以编程方式删除托管属性。
从控制台应用程序中删除托管属性
在应用程序中设置对以下 DLL 的引用:
Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.Search.dll
在控制台应用程序的类文件中,将以下 using 语句添加到具有其他命名空间指令的代码顶部附近。
using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration;
创建用于向控制台窗口写入使用信息的函数。
static void Usage() { Console.WriteLine("Delete Managed Properties Sample"); Console.WriteLine("Usage: DeleteManagedPropertiesSample.exe PropertyName"); }
在控制台应用程序的 Main() 函数中,添加检查 args[] 参数中项数的代码;这个数必须等于 1。否则,请调用步骤 3 中定义的 Usage() 函数。
if (args.Length != 1) { Usage(); return; }
检索 args[] 参数中指定的值,这些值将用于指定要删除的托管属性的名称。
string strName = args[0];
若要对 SSP 的搜索上下文检索 Schema 对象,请添加以下代码。有关搜索上下文的检索方法的详细信息,请参阅如何:返回搜索服务提供程序的搜索上下文。
/* Replace <SiteName> with the name of a site using the SSP */ string strURL = "http://<SiteName>"; Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
使用以下代码检索托管属性的集合。
ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
确定托管属性的集合中是否包括指定属性。
if (properties.Contains(strName)) { Console.WriteLine(strName + " property does not exist. Delete failed."); return; }
如果 Contains 方法返回 true,则在托管属性中循环,并对每个属性查看 Name 属性是否与 strName 的值相符。
foreach (ManagedProperty property in properties) { if (property.Name == strName) {
接下来,通过检查 DeleteDisallowed 属性的值,确认可以删除该属性。
if (property.DeleteDisallowed) { Console.WriteLine("DeleteDisallowed enabled for " + strName + ". Delete failed."); return; }
如果 DeleteDisallowed 属性返回 false,则调用 DeleteAllMappings 方法,然后对该属性调用 Delete 方法。
property.DeleteAllMappings(); property.Delete(); Console.WriteLine(strName + " deleted."); return; } }
示例
下面是控制台应用程序类示例的完整代码。
先决条件
- 确保已创建了共享服务提供程序。
项目引用
运行本示例之前,在控制台应用程序代码项目中添加以下项目引用:
Microsoft.SharePoint
Microsoft.Office.Server
Microsoft.Office.Server.Search
using System;
using System.Collections;
using System.Text;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
namespace DeleteManagedPropertiesSample
{
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length != 1)
{
Usage();
return;
}
string strName = args[0];
/*
Replace <SiteName> with the name of a site using the SSP
*/
string strURL = "http://<SiteName>";
Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
if (!properties.Contains(strName))
{
Console.WriteLine(strName + " property does not exist. Delete failed.");
return;
}
foreach (ManagedProperty property in properties)
{
if (property.Name == strName)
{
if (property.DeleteDisallowed)
{
Console.WriteLine("DeleteDisallowed enabled for " + strName + ". Delete failed.");
return;
}
property.DeleteAllMappings();
property.Delete();
Console.WriteLine(strName + " deleted.");
return;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void Usage()
{
Console.WriteLine("Delete Managed Properties Sample");
Console.WriteLine("Usage: DeleteManagedPropertiesSample.exe PropertyName");
}
}
}