none
sharepoint 2013 creating Result Source via code

    Question

  • i am trying to create new result source with c# in a console application and get error each time, i succeed updating exist result source but can't create new one here is my code :

    using (SPSite oSPsite = new SPSite("http://XXXXXXX/sites/SearchAdministration"))
            {
                Microsoft.SharePoint.SPServiceContext context = SPServiceContext.GetContext(oSPsite);
    
                // Get the search service application proxy
                SearchServiceApplicationProxy 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
                SearchServiceApplicationInfo ssai = searchProxy.GetSearchServiceApplicationInfo();
    
                // Get the application itself
                SearchServiceApplication application = Microsoft.Office.Server.Search.Administration.SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(ssai.SearchServiceApplicationId);
    
                FederationManager fedManager = new FederationManager(application);
                SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.SPSite, oSPsite.RootWeb);
                Source currentResultSource = fedManager.CreateSource(owner);
                currentResultSource.Commit();                 
            }  


    the error is in the commit function - "The operation you are attempting violates an enforced dependency between objects, such as depending on an object that does not exist or deleting an object that other objects depend on." i have noticed that after the creation of the result source, the currentResultSource.id is all zero's


    Sunday, December 30, 2012 5:55 AM

Answers

  • Hi 

    try to add some values to source like name and providerid

     Source resultSource = manager.CreateSource(owner);//manager is federation manager
                resultSource.Name = name;
                resultSource.CreateQueryTransform(properties, query);
                resultSource.ProviderId = manager.ListProviders()["Local SharePoint Provider"].Id;
                resultSource.Commit();

    It will work because I have tested this and it is working fine

    • Marked as answer by royibeni Wednesday, January 09, 2013 7:17 AM
    Saturday, January 05, 2013 4:31 AM

All replies

  • Hi 

    try to add some values to source like name and providerid

     Source resultSource = manager.CreateSource(owner);//manager is federation manager
                resultSource.Name = name;
                resultSource.CreateQueryTransform(properties, query);
                resultSource.ProviderId = manager.ListProviders()["Local SharePoint Provider"].Id;
                resultSource.Commit();

    It will work because I have tested this and it is working fine

    • Marked as answer by royibeni Wednesday, January 09, 2013 7:17 AM
    Saturday, January 05, 2013 4:31 AM
  • Thanks it is working!!!
    Wednesday, January 09, 2013 7:18 AM
  • Hi i am also facing the similar issue.

    Could you pls post the working code?

    Tnx in advance

    Wednesday, January 09, 2013 9:01 AM
  •  using (SPSite oSPsite = new SPSite("http://xxxxxxx/sites/SearchAdministration"))
                {
                    Microsoft.SharePoint.SPServiceContext context = SPServiceContext.GetContext(oSPsite);
    
                    // Get the search service application proxy
                    SearchServiceApplicationProxy 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
                    SearchServiceApplicationInfo ssai = searchProxy.GetSearchServiceApplicationInfo();
    
                    // Get the application itself
                    SearchServiceApplication application = Microsoft.Office.Server.Search.Administration.SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(ssai.SearchServiceApplicationId);
    
                    FederationManager fedManager = new FederationManager(application);
                    SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.SPSite, oSPsite.RootWeb);
                    Source currentResultSource = fedManager.CreateSource(owner);
                    currentResultSource.Name = "ResultTest";
                    Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties QueryProperties = new Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties();
                    currentResultSource.CreateQueryTransform(QueryProperties, "");
                    currentResultSource.ProviderId = fedManager.ListProviders()["Local SharePoint Provider"].Id;
                    currentResultSource.Commit();                 
                }  

    Wednesday, January 09, 2013 9:28 AM
  • Hi

    I am working on a similar code. The code returns null when I try to find a result source using fedManager.GetSourceByName() method. I have created the result source at a site level. My goal is to update the result source query once I can find it. Please help me find out where I am going wrong.

    Please find here my code:

    using (SPSite oSPsite = new SPSite("http://SiteColURL"))
                {


                    Microsoft.SharePoint.SPServiceContext context = SPServiceContext.GetContext(oSPsite);

                    // Get the search service application proxy
                    SearchServiceApplicationProxy 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
                    SearchServiceApplicationInfo ssai = searchProxy.GetSearchServiceApplicationInfo();

                    // Get the application itself
                    //SearchServiceApplication application = Microsoft.Office.Server.Search.Administration.SearchService.Service.SearchApplications.GetValue(<SearchServiceApplication>ssai.SearchServiceApplicationId);
                    SearchServiceApplication application = Microsoft.Office.Server.Search.Administration.SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(ssai.SearchServiceApplicationId);

                    FederationManager fedManager = new FederationManager(application);
                    //SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.SPSite, oSPsite.RootWeb);
                    SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.Ssa, oSPsite.RootWeb);
                   
                    Source currentResultSource = fedManager.GetSourceByName("Test Result Source", owner);
                   
                    Console.WriteLine(currentResultSource.ToString());
    }


    Hemil Damor

    Wednesday, March 20, 2013 11:45 PM
  • The code is fine.I copied your code and is working fine in my environment

    Is it working for SSA and not working for site.

    try

    fedManager.GetDefaultSource(owner); 

    and see whether you are getting result.

    Friday, March 22, 2013 10:54 AM