Search This Blog

Friday, January 3, 2014

How to programmatically update a content source in SharePoint 2010 enterprise / fast search

Following are the assembly references required to execute the code


using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.SharePoint.BusinessData.Parser;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.BusinessData.Infrastructure;
using Microsoft.BusinessData.Infrastructure;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.Office.Server.Search;
using Microsoft.SharePoint.Search.Extended.Administration;
 
Here is the function that take two parameters as first is your central application url and other parameter to define if you want to configure fast search or enterprise search. You can use the code individually also in case you dont want to configure both.


public void UpdateContentSource(string WebAppUrl, bool IsFast)
        {
            try
            {

                string strContentSourceName = "<CustomContentSourceName>";
                string strLogSystemName = "<BDCLogSystemName>";
                //Content Source Update Started
                //execute this code if you are updaameting a fast search content source. else user for enterprise content source
                if (IsFast)
                {
                    foreach (SearchServiceApplication FASTServiceApp in SearchService.Service.SearchApplications)
                    {
                        SearchServiceApplicationType at = FASTServiceApp.SearchApplicationType;
                        if (FASTServiceApp.SearchApplicationType == SearchServiceApplicationType.ExtendedConnector)
                        {
                            Content FastContent = new Content(FASTServiceApp);
                            bool CheckExist = false;
                            ContentSourceCollection FastCSColl = FastContent.ContentSources;
                            foreach (ContentSource cs in FastCSColl)
                            {
                                if (cs.Name.Equals(strContentSourceName))
                                {
                                    CheckExist = true;
                                    Console.WriteLine(strContentSourceName + " FAST Search Content Source already exist");

                                }
                            }
                            BusinessDataContentSource CustomConSource = null;
                            if (CheckExist)
                            {
                                CustomConSource = (BusinessDataContentSource)FastCSColl[strContentSourceName];
                                if (CustomConSource.StartAddresses.Count > 0)
                                {
                                    CustomConSource.StartAddresses.Remove(BusinessDataContentSource.ConstructStartAddress("Default", new Guid("00000000-0000-0000-0000-000000000000"), strLogSystemName, strLogSystemName));
                                }
                                CustomConSource.Delete();
                                FastCSColl.Update();
                                Console.WriteLine(strContentSourceName + " FAST Search Content Source Deleted");
                            }

                            CustomConSource = (BusinessDataContentSource)FastCSColl.Create(typeof(BusinessDataContentSource), strContentSourceName);
                            CustomConSource.StartAddresses.Add(BusinessDataContentSource.ConstructStartAddress("Default", new Guid("00000000-0000-0000-0000-000000000000"), strLogSystemName, strLogSystemName));
                            CustomConSource.CrawlPriority = CrawlPriority.Normal;
                            FastCSColl.Update();
                            Console.WriteLine("External Source FAST Content Source Created");

                        }

                    }
                }
                else
                {
                    using (SPSite site = new SPSite(WebAppUrl))
                    {
                        foreach (SearchServiceApplication objServiceApp in SearchService.Service.SearchApplications)
                        {
                            if (objServiceApp.SearchApplicationType == SearchServiceApplicationType.Regular)
                            {
                                SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(site));
                                Guid appId = proxy.GetSearchServiceApplicationInfo().SearchServiceApplicationId;
                                SearchServiceApplication SearchApp = objServiceApp;         //SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(appId);
                                Content content = new Content(SearchApp);
                                ContentSourceCollection csColl = content.ContentSources;
                                bool CheckExist = false;
                                foreach (ContentSource cs in csColl)
                                {
                                    if (cs.Name.Equals(strContentSourceName))
                                    {
                                        CheckExist = true;
                                        Console.WriteLine(strContentSourceName + " is already exist");
                                    }
                                }
                                BusinessDataContentSource CustomConSource = null;
                                if (CheckExist)
                                {
                                    CustomConSource = (BusinessDataContentSource)csColl[strContentSourceName];
                                    if (CustomConSource.StartAddresses.Count > 0)
                                    {
                                        CustomConSource.StartAddresses.Remove(BusinessDataContentSource.ConstructStartAddress("Default", new Guid("00000000-0000-0000-0000-000000000000"), strLogSystemName, strLogSystemName));
                                    }
                                    CustomConSource.Delete();
                                    csColl.Update();
                                    Console.WriteLine(strContentSourceName + " Deleted");
                                }
                                CustomConSource = (BusinessDataContentSource)csColl.Create(typeof(BusinessDataContentSource), strContentSourceName);
                                CustomConSource.StartAddresses.Add(BusinessDataContentSource.ConstructStartAddress("Default", new Guid("00000000-0000-0000-0000-000000000000"), strLogSystemName, strLogSystemName));
                                CustomConSource.CrawlPriority = CrawlPriority.Normal;
                                csColl.Update();
                                Console.WriteLine("External Source Content Source Created");
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:" + ex.Message);
            }
            Console.WriteLine("Content Source Update Ended");

        }


No comments:

Post a Comment