2

When run, this code returns an error on the last line "Error Calling GetDisplayGroup with 2 arguments, Specified cast is not valid".

    $site=get-spsite http://xxx/sites/xxx

    $displayName="Search Dropdown"

    $owningSiteURL=New-Object System.Uri($site.URL)
    $displayInAdminUI=$true
    $addScope="Forms Library"

    $searchContext=[Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($site)
    $scopes=New-Object Microsoft.Office.Server.Search.Administration.Scopes($searchContext)
    $displayGroup=$scopes.GetDisplayGroup($owningSiteURL,$displayName)

The reference documents on MSDN show 2 arguments to be passed, a System.Uri and a string. Near as I can tell this is what is happening. Anyone have any idea? I suspect an issue to get bumped to Microsoft but not sure...

EDIT TO ADD: I should also add that when we access the site (via a browser, not even a PowerShell WebClient visit will do the trick) the problem resolves, nice that it can be fixed by this, but I'm trying to set a default search group for 500+ sites, visiting each one by browser after running to that far in the script is not a practical option.

2
  • Not at my box just now, bot could you check that $scopes is not null: if ($scopes) May 27, 2011 at 14:44
  • Nope, no scopes needing compilation either.
    – tekiegreg
    May 27, 2011 at 15:00

9 Answers 9

1

The code looks fine to me. These kind of PowerShell errors can be tricky, be sure to check in ULS log (powershell.exe entries) for further info.

You could also try and do it in consoleapp. Just to get better error message

1
  • Good ideas, but didn't turn up much :-/ thanks!
    – tekiegreg
    May 27, 2011 at 18:41
1

I tested your code and it worked fine...

But by a little messing around I got your exception message. In one of the tests I modified the displayname to a non-existing. If the group name doesn't exists, the exception is thrown. Also check the name on extra white spaces.

1

Could it be a problem with the URI? I am running identical code in one site collection and it works.

In a second site collection ( which is not the root site collection ) it doesn't work and displays the same error:

    Exception calling "GetDisplayGroup" with "2" argument(s): "Specified cast is not valid."
At line:1 char:38
+ $displayGroup=$scopes.GetDisplayGroup <<<< ($owningSiteURL,"Search Dropdown ")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I have ran a write-host to print out all the available displaygroups:

foreach ($displaygroup in $scopes.AllDisplayGroups ){ Write-Host $displaygroup.Name }

and my display group is in the list ( it comes up several times actually ) therefore I'm suspicious of the URI.

1
  • Ok this is where I wish I could find someone else to test, as it's too much effort for me now that the environment is no longer in my control and reproducing would require some work. If I could get third party verification from another ID I'll call this the answer :-)
    – tekiegreg
    Dec 2, 2011 at 17:37
1

I have the same problem, same code, and if I look at the properties of the Scopes object after getting it I can see that there are no DisplayGroups in AllDisplayGroups. For me, I'm sure this is why the script is failing there, but I can't figure out WHY the displayGroup does not show up in the Scopes object. I'm trying to use the default "Search Dropdown".

PS> $scopes

CompilationScheduleType    : Automatic
AllScopes                  : {People, All Sites, Global Query Exclusion, Rank Demoted Sites...}
AllDisplayGroups           : {}
AverageCompilationDuration : 00:01:09
LastCompilationTime        : 12/01/2012 10:12:35 PM
NextCompilationTime        : 12/01/2012 10:27:35 PM
CompilationState           : Idle
CompilationPercentComplete : 100
ScopesNeedingCompilation   : 0
1

I had the same problem. We need to use a RemoteScopes object, as in Manesh's example.

$serviceContext = Get-SPServiceContext -Site $site
$scopes = New-Object Microsoft.Office.Server.Search.Administration.RemoteScopes($serviceContext)
1

I have been encountering the same issue intermittently myself using code like this:

$SPWebApp = Get-SPWebApplication $spSiteUrl
$displayGroupName = "Search Dropdown"
$ssa = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$scopes=New-Object Microsoft.Office.Server.Search.Administration.Scopes($ssa)

foreach ($SPSite in $SPWebApp.Sites) {
    $displayGroup = $scopes.GetDisplayGroup($SPSite.Url, $displayGroupName)
    ...
}

