Example #1
0
        private SPManagedPropertyInfo GetManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;

            // Get the search schema
            var sspSchema         = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                managedPropertyDefinition = ssa.GetManagedProperty(propertyName, owner);
            }
            else
            {
                this.logger.Warn("Managed Property '{0}' not found.", propertyName);
            }

            return(managedPropertyDefinition);
        }
Example #2
0
        private static List <MappingInfo> GetInitialCrawledPropertyMappings(
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            var mappingCollection = new List <MappingInfo>();

            // If specified to overwrite all crawled property mappings
            // set an empty mapping info list before recreating the mappings.
            // Else if, if specified to append the crawled properties, initialize the
            // mapping collection to the existing mappings on the managed property.
            switch (managedPropertyInfo.UpdateBehavior)
            {
            case ManagedPropertyUpdateBehavior.OverwriteCrawledProperties:
            case ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists:
                ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappingCollection, owner);
                break;

            case ManagedPropertyUpdateBehavior.AppendCrawledProperties:
                mappingCollection = ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner);
                break;
            }

            return(mappingCollection);
        }
Example #3
0
        private void SetCrawledPropertyMappings(
            SPSite site,
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner,
            List <MappingInfo> mappings)
        {
            // Ensure crawl properties mappings
            foreach (var crawledPropertyKeyAndOrder in managedPropertyInfo.CrawledProperties)
            {
                // Get the crawled property (there may be more than one matching that name)
                var matchingCrawledProperties = this.GetCrawledPropertyByName(site, crawledPropertyKeyAndOrder.Key);
                if (matchingCrawledProperties != null && matchingCrawledProperties.Count > 0)
                {
                    foreach (var crawledProperty in matchingCrawledProperties)
                    {
                        // Create mapping information
                        var mapping = new MappingInfo
                        {
                            CrawledPropertyName = crawledProperty.Name,
                            CrawledPropset      = crawledProperty.Propset,
                            ManagedPid          = managedPropertyDefinition.Pid,
                            MappingOrder        = crawledPropertyKeyAndOrder.Value
                        };

                        // If managed property doesn't already contain a mapping for the crawled property, add it
                        if (
                            ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner)
                            .All(m => m.CrawledPropertyName != mapping.CrawledPropertyName))
                        {
                            mappings.Add(mapping);
                        }
                        else
                        {
                            this.logger.Info(
                                "Mapping for managed property {0} and crawled property with name {1} is already exists",
                                managedPropertyDefinition.Name,
                                crawledPropertyKeyAndOrder);
                        }
                    }
                }
                else
                {
                    this.logger.Warn("Crawled property with name {0} not found!", crawledPropertyKeyAndOrder);
                }
            }

            // Apply mappings to the managed property
            if (mappings.Count > 0)
            {
                ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappings, owner);
            }
        }
