Sunday, March 17, 2013

programmatically Mapping between Managed Properties and Crawled Properties in SharePoint 2013



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;

namespace ManagedAndCrawledPropertyMappingSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Guid cPropGUID = new Guid("00110329-0000-0110-c000-000000111146");  // // This GUID is for 'MyBookName'. we can get this GUID through programmatically
                string cPropName = "urn:schemas-microsoft-com:sharepoint:portal:profile: ‘MyBookName’";
                int vType = Convert.ToInt32(0); // //We can variant type through programmatically;              
  string mPropName = "MyBookName";
                string strCategoryName = "People";
                string strCraledPropName = string.Empty;

                string strURL = "http://<myServer>:14341";
                SearchContext context;
                using (SPSite site = new SPSite(strURL))
                {
                    context = SearchContext.GetContext(site);
                }
                Schema sspSchema = new Schema(context);

                // Get Crawled Properties
                CategoryCollection categories = sspSchema.AllCategories;
                foreach (Category category in categories)
                {
                    // Console.WriteLine(category.Name);
                    if (category.Name == strCategoryName)
                    {
                        foreach (CrawledProperty property in category.GetAllCrawledProperties())
                        {
                            strCraledPropName = property.Name;
                            Console.WriteLine("Propset: " + property.Propset);
                            Console.WriteLine("Name: " + property.Name);
                            Console.WriteLine("VariantType: " + property.VariantType);
                        }
                        Console.Read();
                    }
                }
                Console.Read();


                ManagedPropertyCollection properties = sspSchema.AllManagedProperties;

                bool isMngdPropertyExists = false;
                foreach (ManagedProperty mProperty in properties)
                {
                    if (mProperty.Name == mPropName)
                    {
                        isMngdPropertyExists = true;
                    }
                }

                if (!isMngdPropertyExists)
                {
                    ManagedProperty _mpNewProperty = properties.Create(mPropName, ManagedDataType.Text);
                    _mpNewProperty.Refinable = true;
                    _mpNewProperty.Sortable = true;
                    _mpNewProperty.SafeForAnonymous = true;
                    _mpNewProperty.Queryable = true;
                    _mpNewProperty.Update();
                }


                ManagedPropertyCollection props = sspSchema.AllManagedProperties;
                ManagedProperty mProp1 = properties["MyBookName"];
                foreach (CrawledProperty cProp in mProp1.GetMappedCrawledProperties(mProp1.GetMappings().Count))
                {
                    Console.WriteLine(cProp.Name);
                    Console.WriteLine(cProp.Propset);
                }
                Console.Read();


                //if (properties[mPropName] != null)
                //{
                //    ManagedProperty _mpNewProperty = properties.Create(mPropName, ManagedDataType.Text);
                //    _mpNewProperty.Update();
                //}
                //foreach(ManagedProperty mProp1 in properties)
                //{
                //    Console.WriteLine(mProp1.Name);
                //}
                //Console.Read();
                ManagedProperty mProp = properties[mPropName];
                //Mapping newMapping = new Mapping(cPropGUID, cPropName, vType, mProp.PID);//          
                MappingCollection mappings = mProp.GetMappings();
                foreach (Mapping mpg in mappings)
                {
                    Console.WriteLine("CrawledPropertyName: " + mpg.CrawledPropertyName);
                    Console.WriteLine("CrawledPropertyVariantType: " + mpg.CrawledPropertyVariantType);
                    Console.WriteLine("ManagedPid: " + mpg.ManagedPid);

                }
                Console.Read();

                if (mProp.DeleteDisallowed)
                {
                    mProp.DeleteAllMappings();
                    Console.WriteLine(mPropName + " Delete All Mappings.");
                    Console.Read();
                }

                //Mapping newMapping = new Mapping(cPropGUID, strCraledPropName, vType, mProp.PID);
                Mapping newMapping = new Mapping(cPropGUID, cPropName, vType, mProp.PID);
                if (mappings.Contains(newMapping))
                {
                    Console.WriteLine("Mapping failed: requested mapping already exists.");
                    Console.Read();
                    return;
                }
                mappings.Add(newMapping);
                mProp.SetMappings(mappings);
                Console.WriteLine(cPropName + " crawled property mapped to " + mProp.Name + " managed property.");
                Console.Read();
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
                Console.Read();
            }
        }
    }
}

Thanks...

2 comments:

Featured Post

Building Secure APIs with FastAPI and Azure AD Authentication

Building Secure APIs with FastAPI and Azure AD Authentication Published on September 2, 2025 In today's world of microservices and API-f...

Popular posts