Through testing this on multiple environments and a lot of trial and error (as the issue was sporadic and didn't always occur), I found that the way around it was to visit the /_layouts/viewscopes.aspx page for each site collection in-browser first before running the script. Once the page had been visited, the .GetDisplayGroup method works without error.

I have no idea why this might be the case, although it definitely fixed the issue for me. Luckily the solution I'm working on only has a handful of site collections so it's not too cumbersome, but it obviously might not be an ideal solution for those with many site collections to contend with.

2
  • This sounds the most promising of anything I've done, but as I've moved away from this environment and no longer have access, can't test for the answer. You may have to settle with a +1 instead :-/
    – tekiegreg
    Aug 19, 2013 at 16:56
  • If opening the pages in-browser proves to be the only way to get this working, perhaps it might be possible to write a PS script that will open all relevant required pages then close them once they've loaded. A bit of a hack step to be fair, but it might be better than individually doing them manually. Some starter points here that may be useful: social.technet.microsoft.com/Forums/windowsserver/en-US/… stackoverflow.com/questions/14563003/… Good luck!
    – Vassal
    Aug 20, 2013 at 14:48
0

This works -

$SiteUrl = "https://portal" 
$DisplayGroup = "Search Dropdown"
$ServiceContext = Get-SPServiceContext -Site $SiteUrl
$ScopesManager = New-Object Microsoft.Office.Server.Search.Administration.RemoteScopes($ServiceContext) 

$SearchScopeDisplayGroup = $ScopesManager.GetDisplayGroup($SiteUrl, $DisplayGroup) 
$SearchScopeDisplayGroup.Clear()

$ScopesManager.AllScopes | ForEach-Object { 
    $DisplayGroupScopeCurrent = $_ 
    $SearchScope = $ScopesManager.AllScopes | Where { $_.Name -eq $DisplayGroupScopeCurrent.name } 
    $SearchScopeDisplayGroup.Add($SearchScope) 
} 

$SearchScopeDisplayGroup.Update() 
1
  • hi welcome to sharepoint.stackexchange, care to explain more indepth on what the code is doing? so people can understand what is going on
    – Ali Jafer
    Nov 26, 2012 at 10:43
0

I had the same error appearing to my code when trying to "GetDisplayGroup". At first the error was appearing when I was running the powershell under an account that did not have rights for the specific $site.URL .

I ran powershell under a different account and I found out that there was no error in the code. It seems that the specific error appears when the display group cannot be found in the display group collection. For this reason I used this code:

try
    {
        $existingDisplayGroup = $scopes.GetDisplayGroup($owningSiteURL, $DisplayGroupName) # When the display group is not found an exception is thrown

        if($existingDisplayGroup -ne $null)
        {
                   # When the display group is found you run code here
        }
    }
    catch
    {
        # When the display group is not found code resumes here
    }
0

I know this is almost three years old, so you've probably manually visited viewscopes.aspx for the 500 sites, but here's my solution :)

Short story: viewscopes.aspx ensures the site collection is "registered" as a consumer of scopes (whatever the heck that means), which in turn creates the display groups and populates them with scopes. Here's some PowerShell to automate, without having to hit viewscopes.aspx:

Add-PSSnapIn Microsoft.SharePoint.Powershell -ErrorAction:SilentlyContinue
[reflecton.assembly]::LoadWithPartialName("Microsoft.Office.Server") | Out-Null

# Get our site, search context and scopes
$site = Get-SPSite $siteUrl
$siteId = $site.Id.ToString("D")
$ctx = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($site)
$scopes = New-Object Microsoft.Office.Server.Search.Administration.Scopes($ctx)

# Here's the cool stuff. We're really just calling $scopes.Consumers.EnsureRegistered($siteId)
$scopes.Consumers.EnsureRegistered($siteId)
$BindingFlags = [Reflection.BindingFlags] "NonPublic,Instance"

$consumers = $scopes.GetType().GetMember("Consumers",$BindingFlags)[0].GetValue($scopes, $null)
$consumer = $consumers.GetType().GetMethod("EnsureRegistered",$BindingFlags).Invoke($consumers, $siteId)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.