65.9K
CodeProject is changing. Read more.
Home

Get all Lookup Columns in a SharePoint site

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (2 votes)

Apr 7, 2014

CPOL
viewsIcon

16012

Introduction

The following code gets all the lookup columns present in all the list in a site collection. One can modify this to search for lookup columns with a particular name.

Using the code

You need to run the Powershell script in either 'SharePoint 2010 Management Shell' or third party editors like 'Power GUI' etc. Please note that the script should run on the server machine on which the site collection is present.

Also, one has to open the console as 'Administrator', otherwise will get error.

# Add SharePoint Snapin to PowerShell            
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {            
    Add-PSSnapin Microsoft.SharePoint.PowerShell            
}
    
function GetAllLookupColumns
{
    Param(

        [Parameter(Mandatory=$true, HelpMessage='-URL - Please provide the URL of the site collection.')]
        [string]$Url

    ) # END PARAMS
      
    if ($Url)
    {
        #write "Get the url"
        $site = Get-SPSite $Url
        #write "Get the url"
        foreach ($spweb in $site.AllWebs){  
            foreach($list in $spweb.Lists){
                foreach ($field in $list.Fields){ 
                    if($field.Type -eq "Lookup")
                    {
                        Write-Host "Match :: WEB - "$spweb.Url" :: Field - "$field" :: List - "$list
                    }
                }

            }
        }        
    }
    $site.dispose()            
    echo "Finished"
} 

To get all the places of lookup columns with a particular name, one can edit this code to have 1 more input parameter of field name and compare that in the IF condition.