Example #4
0
        public void EnsureManagedProperty_WhenNoChangesWanted_ShouldCreateManagedPropertyIfDoesntExist()
        {
            const string ManagedPropertyName = "TestManagedProperty";

            using (var testScope = SiteTestScope.BlankSite())
            {
                // Arrange
                var owner = new SearchObjectOwner(SearchObjectLevel.Ssa, testScope.SiteCollection.RootWeb);
                var managedPropertyInfo = new ManagedPropertyInfo(ManagedPropertyName, ManagedDataType.Text)
                {
                    Sortable          = true,
                    Refinable         = true,
                    Queryable         = true,
                    CrawledProperties = new Dictionary <string, int>()
                    {
                        { "ows_Title", 1 }
                    },
                    UpdateBehavior = ManagedPropertyUpdateBehavior.NoChangesIfAlreadyExists
                };

                using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope())
                {
                    var searchHelper = injectionScope.Resolve <ISearchHelper>();
                    var ssa          = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection);

                    try
                    {
                        // Act
                        var actualManagedProperty          = searchHelper.EnsureManagedProperty(testScope.SiteCollection, managedPropertyInfo);
                        var refetchedActualManagedProperty = ssa.GetManagedProperty(ManagedPropertyName, owner);

                        // Assert
                        Assert.IsNotNull(actualManagedProperty);
                        Assert.AreEqual(true, actualManagedProperty.Sortable);
                        Assert.AreEqual(true, actualManagedProperty.Refinable);
                        Assert.AreEqual(true, actualManagedProperty.Queryable);
                        Assert.IsTrue(actualManagedProperty.GetMappedCrawledProperties(2).Count == 1);
                        Assert.IsTrue(actualManagedProperty.GetMappedCrawledProperties(2)[0].Name == "ows_Title");

                        Assert.IsNotNull(refetchedActualManagedProperty);
                        Assert.AreEqual(true, refetchedActualManagedProperty.Sortable);
                        Assert.AreEqual(true, refetchedActualManagedProperty.Refinable);
                        Assert.AreEqual(true, refetchedActualManagedProperty.Queryable);
                        Assert.IsTrue(ssa.GetManagedPropertyMappings(refetchedActualManagedProperty, owner).Count == 1);
                        Assert.IsTrue(ssa.GetManagedPropertyMappings(refetchedActualManagedProperty, owner)[0].CrawledPropertyName == "ows_Title");
                    }
                    finally
                    {
                        // Clean up
                        searchHelper.DeleteManagedProperty(testScope.SiteCollection, managedPropertyInfo);
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Delete a managed property from the search schema
        /// </summary>
        /// <param name="site">The context site</param>
        /// <param name="managedPropertyInfo">The managed property info</param>
        public void DeleteManagedProperty(SPSite site, ManagedPropertyInfo managedPropertyInfo)
        {
            var ssa = this.GetDefaultSearchServiceApplication(site);

            // Get the search schema
            var sspSchema         = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            if (managedProperties.Contains(managedPropertyInfo.Name))
            {
                var prop = managedProperties[managedPropertyInfo.Name];
                prop.DeleteAllMappings();
                prop.Delete();
            }
        }
Example #6
0
        private void ConfigureManagerProperty(
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo)
        {
            if ((managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.UpdateConfiguration) ||
                (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists))
            {
                this.logger.Info(
                    "Configuring managed property '{0}' on update behavior '{1}'",
                    managedPropertyInfo.Name,
                    managedPropertyInfo.UpdateBehavior);

                // Configure the managed property
                managedPropertyDefinition.Sortable = managedPropertyInfo.Sortable;
                if (managedPropertyDefinition.Sortable)
                {
                    managedPropertyDefinition.SortableType = managedPropertyInfo.SortableType;
                }

                managedPropertyDefinition.Refinable = managedPropertyInfo.Refinable;
                if (managedPropertyDefinition.Refinable)
                {
                    // use "active" refine mode whenever refinable=TRUE
                    managedPropertyDefinition.RefinerConfiguration.Type =
                        Microsoft.Office.Server.Search.Administration.RefinerType.Deep;
                }

                managedPropertyDefinition.Retrievable     = managedPropertyInfo.Retrievable;
                managedPropertyDefinition.RespectPriority = managedPropertyInfo.RespectPriority;
                managedPropertyDefinition.Queryable       = managedPropertyInfo.Queryable;
                managedPropertyDefinition.Searchable      = managedPropertyInfo.Searchable;

                if (managedPropertyDefinition.Searchable)
                {
                    managedPropertyDefinition.FullTextIndex = managedPropertyInfo.FullTextIndex;
                    managedPropertyDefinition.Context       = managedPropertyInfo.Context;
                }
                else
                {
                    managedPropertyDefinition.FullTextIndex = string.Empty;
                    managedPropertyDefinition.Context       = 0;
                }

                managedPropertyDefinition.HasMultipleValues = managedPropertyInfo.HasMultipleValues;
                managedPropertyDefinition.SafeForAnonymous  = managedPropertyInfo.SafeForAnonymous;
            }
        }
Example #7
0
        /// <summary>
        /// Ensure a managed property in the search service application schema
        /// </summary>
        /// <param name="site">The context site</param>
        /// <param name="managedPropertyInfo">The managed property info</param>
        /// <returns>The managed property</returns>
        public ManagedProperty EnsureManagedProperty(SPSite site, ManagedPropertyInfo managedPropertyInfo)
        {
            var ssa          = this.GetDefaultSearchServiceApplication(site);
            var propertyName = managedPropertyInfo.Name;

            // this forces managed prop definition to SSA-scope
            // (i.e. all managed props will be farm-wide)
            var owner = new SearchObjectOwner(SearchObjectLevel.Ssa, site.RootWeb);

            // Get the managed property and if null, create it
            var managedPropertyDefinition = this.GetManagedProperty(managedPropertyInfo, ssa, owner);

            if (managedPropertyDefinition == null)
            {
                // If managed property was created, make sure it sets the crawled property mappings and configuration
                managedPropertyInfo.UpdateBehavior = ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists;
                managedPropertyDefinition          = this.CreateManagedProperty(managedPropertyInfo, ssa, owner);
            }

            if (managedPropertyDefinition != null)
            {
                // If crawled property mappings need to be overwritten or appended
                if (ShouldUpdateCrawledPropertyMappings(managedPropertyInfo))
                {
                    var mappings = GetInitialCrawledPropertyMappings(managedPropertyDefinition, managedPropertyInfo, ssa, owner);
                    this.SetCrawledPropertyMappings(site, managedPropertyDefinition, managedPropertyInfo, ssa, owner, mappings);
                }

                if (ShouldUpdateConfiguration(managedPropertyInfo))
                {
                    this.ConfigureManagerProperty(managedPropertyDefinition, managedPropertyInfo);
                }

                // Save through the schema manager (don't call .Update on the managed property object itself, its config won't get saved properly)
                ssa.UpdateManagedProperty(managedPropertyDefinition, owner);
            }

            // Re-fetch schema, it might be stale at this point
            var sspSchema = new Schema(ssa);

            return(sspSchema.AllManagedProperties[propertyName]);
        }
Example #8
0
        private SPManagedPropertyInfo CreateManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;
            var propertyType = managedPropertyInfo.DataType;

            // Get the search schema
            var sspSchema         = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                var prop = managedProperties[propertyName];
                if (prop.DeleteDisallowed)
                {
                    this.logger.Warn("Delete is disallowed on the Managed Property {0}", propertyName);
                }
                else
                {
                    prop.DeleteAllMappings();
                    prop.Delete();
                    managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
                }
            }
            else
            {
                managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
            }

            return(managedPropertyDefinition);
        }
