Quantcast
Channel: Second Life of a Hungarian SharePoint Geek
Viewing all articles
Browse latest Browse all 206

How to get the groups associated with a Person or Group field

$
0
0

Recently one of my colleagues wanted to delete a SharePoint group just because it had no permission assigned to. Fortunately, I remembered that I was created that group a few week ago to make it possible to limit the selectable users in the Assigned To field at one of our Tasks list (see illustration below).

image

If you are not sure about your groups, the following two PowerShell snippets can help to find the ones that are associated with Person or Group fields as a container of the selectable users (see SelectionGroup property). Before deleting a group, it might be advisable to check if it is associated with a field (using the first version below), and if it is, either change the association first, or let the group live.

Using the first script you can find the lists / fields on your site, where the selection scope is limited to a specific group called GroupName:

$site = Get-SPSite “http://yourspsite”
$group = $site.RootWeb.SiteGroups["GroupName"]
$site | Get-SPWeb -limit all | % { $_.Lists } | % {
    $_.Fields | ? {
        $_.Type -eq ‘User’ -and $_.SelectionGroup -eq $group.ID
      } | % {
        $list=$_.ParentList
        $web=$list.ParentWeb
        Write-Host ———————
        Write-Host Web: $web.Url
        Write-Host List: $list.Title
        Write-Host Field: $_.Title
      }
   }

In the second version of the script any list / field / group is displayed, where the selection scope is limited to any group. Orphaned associations may be possible, if the group itself was deleted after setting it as selection scope. In this case we display only the ID of the group, but that Name cannot be resolved anymore.

$site = Get-SPSite “http://yourspsite”
$siteGroups = $site.RootWeb.SiteGroups
$site | Get-SPWeb -limit all | % { $_.Lists } | % {
    $_.Fields | ? {
        $_.Type -eq ‘User’ -and $_.SelectionGroup -ne 0
      } | % {
        $list=$_.ParentList
        $web=$list.ParentWeb
        $groupName = $_.SelectionGroup
        try { $groupName = $siteGroups.GetByID($_.SelectionGroup).Name } catch { }
        Write-Host ———————
        Write-Host Web: $web.Url
        Write-Host List: $list.Title
        Write-Host Field: $_.Title
        Write-Host Group: $groupName
      }
   }

Note: the scripts above do not find groups selected as a value of a Person or Group field!



Viewing all articles
Browse latest Browse all 206

Trending Articles