0

How to find the site columns associated with a Content Type using PowerShell Script in my SP 2013 Site Collection.

Add-PSSnapin Microsoft.SharePoint.PowerShell
$site = Get-SPSite "http://srvr1:123/sites/HR"   #your URL  
$web = $site.RootWeb

# Get the GUID of your Site Column View_x0020_Access
$guid =$web.Fields["myDocumentCol"].id            # your Site 
$ct = $web.ContentTypes["Basic Content Type"] 

for ($i=0; $i -lt $ct.Count; $i++) 
{
    Write-Host "inside for loop"
    for ($j=0; $j -lt $ct[$i].Fields.Count; $j++) 
    {
       if ($ct[$i].Fields[$j].id -eq $guid)
       {
           Write-Host $ct[$i].Name " has column"
       }
    }
 }

I tried the above code but it didn't work. It gave me index into null Array error.

2 Answers 2

1

Try

Add-PSSnapin Microsoft.SharePoint.PowerShell
$site = Get-SPSite "http://srvr1:123/sites/HR"   #your URL  
$web = $site.RootWeb
# Get the GUID of your Site Column View_x0020_Access
$guid =$web.Fields["myDocumentCol"].id            # your Site 
$ct = $web.ContentTypes["Basic Content Type"] 
if($ct) 
{
    Write-Host "inside for loop"
    $fieldInUse = $ct.Fields | Where {$_.Id -eq $guid }
    if ($fieldInUse)
    {
        Write-Host $ct.Name " has column"
    }       
}
1

Didn't get Amals script to work, but used this instead. The real difference is $ct = $web.AvailableContentTypes.

# Find all content types that use that column type
# This will display all content types and list details if there match for the column

$site = Get-SPSite "http://srvr1:123/sites/HR"
$web = $site.RootWeb

# Get the GUID of your Site Column 
$guid = $web.fields["myDocumentCol"].id
$ct = $web.AvailableContentTypes 

for ($i=0; $i -lt $ct.Count; $i++) 
{
  for ($j=0; $j -lt $ct[$i].Fields.Count; $j++) 
  {
    if ($ct[$i].Fields[$j].id -eq $guid)
    {
      Write-Host $ct[$i].Name " has column"
    }
  }
}

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.