Sometimes there may be a requirement, where we have exposed some metadata fields in a document library and we need those fields to be available when we use SharePoint search, and also in search results.
To achieve this, we need to carry out the following steps:
To achieve this, we need to carry out the following steps:
- Create content type with metadata fields (This step has already been covered in my previous post - http://www.directsharepoint.com/2011/12/create-custom-content-type-with-custom.html#links)
- Create managed properties for the metadata fields (for content type fields)
- Run full crawl on the server so that the properties become available for mapping.
- Map crawled properties to the Managed properties.
//Get the default shared service provider
ServerContext sc = ServerContext.GetContext(SPContext.Current.Site);
PropertyInfo srpProp = sc.GetType().GetProperty(
"SharedResourceProvider", BindingFlags.NonPublic | BindingFlags.Instance);
object srp = srpProp.GetValue(sc, null);
PropertyInfo srpNameProp = srp.GetType().GetProperty(
"Name", BindingFlags.Public | BindingFlags.Instance);
string sspName = (string)srpNameProp.GetValue(srp, null);
//Get search Schema
ServerContext serverContext = ServerContext.GetContext(sspName);
SearchContext searchContext = SearchContext.GetContext(serverContext);
Schema schema = new Schema(searchContext);
Once we have the ssp and search schema details, we read the content type fields and create managed properties for the metadata.
//Getting the content type object
SPContentType siteCT = web.ContentTypes["CustomDocLibCT"];
//Getting the content type's fields
CTFields = siteCT.Fields;
//Loop on the content type fields to find the internal names of the columns
FindInternalNamesOfMetadata();
//Putting values in an array
InternalNamesArray = InternalNames.Split(';');
//Looping on the array
for (int i = 0; i < InternalNamesArray.Length; i++)
{
string managedPropertyName = InternalNamesArray[i];
string PropertyPublished = string.Format("ows_{0}",InternalNamesArray[i]);
if(!schema.AllManagedProperties.Contains("MyCustom"+managedPropertyName))
{
ManagedProperty newManagedProperty = schema.AllManagedProperties.Create("MyCustom" + managedPropertyName, ManagedDataType.Text);
//Get crawled property to map in the Managed Property
CrawledProperty cprop = null;
foreach(CrawledProperty prop in schema.QueryCrawledProperties (PropertyPublished, 1000, Guid.NewGuid(), string.Empty, true))
{
if (prop.Name == PropertyPublished)
{
cprop = prop;
break;
}
}
if (cprop != null)
{
// Map the crawled prop to the Managed Prop
MappingCollection mappings = new MappingCollection();
mappings.Add(new Mapping(cprop.Propset, cprop.Name, cprop.VariantType, newManagedProperty.PID));
newManagedProperty.SetMappings(mappings);
// Set Some other properties
newManagedProperty.FullTextQueriable = true;
newManagedProperty.EnabledForScoping = true;
newManagedProperty.Update();
}
}
}
//Create new managed property - All Metadata & Assign all metadata field crawled properties to it
createAllMetadataProperty(schema, InternalNamesArray);
InternalNamesArray = null;
InternalNames = string.Empty;
The method createAllMetadataProperty is used to create a property that will search for all the metadata values collectively.
private void createAllMetadataProperty(Schema schema, string[] IntNamesArray)
{
if (schema.AllManagedProperties.Contains("MyCustomAllMetadata"))
{
schema.AllManagedProperties["MyCustomAllMetadata "].DeleteAllMappings();
schema.AllManagedProperties["MyCustomAllMetadata "].Delete();
}
string PropertyPublishedNew = string.Empty;
MappingCollection mappings = new MappingCollection();
//create managed property
ManagedProperty newManagedProperty = schema.AllManagedProperties.Create("MyCustomAllMetadata ", ManagedDataType.Text);
// Get the (crawled) property you want to map in the ManagedProperty
for (int i = 0; i < IntNamesArray.Length; i++)
{
PropertyPublishedNew = string.Format("ows_{0}", IntNamesArray[i]);
foreach (CrawledProperty cprop in schema.QueryCrawledProperties(PropertyPublishedNew, 1000, Guid.NewGuid(), string.Empty, true))
{
// Map the crawled properties to the Managed Prop
mappings.Add(new Mapping(cprop.Propset, cprop.Name, cprop.VariantType,newManagedProperty.PID));
}
}
newManagedProperty.SetMappings(mappings);
// Set Some other properties
newManagedProperty.FullTextQueriable = true;
newManagedProperty.EnabledForScoping = true;
newManagedProperty.Update();
IntNamesArray = null;
}
No comments:
Post a Comment