C# 使用C在SharePoint高级搜索页面中创建托管属性#
我正在进行高级搜索定制。高级搜索页面有一个属性选择器,可以用托管属性填充,我可以使用SharePoint界面公开托管属性。但是,我需要使用C#为高级搜索页面创建托管属性。如何以编程方式创建托管属性并将其添加到高级搜索属性中?你知道吗C# 使用C在SharePoint高级搜索页面中创建托管属性#,c#,sharepoint,sharepoint-2010,sharepoint-2007,C#,Sharepoint,Sharepoint 2010,Sharepoint 2007,我正在进行高级搜索定制。高级搜索页面有一个属性选择器,可以用托管属性填充,我可以使用SharePoint界面公开托管属性。但是,我需要使用C#为高级搜索页面创建托管属性。如何以编程方式创建托管属性并将其添加到高级搜索属性中?你知道吗 谢谢。我解决了我的问题,首先,我创建了托管属性及其映射。您可以从此访问解决方案 注意:别忘了!!创建新的托管属性后开始完全爬网 public void CreateManagedProperty() { // Get the
谢谢。我解决了我的问题,首先,我创建了托管属性及其映射。您可以从此访问解决方案 注意:别忘了!!创建新的托管属性后开始完全爬网
public void CreateManagedProperty()
{
// Get the default service context
SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);// Get the search service application proxy
var searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
// Get the search service application info object so we can find the Id of our Search Service App
if (searchProxy != null)
{
SearchServiceApplicationInfo ssai = searchProxy.GetSearchServiceApplicationInfo();
// Get the application itself
var application = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(ssai.SearchServiceApplicationId);
// Get the schema of our Search Service Application
var schema = new Schema(application);
// Get all the managed properties
ManagedPropertyCollection properties = schema.AllManagedProperties;
// Add a new property
ManagedProperty myProperty = properties.Create(Constants.ManagedPropertyName, ManagedDataType.Text);
myProperty.EnabledForScoping = true;
// Get the current mappings
MappingCollection mappings = myProperty.GetMappings();
// Add a new mapping to a previously crawled field
var myMapping = new Mapping(
new Guid(Constants.CrawledPropertyGuid), Constants.CrawledPropertyName, 31, myProperty.PID);
// Add the mapping
mappings.Add(myMapping);
// Update the collection of mappings
myProperty.SetMappings(mappings);
// Write the changes back
myProperty.Update();
}
}
public void AddAdvancedSearchProperty()
{
string sourcefile =
string.Format(
"{0}\\{1}", SPUtility.GetGenericSetupPath("TEMPLATE\\ADMIN\\ManagedProperties"), "NewAdvancedSearchProperty.xml");
// Load the xml file into XmlDocument object.
var xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(sourcefile);
}
catch (XmlException e)
{
Console.WriteLine(e.Message);
}
// Now create StringWriter object to get data from xml document.
var sw = new StringWriter();
var xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
string newXmlString = sw.ToString();
using (var sc = new SPSite("YOUR SITE"))
{
using (SPWeb web = sc.OpenWeb("searchcentre"))
{
SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("pages/advanced.aspx", PersonalizationScope.Shared);
foreach (var wp in mgr.WebParts)
{
if (wp is AdvancedSearchBox)
{
var asb = wp as AdvancedSearchBox;
asb.Properties = newXmlString;
mgr.SaveChanges(asb);
}
}
mgr.Web.Dispose();
}
}
}