Example #9
0
        private void ConfigureManagerProperty(
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo)
        {
            if ((managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.UpdateConfiguration) || 
                (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists))
            {
                this.logger.Info(
                    "Configuring managed property '{0}' on update behavior '{1}'", 
                    managedPropertyInfo.Name, 
                    managedPropertyInfo.UpdateBehavior);

                // Configure the managed property
                managedPropertyDefinition.Sortable = managedPropertyInfo.Sortable;
                if (managedPropertyDefinition.Sortable)
                {
                    managedPropertyDefinition.SortableType = managedPropertyInfo.SortableType;
                }

                managedPropertyDefinition.Refinable = managedPropertyInfo.Refinable;
                if (managedPropertyDefinition.Refinable)
                {
                    // use "active" refine mode whenever refinable=TRUE
                    managedPropertyDefinition.RefinerConfiguration.Type =
                        Microsoft.Office.Server.Search.Administration.RefinerType.Deep;
                }

                managedPropertyDefinition.Retrievable = managedPropertyInfo.Retrievable;
                managedPropertyDefinition.RespectPriority = managedPropertyInfo.RespectPriority;
                managedPropertyDefinition.Queryable = managedPropertyInfo.Queryable;
                managedPropertyDefinition.Searchable = managedPropertyInfo.Searchable;

                if (managedPropertyDefinition.Searchable)
                {
                    managedPropertyDefinition.FullTextIndex = managedPropertyInfo.FullTextIndex;
                    managedPropertyDefinition.Context = managedPropertyInfo.Context;
                }
                else
                {
                    managedPropertyDefinition.FullTextIndex = string.Empty;
                    managedPropertyDefinition.Context = 0;
                }

                managedPropertyDefinition.HasMultipleValues = managedPropertyInfo.HasMultipleValues;
                managedPropertyDefinition.SafeForAnonymous = managedPropertyInfo.SafeForAnonymous;
            }
        }
