How to Set Specific Site Collection as SharePoint Search Scope using C#?

How to Set Specific Site Collection as SharePoint Search Scope using C#?

  • I have two site collections as Portal1 and Portal2. In Portal2, I have enterprise search centre and will add custom Search scope for Portal1 site collection using C#. I deployed project in Portal2 and activated feature. With properties.Feature.parent as SPSite, I got Site of Portal2. How can I get Portal1 (SpSite site)? scope.Rules.CreateUrlRule(ScopeRuleFilterBehavior.Include, UrlScopeRuleType.HostName, site.RootWeb.Url); Site.RootWeb.Url should get URL of Portal1. Thanks

  • Answer:

    If you know the url of Portal1, you can directly use it instead of site.RootWeb.Url. If you need SPSite object of Portal1 you can use : using (SPSite oSiteCollection = new SPSite([Portal1Url])) { ... } If you dont know the url of Portal1,You should know have at least one property to find the site collection in the web application.Below is the example if you know Title of rootweb: public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite site = properties.Feature.Parent as SPSite; if (site != null) { SPWebApplication webApp = site.WebApplication; string targetUrl = string.Empty; foreach (SPSite siteToSearch in webApp.Sites.Where (siteToSearch => siteToSearch.RootWeb.Title == "Portal1 Title")) { targetUrl = siteToSearch.Url; } } }

mkacar at SharePoint Visit the source

Was this solution helpful to you?

Other answers

If you are trying to get the URL for Portal1 from a feature activated in Portal2 you will need to either get it from the SPWebApplication and use logic to determine which SPSite object to use, use a hard-coded URL to create a Portal1 SPSite object, or use a feature property to define the URL to use to create a Portal1 SPSite object. Somewhere along the line you will have to define a hard-coded or property-based value that will enable you to create an SPSite object, unless you know a way to achieve the same using logic. Hope this helps.

Steve

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.