Get-SPEnterpriseSearchQueryScopeDisplayGroup and Remove-SPEnterpriseSearchQueryScopeDisplayGroupScope

Aug 10, 2010 at 12:47 AM

1. Get-SPEnterpriseSearchQueryScopeDisplayGroup

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.PowerShell;

using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Cmdlet;

namespace Community.SharePoint.PowerShell
{
    /// <summary>
    /// SharePoint PowerShell Community Kit
    /// This cmdlet gets a display group given a site collection
    /// URL and SearchServiceApplicationPipeBind
    /// Example (Gets a scope display group called "Search Dropdown"):
    /// $searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
    /// $group = Get-SPEnterpriseSearchQueryScopeDisplayGroup -Name "Search Dropdown" -url http://sp2010 -SearchApplication $searchapp
    /// Author: Peter Cuttriss
    /// Creation Date: 8/10/2010
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "SPEnterpriseSearchQueryScopeDisplayGroup")]
    public class GetEnterpriseSearchQueryScopeDisplayGroup : SPGetCmdletBase<ScopeDisplayGroup>
    {
        [Parameter(Mandatory = true, Position = 1)]
        public string Name { get; set; }

        [Parameter(Mandatory = true), Alias(new string[] { "o" })]
        public Uri Url { get; set; }

        [Parameter(Mandatory = false), DefaultValue(true), Alias(new string[] { "disp" })]
        public bool? DisplayInAdminUI { get; set; }

        [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
        public SearchServiceApplicationPipeBind SearchApplication { get; set; }

        protected override IEnumerable<ScopeDisplayGroup> RetrieveDataObjects()
        {
            List<ScopeDisplayGroup> list = new List<ScopeDisplayGroup>();

            // get the search applicatioin from the pipe bind
            SearchServiceApplication searchServiceApplication = SearchApplication.Read();

            Scopes scopes = new Scopes(searchServiceApplication);
            try
            {
                if (scopes.GetDisplayGroup(Url, Name) != null)
                {
                    list.Add(scopes.GetDisplayGroup(Url, Name));
                }
            }
            catch (InvalidCastException invEx)
            {
                throw new InvalidCastException("The scope display group " + Name + " does not exist.", invEx);
            }

            return list;
        }
    }
}

2. Remove-SPEnterpriseSearchQueryScopeDisplayGroupScope

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.PowerShell;

using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Cmdlet;

namespace Community.SharePoint.PowerShell
{
    /// <summary>
    /// SharePoint PowerShell Community Kit
    /// This cmdlet creates removes a scope from a given display group given
    /// ScopeDisplayGroupPipeBind and ScopePipeBind
    /// Example (Removes the "All Sites" scope from the "Search Dropdown" display group):
    /// $searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
    /// $group = Get-SPEnterpriseSearchQueryScopeDisplayGroup -Name "Search Dropdown" -url http://sp2010 -SearchApplication $searchapp
    /// $scope = Get-SPEnterpriseSearchQueryScope -Identity "All Sites" -SearchApplication $searchapp
    /// Remove-SPEnterpriseSearchQueryScopeDisplayGroupScope -ScopeDisplayGroup $group -Scope $Scope
    /// Author: Peter Cuttriss
    /// Creation Date: 8/10/2010
    /// </summary>
    [Cmdlet(VerbsCommon.Remove, "SPEnterpriseSearchQueryScopeDisplayGroupScope")]
    public class RemoveEnterpriseSearchQueryScopeDisplayGroupScope : SPRemoveCmdletBase<ScopeDisplayGroup>
    {
        [Parameter(Mandatory = true, ValueFromPipeline = true, Position=0), Alias(new string[] { "g" })]
        public ScopeDisplayGroupPipeBind ScopeDisplayGroup { get; set; }

        [Parameter(Mandatory = true, ValueFromPipeline = true), Alias(new string[] { "s" })]
        public ScopePipeBind Scope { get;  set; }

        protected override void DeleteDataObject()
        {
            ScopeDisplayGroup scopeDisplayGroup = ScopeDisplayGroup.Read();
            Scope scope = Scope.Read();

            // remove the scope from the display group
            scopeDisplayGroup.Remove(scope);
            scopeDisplayGroup.Update();
        }
    }
}

Coordinator
Aug 10, 2010 at 3:24 PM

This is great.  I've tried out the Get-SPEnterpriseSearchQueryScopeDisplayGroup cmdlet already and I am using it to test my Remove-SPEnterpriseSearchQueryScopeDisplayGroup method I was writing.  I'll also get your code in you suggested to verify that the scope doesn't already exist.  Thanks for the contribution.

Corey