Example #10
0
        private void SetCrawledPropertyMappings(
            SPSite site,
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner,
            List<MappingInfo> mappings)
        {
            // Ensure crawl properties mappings
            foreach (var crawledPropertyKeyAndOrder in managedPropertyInfo.CrawledProperties)
            {
                // Get the crawled property (there may be more than one matching that name)
                var matchingCrawledProperties = this.GetCrawledPropertyByName(site, crawledPropertyKeyAndOrder.Key);
                if (matchingCrawledProperties != null && matchingCrawledProperties.Count > 0)
                {
                    foreach (var crawledProperty in matchingCrawledProperties)
                    {
                        // Create mapping information
                        var mapping = new MappingInfo
                        {
                            CrawledPropertyName = crawledProperty.Name,
                            CrawledPropset = crawledProperty.Propset,
                            ManagedPid = managedPropertyDefinition.Pid,
                            MappingOrder = crawledPropertyKeyAndOrder.Value
                        };

                        // If managed property doesn't already contain a mapping for the crawled property, add it
                        if (
                            ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner)
                                .All(m => m.CrawledPropertyName != mapping.CrawledPropertyName))
                        {
                            mappings.Add(mapping);
                        }
                        else
                        {
                            this.logger.Info(
                                "Mapping for managed property {0} and crawled property with name {1} is already exists",
                                managedPropertyDefinition.Name,
                                crawledPropertyKeyAndOrder);
                        }
                    }
                }
                else
                {
                    this.logger.Warn("Crawled property with name {0} not found!", crawledPropertyKeyAndOrder);
                }
            }

            // Apply mappings to the managed property
            if (mappings.Count > 0)
            {
                ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappings, owner);
            }
        }
Example #11
0
        private SPManagedPropertyInfo CreateManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;
            var propertyType = managedPropertyInfo.DataType;

            // Get the search schema
            var sspSchema = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                var prop = managedProperties[propertyName];
                if (prop.DeleteDisallowed)
                {
                    this.logger.Warn("Delete is disallowed on the Managed Property {0}", propertyName);
                }
                else
                {
                    prop.DeleteAllMappings();
                    prop.Delete();
                    managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
                }
            }
            else
            {
                managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
            }

            return managedPropertyDefinition;
        }
Example #12
0
        private SPManagedPropertyInfo GetManagedProperty(
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;
            var propertyName = managedPropertyInfo.Name;

            // Get the search schema
            var sspSchema = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            // If the managed property already exists
            // Else create it.
            if (managedProperties.Contains(propertyName))
            {
                managedPropertyDefinition = ssa.GetManagedProperty(propertyName, owner);
            }
            else
            {
                this.logger.Warn("Managed Property '{0}' not found.", propertyName);
            }

            return managedPropertyDefinition;
        }
Example #13
0
        /// <summary>
        /// Delete a managed property from the search schema
        /// </summary>
        /// <param name="site">The context site</param>
        /// <param name="managedPropertyInfo">The managed property info</param>
        public void DeleteManagedProperty(SPSite site, ManagedPropertyInfo managedPropertyInfo)
        {
            var ssa = this.GetDefaultSearchServiceApplication(site);

            // Get the search schema
            var sspSchema = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            if (managedProperties.Contains(managedPropertyInfo.Name))
            {
                var prop = managedProperties[managedPropertyInfo.Name];
                prop.DeleteAllMappings();
                prop.Delete();
            }
        }
Example #14
0
        /// <summary>
        /// Ensure a managed property in the search service application schema
        /// </summary>
        /// <param name="site">The context site</param>
        /// <param name="managedPropertyInfo">The managed property info</param>
        /// <returns>The managed property</returns>
        public ManagedProperty EnsureManagedProperty(SPSite site, ManagedPropertyInfo managedPropertyInfo)
        {
            var ssa = this.GetDefaultSearchServiceApplication(site);
            var propertyName = managedPropertyInfo.Name;

            // this forces managed prop definition to SSA-scope 
            // (i.e. all managed props will be farm-wide)
            var owner = new SearchObjectOwner(SearchObjectLevel.Ssa, site.RootWeb);

            // Get the managed property and if null, create it
            var managedPropertyDefinition = this.GetManagedProperty(managedPropertyInfo, ssa, owner);
            if ((managedPropertyDefinition == null) || ShouldRecreateManagedProperty(managedPropertyDefinition, managedPropertyInfo))
            {
                // If managed property was created, make sure it sets the crawled property mappings and configuration
                managedPropertyInfo.UpdateBehavior = ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists;
                managedPropertyDefinition = this.CreateManagedProperty(managedPropertyInfo, ssa, owner);
            }

            if (managedPropertyDefinition != null)
            {
                // If crawled property mappings need to be overwritten or appended
                if (ShouldUpdateCrawledPropertyMappings(managedPropertyInfo))
                {
                    var mappings = GetInitialCrawledPropertyMappings(managedPropertyDefinition, managedPropertyInfo, ssa, owner);
                    this.SetCrawledPropertyMappings(site, managedPropertyDefinition, managedPropertyInfo, ssa, owner, mappings);
                }

                if (ShouldUpdateConfiguration(managedPropertyInfo))
                {
                    this.ConfigureManagerProperty(managedPropertyDefinition, managedPropertyInfo);
                }

                // Save through the schema manager (don't call .Update on the managed property object itself, its config won't get saved properly)
                ssa.UpdateManagedProperty(managedPropertyDefinition, owner);
            }

            // Re-fetch schema, it might be stale at this point
            var sspSchema = new Schema(ssa);
            return sspSchema.AllManagedProperties[propertyName];
        }
