我正在进行高级搜索自定义。高级搜索页面有一个属性选择器,可以使用托管属性填充,我可以使用SharePoint界面公开托管属性。但是,我需要使用C#为高级搜索页面创建托管属性。如何以编程方式创建托管属性并将其添加到高级搜索属性中?你对此有什么想法吗?
谢谢。
答案 0 :(得分:2)
我解决了我的问题,首先,我用它的映射创建了托管属性。您可以通过此link访问解决方案。
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();
}
}
}
注意:别忘了!!创建新托管属性后启动完全爬网。