Search code examples
c#sharepointsharepoint-2010sharepoint-2007

Creating Managed Properties in the SharePoint Advanced Search Page Using C#


enter image description here

I am working on Advanced Search customization. Advanced Search page has a property picker that can be populated with managed properties and I can expose managed properties with SharePoint interfaces. However, I need to create managed property for Advanced Search Page using C#. How can I programmatically create managed properties and add them into the Advanced Search Properties? Do you have any idea about that?

Thank you.


Solution

  • I solved my problem, Firstly, I created managed property with its mapping. You can access the solution from this 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();
            }
        }
    

    And then managed property was added to Advanced search Property;

            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();
                }
            }
        }
    

    Note: Don't Forget!! Start Full crawl after creating new managed property.