Example #15
0
 /// <summary>
 /// Initializes a new result type rule
 /// </summary>
 /// <param name="property">Managed property metadata</param>
 /// <param name="propertyOperator">The operator</param>
 /// <param name="values">The associated values</param>
 public ResultTypeRuleInfo(ManagedPropertyInfo property, PropertyRuleOperator.DefaultOperator propertyOperator, ICollection<string> values)
 {
     this.PropertyName = property.Name;
     this.Operator = propertyOperator;
     this.Values = values;
 }
Example #16
0
        /// <summary>
        /// Ensure a managed property in the search service application schema
        /// </summary>
        /// <param name="site">The context site</param>
        /// <param name="managedPropertyInfo">The managed property info</param>
        /// <returns>The managed property</returns>
        public ManagedProperty EnsureManagedProperty(SPSite site, ManagedPropertyInfo managedPropertyInfo)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;

            var ssa = this.GetDefaultSearchServiceApplication(site);
            var propertyName = managedPropertyInfo.Name;
            var propertyType = managedPropertyInfo.DataType;
            var owner = new SearchObjectOwner(SearchObjectLevel.Ssa, site.RootWeb);     // this forces managed prop definition to SSA-scope
                                                                                        // (i.e. all managed props will be farm-wide)

            // Get the search schema
            var sspSchema = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            if (managedProperties.Contains(propertyName))
            {
                var prop = managedProperties[propertyName];
                if (managedPropertyInfo.OverwriteIfAlreadyExists)
                {
                    if (prop.DeleteDisallowed)
                    {
                        this.logger.Warn("Delete is disallowed on the Managed Property {0}", propertyName);
                    }
                    else
                    {
                        prop.DeleteAllMappings();
                        prop.Delete();
                        managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
                    }
                }
            }
            else
            {
                managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
            }

            if (managedPropertyDefinition != null)
            {
                var mappingCollection = new List<MappingInfo>(); // new MappingCollection();

                // Ensure crawl properties mappings
                foreach (KeyValuePair<string, int> crawledPropertyKeyAndOrder in managedPropertyInfo.CrawledProperties)
                {
                    // Get the crawled property (there may be more than one matching that name)
                    var matchingCrawledProperties = this.GetCrawledPropertyByName(site, crawledPropertyKeyAndOrder.Key);

                    if (matchingCrawledProperties != null && matchingCrawledProperties.Count > 0)
                    {
                        foreach (var cp in matchingCrawledProperties)
                        {
                            // Create mapping information
                            var mapping = new MappingInfo
                            {
                                CrawledPropertyName = cp.Name,
                                CrawledPropset = cp.Propset,
                                ManagedPid = managedPropertyDefinition.Pid,
                                MappingOrder = crawledPropertyKeyAndOrder.Value
                            };

                            if (!ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner).Any(m => m.CrawledPropertyName == mapping.CrawledPropertyName))
                            {
                                mappingCollection.Add(mapping);
                            }
                            else
                            {
                                this.logger.Info("Mapping for managed property {0} and crawled property with name {1} is already exists", managedPropertyDefinition.Name, crawledPropertyKeyAndOrder);
                            }
                        }
                    }
                    else
                    {
                        this.logger.Warn("Crawled property with name {0} not found!", crawledPropertyKeyAndOrder);
                    }
                }

                // Apply mappings to the managed property
                if (mappingCollection.Count > 0)
                {
                    ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappingCollection, owner);
                }

                // Configure the managed property
                managedPropertyDefinition.Sortable = managedPropertyInfo.Sortable;
                if (managedPropertyDefinition.Sortable)
                {
                    managedPropertyDefinition.SortableType = managedPropertyInfo.SortableType;
                }

                managedPropertyDefinition.Refinable = managedPropertyInfo.Refinable;
                if (managedPropertyDefinition.Refinable)
                {
                    // use "active" refine mode whenever refinable=TRUE
                    managedPropertyDefinition.RefinerConfiguration.Type = Microsoft.Office.Server.Search.Administration.RefinerType.Deep;
                }

                managedPropertyDefinition.Retrievable = managedPropertyInfo.Retrievable;
                managedPropertyDefinition.RespectPriority = managedPropertyInfo.RespectPriority;
                managedPropertyDefinition.Queryable = managedPropertyInfo.Queryable;
                managedPropertyDefinition.Searchable = managedPropertyInfo.Searchable;

                if (managedPropertyDefinition.Searchable)
                {
                    managedPropertyDefinition.FullTextIndex = managedPropertyInfo.FullTextIndex;
                    managedPropertyDefinition.Context = managedPropertyInfo.Context;
                }
                else
                {
                    managedPropertyDefinition.FullTextIndex = string.Empty;
                    managedPropertyDefinition.Context = (short)0;
                }

                managedPropertyDefinition.HasMultipleValues = managedPropertyInfo.HasMultipleValues;
                managedPropertyDefinition.SafeForAnonymous = managedPropertyInfo.SafeForAnonymous;

                // Save through the schema manager (don't call .Update on the managed property object itself, its config won't get saved properly)
                ssa.UpdateManagedProperty(managedPropertyDefinition, owner);
            }

            // Re-fetch schema, it might be stale at this point
            sspSchema = new Schema(ssa);

            return sspSchema.AllManagedProperties[propertyName];
        }
