2

I am getting an error while creating result source programmatically using Console Application.

Below is my code.

 using (SPSite oSPsite = new SPSite("http://xxxx/subsite1/"))
        {
            Microsoft.SharePoint.SPServiceContext context = SPServiceContext.GetContext(oSPsite);

            // Get the search service application proxy
            SearchServiceApplicationProxy searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
            SearchService searchService = SearchService.Service;
            if (searchProxy != null)
            {
                // 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 = 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();
            }
        }  

and I am getting following error.

The type 'Microsoft.Office.Server.Search.Administration.SearchServiceApplication' cannot be used as type parameter 'U' in the generic type or method 'Microsoft.SharePoint.Administration.SPPersistedObjectCollection.GetValue(System.Guid)'. There is no implicit reference conversion from 'Microsoft.Office.Server.Search.Administration.SearchServiceApplication' to 'Microsoft.SharePoint.Administration.SPPersistedObject'.

Error is showing at below line from above code.

 SearchServiceApplication application = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(ssai.SearchServiceApplicationId);

2 Answers 2

1

You might be missing an assembly reference, make sure your project is referencing both Microsoft.Office.Server and Microsoft.Office.Server.Search.

0

This code is working on my VM which has SharePoint 2013 installed. I am assuming you have SharePoint 2013 Server installed where this code is executing.

1
  • Yes. I have SP 2013 Server installed in my VM. Commented Dec 2, 2013 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.