Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
Not at my box just now, bot could you check that $scopes is not null: if ($scopes) – Anders Rask May 27 '11 at 14:44
Nope, no scopes needing compilation either. – tekiegreg May 27 '11 at 15:00

6 Answers

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

share|improve this answer
Good ideas, but didn't turn up much :-/ thanks! – tekiegreg May 27 '11 at 18:41

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.

share|improve this answer

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.

share|improve this answer
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 '11 at 17:37

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
share|improve this answer

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)
share|improve this answer

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() 
share|improve this answer
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 Sharepoint Nov 26 '12 at 10:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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