Example #17
0
 private static bool ShouldUpdateCrawledPropertyMappings(ManagedPropertyInfo managedPropertyInfo)
 {
     return (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteCrawledProperties)
            || (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.AppendCrawledProperties)
            || (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists);
 }
Example #18
0
        private static List<MappingInfo> GetInitialCrawledPropertyMappings(
            SPManagedPropertyInfo managedPropertyDefinition,
            ManagedPropertyInfo managedPropertyInfo,
            SearchServiceApplication ssa,
            SearchObjectOwner owner)
        {
            var mappingCollection = new List<MappingInfo>();

            // If specified to overwrite all crawled property mappings
            // set an empty mapping info list before recreating the mappings.
            // Else if, if specified to append the crawled properties, initialize the
            // mapping collection to the existing mappings on the managed property.
            switch (managedPropertyInfo.UpdateBehavior)
            {
                case ManagedPropertyUpdateBehavior.OverwriteCrawledProperties:
                case ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists:
                    ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappingCollection, owner);
                    break;
                case ManagedPropertyUpdateBehavior.AppendCrawledProperties:
                    mappingCollection = ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner);
                    break;
            }

            return mappingCollection;
        }
Example #19
0
 private static bool ShouldRecreateManagedProperty(SPManagedPropertyInfo managedPropertyDefinition, ManagedPropertyInfo managedPropertyInfo)
 {
     // If the managed type has changed, the managed property needs to be recreated.
     // NOTE: Simply changing the 'ManagedType' property doesn't work EVEN IF IT'S NOT READ-ONLY.
     return(managedPropertyDefinition.ManagedType != managedPropertyInfo.DataType);
 }
