Second Life of a Hungarian SharePoint Geek

July 12, 2013

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

Filed under: PowerShell, SP 2010 — Tags: , — Peter Holpar @ 21:11

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!

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: