Custom Search Scopes at Site Collection Level


static void GetAvailableScopes()
        {
            SearchContext searchContext;
            using (SPSite site = new SPSite("Your Site Collection URL"))
            {
                searchContext = SearchContext.GetContext(site);
                Scopes scopes = new Scopes(searchContext);
                foreach (Scope scope in scopes.AllScopes)
                {                  
                    Console.WriteLine("\t Scope Name: {0}", scope.Name);   
                }
            }
        }
 
static void CreateScope()
        {
            SearchContext searchContext;
            using (SPSite site = new SPSite("Your Site Collection URL"))
            {
                searchContext = SearchContext.GetContext(site);
                Scopes scopes = new Scopes(searchContext);
                Scope newScope = scopes.AllScopes.Create("Your Custom Scope", "Scope Desc", new Uri(site.Url), true, "Custom Search ASPx Page", ScopeCompilationType.AlwaysCompile);
                newScope.Rules.CreateUrlRule(ScopeRuleFilterBehavior.Include, UrlScopeRuleType.Folder, "Document Library Folder with URL");                 
                newScope.Update();
                ScopeDisplayGroup group = scopes.GetDisplayGroup(new Uri(site.Url), "Search Dropdown");
                group.Add(newScope);
                group.Update();
            }
        }

 
private static void RemoveSharedScopes()
        {
             SearchContext searchContext;
             using (SPSite site = new SPSite("your Site Collection URL"))
             {
                 searchContext = SearchContext.GetContext(site);
                 Scopes scopes = new Scopes(searchContext);
                 ScopeDisplayGroup group = scopes.GetDisplayGroup(new Uri(site.Url), "Search Dropdown");                
                 foreach (Scope scope in scopes.GetSharedScopes())
                 {
                     if (scope.Name == "All Sites" || scope.Name == "People")
                         group.Remove(scope);
                 }
                 group.Update();
             }
        }


static void UpdateSiteCollectionSearchCenterSettings()
        {           
            using (SPSite site = new SPSite("Your Site Collection URL"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    string searchCenter = @"Search Center URL";
                    web.AllProperties["SRCH_ENH_FTR_URL"] = searchCenter;
                    web.Update();
                }
            }
        }

Comments

Popular posts from this blog

Windows Azure Package Build Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Resource ID : 1. The request limit for the database is 180 and has been reached.

How to get Client's Location using IPAddress