Example #20
0
        /// <summary>
        /// Ensure a managed property in the search service application schema
        /// </summary>
        /// <param name="site">The context site</param>
        /// <param name="managedPropertyInfo">The managed property info</param>
        /// <returns>The managed property</returns>
        public ManagedProperty EnsureManagedProperty(SPSite site, ManagedPropertyInfo managedPropertyInfo)
        {
            SPManagedPropertyInfo managedPropertyDefinition = null;

            var ssa          = this.GetDefaultSearchServiceApplication(site);
            var propertyName = managedPropertyInfo.Name;
            var propertyType = managedPropertyInfo.DataType;
            var owner        = new SearchObjectOwner(SearchObjectLevel.Ssa, site.RootWeb); // this forces managed prop definition to SSA-scope
                                                                                           // (i.e. all managed props will be farm-wide)

            // Get the search schema
            var sspSchema         = new Schema(ssa);
            var managedProperties = sspSchema.AllManagedProperties;

            if (managedProperties.Contains(propertyName))
            {
                var prop = managedProperties[propertyName];
                if (managedPropertyInfo.OverwriteIfAlreadyExists)
                {
                    if (prop.DeleteDisallowed)
                    {
                        this.logger.Warn("Delete is disallowed on the Managed Property {0}", propertyName);
                    }
                    else
                    {
                        prop.DeleteAllMappings();
                        prop.Delete();
                        managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
                    }
                }
            }
            else
            {
                managedPropertyDefinition = ssa.CreateManagedProperty(propertyName, propertyType, owner);
            }

            if (managedPropertyDefinition != null)
            {
                var mappingCollection = new List <MappingInfo>(); // new MappingCollection();

                // Ensure crawl properties mappings
                foreach (KeyValuePair <string, int> crawledPropertyKeyAndOrder in managedPropertyInfo.CrawledProperties)
                {
                    // Get the crawled property (there may be more than one matching that name)
                    var matchingCrawledProperties = this.GetCrawledPropertyByName(site, crawledPropertyKeyAndOrder.Key);

                    if (matchingCrawledProperties != null && matchingCrawledProperties.Count > 0)
                    {
                        foreach (var cp in matchingCrawledProperties)
                        {
                            // Create mapping information
                            var mapping = new MappingInfo
                            {
                                CrawledPropertyName = cp.Name,
                                CrawledPropset      = cp.Propset,
                                ManagedPid          = managedPropertyDefinition.Pid,
                                MappingOrder        = crawledPropertyKeyAndOrder.Value
                            };

                            if (!ssa.GetManagedPropertyMappings(managedPropertyDefinition, owner).Any(m => m.CrawledPropertyName == mapping.CrawledPropertyName))
                            {
                                mappingCollection.Add(mapping);
                            }
                            else
                            {
                                this.logger.Info("Mapping for managed property {0} and crawled property with name {1} is already exists", managedPropertyDefinition.Name, crawledPropertyKeyAndOrder);
                            }
                        }
                    }
                    else
                    {
                        this.logger.Warn("Crawled property with name {0} not found!", crawledPropertyKeyAndOrder);
                    }
                }

                // Apply mappings to the managed property
                if (mappingCollection.Count > 0)
                {
                    ssa.SetManagedPropertyMappings(managedPropertyDefinition, mappingCollection, owner);
                }

                // Configure the managed property
                managedPropertyDefinition.Sortable = managedPropertyInfo.Sortable;
                if (managedPropertyDefinition.Sortable)
                {
                    managedPropertyDefinition.SortableType = managedPropertyInfo.SortableType;
                }

                managedPropertyDefinition.Refinable = managedPropertyInfo.Refinable;
                if (managedPropertyDefinition.Refinable)
                {
                    // use "active" refine mode whenever refinable=TRUE
                    managedPropertyDefinition.RefinerConfiguration.Type = Microsoft.Office.Server.Search.Administration.RefinerType.Deep;
                }

                managedPropertyDefinition.Retrievable     = managedPropertyInfo.Retrievable;
                managedPropertyDefinition.RespectPriority = managedPropertyInfo.RespectPriority;
                managedPropertyDefinition.Queryable       = managedPropertyInfo.Queryable;
                managedPropertyDefinition.Searchable      = managedPropertyInfo.Searchable;

                if (managedPropertyDefinition.Searchable)
                {
                    managedPropertyDefinition.FullTextIndex = managedPropertyInfo.FullTextIndex;
                    managedPropertyDefinition.Context       = managedPropertyInfo.Context;
                }
                else
                {
                    managedPropertyDefinition.FullTextIndex = string.Empty;
                    managedPropertyDefinition.Context       = (short)0;
                }

                managedPropertyDefinition.HasMultipleValues = managedPropertyInfo.HasMultipleValues;
                managedPropertyDefinition.SafeForAnonymous  = managedPropertyInfo.SafeForAnonymous;

                // Save through the schema manager (don't call .Update on the managed property object itself, its config won't get saved properly)
                ssa.UpdateManagedProperty(managedPropertyDefinition, owner);
            }

            // Re-fetch schema, it might be stale at this point
            sspSchema = new Schema(ssa);

            return(sspSchema.AllManagedProperties[propertyName]);
        }
Example #21
0
 private static bool ShouldUpdateConfiguration(ManagedPropertyInfo managedPropertyInfo)
 {
     return (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.UpdateConfiguration) ||
            (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists);
 }
Example #22
0
 private static bool ShouldUpdateConfiguration(ManagedPropertyInfo managedPropertyInfo)
 {
     return((managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.UpdateConfiguration) ||
            (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists));
 }
