+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Programmatically create a new search index

Programmatically create a new search index

2 posts, 0 answered
  1. Bobby
    Bobby avatar
    56 posts
    Registered:
    11 Feb 2009
    07 Aug 2010
    Link to this post
    Hi,

    I am trying to programmatically create a new search index. I am able to do so by using the following lines of code.

    private void CreateResearchAreaService(string researchArea)
            {
                //Add a new Service for searching to the page.
               
                IndexDataManager indexManager = new IndexDataManager();
                IIndexingServiceInfo newService = indexManager.CreateService(researchArea + "_Index", Messages.SearchServiceDescription);
                indexManager.SaveService(newService);
     }

    This creates a new service index just fine but I need to go a few steps further. I would like to create a new service index that uses my custom index provider. Can you tell me the proper way to do this? The following is the code that I am using:

    private void CreateResearchAreaService(string researchArea)
           {
               //Add a new Service for searching to the page.
                 
               IndexDataManager indexManager = new IndexDataManager();
               IIndexingServiceInfo newService = indexManager.CreateService(researchArea + "_Index", Messages.SearchServiceDescription);
               indexManager.SaveService(newService);
                  
                
               IServiceClientInfo client = indexManager.CreateClient(newService);
               client.Name = "Test";
               client.SettingsControl = typeof(SettingsControl).FullName;
               client.ViewSettingsControl = typeof(ViewControl).FullName;
               client.Description = Messages.SearchServiceDescription;
               client.Settings.Add(SettingsControl.ProviderNameKey, "Libraries");
               client.Settings.Add(SettingsControl.ResearchAreasKey, researchArea);
               client.ClientType = typeof(ResearchAreaIndexProvider);
                 
               newService.ServiceClients.Add(client);
               indexManager.SaveClient(client);
           }



    This code produces the following error when I go to the Administration section to view my current Service Indexes:
    The given key was not present in the dictionary.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
      
    Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
      
    Source Error:
      
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
      
    Stack Trace:
      
    [KeyNotFoundException: The given key was not present in the dictionary.]
       System.ThrowHelper.ThrowKeyNotFoundException() +28
       System.Collections.Generic.Dictionary`2.get_Item(TKey key) +7456284
       Telerik.Search.WebControls.Admin.ControlPanel.clientsRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e) +127
       System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs e) +108
       System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +136
       System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +443
       System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51
       System.Web.UI.WebControls.Repeater.DataBind() +75
       Telerik.Search.WebControls.Admin.ControlPanel.Indexes_ItemDataBound(Object sender, RepeaterItemEventArgs e) +767
       System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs e) +108
       System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +136
       System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +443
       System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51
       System.Web.UI.WebControls.Repeater.DataBind() +75
       Telerik.Search.WebControls.Admin.ControlPanel.LoadMainScreenTemplate() +368
       Telerik.Search.WebControls.Admin.ControlPanel.CreateChildControls() +54
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Control.AddedControl(Control control, Int32 index) +8682853
       System.Web.UI.ControlCollection.Add(Control child) +80
       Telerik.Cms.Web.UI.ServiceList.CreateChildControls() +1118
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842


    Can you please help me figure out what I am doing wrong? I am really lost and can't find any examples of how I would accomplish this.

    Thanks for your help,
    Bobby

  2. Bobby
    Bobby avatar
    56 posts
    Registered:
    11 Feb 2009
    07 Aug 2010
    Link to this post
    Hi again,

        I believe I have figured out my problem. So, for anyone else who wants to know who to create a service index manually and intialize it to a service provider, the code is below.

    private void CreateResearchAreaService(string researchArea)
            {
                //Add a new Service for searching to the page.
                IndexDataManager indexManager = new IndexDataManager();
                IIndexingServiceInfo newService = indexManager.CreateService(researchArea + "_Index", Messages.SearchServiceDescription);
                indexManager.SaveService(newService);
                
              
                IServiceClientInfo client = indexManager.CreateClient(newService);
                client.Name = "ResearchAreaIndex";
                client.SettingsControl = typeof(SettingsControl).FullName;
                client.ViewSettingsControl = typeof(ViewControl).FullName;
                client.Description = Messages.SearchServiceDescription;
                client.Settings.Add(SettingsControl.ProviderNameKey, "Libraries");
                client.Settings.Add(SettingsControl.ResearchAreasKey, researchArea);
                client.ClientType = typeof(ResearchAreaIndexProvider);
               

                newService.ServiceClients.Add(client);
                indexManager.SaveClient(client);
            }



    In my case, my service provider was called ResearchAreaIndex. My previous post was throwing an exception because the IServiceClientInfo.Name needed to be set to the index provider name. Hope this helps someone else.

    Bobby
2 posts, 0 answered