Example #23
0
 private static bool ShouldRecreateManagedProperty(SPManagedPropertyInfo managedPropertyDefinition, ManagedPropertyInfo managedPropertyInfo)
 {
     // If the managed type has changed, the managed property needs to be recreated.
     // NOTE: Simply changing the 'ManagedType' property doesn't work EVEN IF IT'S NOT READ-ONLY.
     return managedPropertyDefinition.ManagedType != managedPropertyInfo.DataType;
 }
Example #24
0
 private static bool ShouldUpdateCrawledPropertyMappings(ManagedPropertyInfo managedPropertyInfo)
 {
     return((managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteCrawledProperties) ||
            (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.AppendCrawledProperties) ||
            (managedPropertyInfo.UpdateBehavior == ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists));
 }
Example #25
0
        public void EnsureManagedProperty_WhenUpdatingConfiguration_ShouldChangeConfigurationAndKeepCrawledPropertyMappings()
        {
            const string ManagedPropertyName = "TestManagedProperty";

            using (var testScope = SiteTestScope.BlankSite())
            {
                // Arrange
                var owner = new SearchObjectOwner(SearchObjectLevel.Ssa, testScope.SiteCollection.RootWeb);
                var managedPropertyInfo = new ManagedPropertyInfo(ManagedPropertyName, ManagedDataType.Text)
                {
                    Sortable = true,
                    Refinable = true,
                    Queryable = true,
                    CrawledProperties = new Dictionary<string, int>()
                    {
                        { "ows_Title", 1 }
                    },
                    UpdateBehavior = ManagedPropertyUpdateBehavior.OverwriteIfAlreadyExists
                };

                var updatedManagedPropertyInfo = new ManagedPropertyInfo(ManagedPropertyName, ManagedDataType.Text)
                {
                    Sortable = false,
                    Refinable = false,
                    Queryable = false,
                    CrawledProperties = new Dictionary<string, int>()
                    {
                        { "ows_Description", 2 }
                    },
                    UpdateBehavior = ManagedPropertyUpdateBehavior.UpdateConfiguration
                };

                using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope())
                {
                    var searchHelper = injectionScope.Resolve<ISearchHelper>();
                    var ssa = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection);

                    try
                    {
                        // Act
                        searchHelper.EnsureManagedProperty(testScope.SiteCollection, managedPropertyInfo);
                        var actualManagedProperty = searchHelper.EnsureManagedProperty(testScope.SiteCollection, updatedManagedPropertyInfo);
                        var refetchedActualManagedProperty = ssa.GetManagedProperty(ManagedPropertyName, owner);

                        // Assert
                        Assert.IsNotNull(actualManagedProperty);
                        Assert.AreEqual(false, actualManagedProperty.Sortable);
                        Assert.AreEqual(false, actualManagedProperty.Refinable);
                        Assert.AreEqual(false, actualManagedProperty.Queryable);
                        Assert.IsTrue(actualManagedProperty.GetMappedCrawledProperties(2).Count == 1);
                        Assert.IsTrue(actualManagedProperty.GetMappedCrawledProperties(2)[0].Name == "ows_Title");

                        Assert.IsNotNull(refetchedActualManagedProperty);
                        Assert.AreEqual(false, refetchedActualManagedProperty.Sortable);
                        Assert.AreEqual(false, refetchedActualManagedProperty.Refinable);
                        Assert.AreEqual(false, refetchedActualManagedProperty.Queryable);
                        Assert.IsTrue(ssa.GetManagedPropertyMappings(refetchedActualManagedProperty, owner).Count == 1);
                        Assert.IsTrue(ssa.GetManagedPropertyMappings(refetchedActualManagedProperty, owner)[0].CrawledPropertyName == "ows_Title");
                    }
                    finally
                    {
                        // Clean up
                        searchHelper.DeleteManagedProperty(testScope.SiteCollection, managedPropertyInfo);
                    }
                }
            }
        }
Example #26
0
 /// <summary>
 /// Initializes a new result type rule
 /// </summary>
 /// <param name="property">Managed property metadata</param>
 /// <param name="propertyOperator">The operator</param>
 /// <param name="values">The associated values</param>
 public ResultTypeRuleInfo(ManagedPropertyInfo property, PropertyRuleOperator.DefaultOperator propertyOperator, ICollection <string> values)
 {
     this.PropertyName = property.Name;
     this.Operator     = propertyOperator;
     this.Values       = values;
 }