Is it possible to itterate all Lists in SharePoint to find out which ones use a custom Lookup field?

I have an application which is currently failing because the custom Lookup field was pointing to a redundant URL which was removed.


It is not clear from your question what kind of solution you are looking. My answer is code based and tested on SP 2010 platform.

So here is c# code (link):

    using(SPSite site = new SPSite("http://mysite"))
            SPField fld = site.RootWeb.AvailableFields["[FIELD DISPLAY NAME]"]
            ICollection<SPFieldTemplateUsage> collection = fld.ListsFieldUsedIn();
            foreach (SPFieldTemplateUsage usage in collection)
                SPWeb web = site.AllWebs[usage.WebID];
                SPList list = web.Lists[usage.ListID];
                // Do something here....

or you can use something similar using PowerShell:

$siteUrl = "http://mysite"
$fieldDisplayName = "My Field"   
$site = Get-SPSite $siteUrl
$field = $site.RootWeb.AvailableFields[$fieldDisplayName]
$lists = $field.ListsFieldUsedIn()
$field.ListsFieldUsedIn() | ForEach-Object {
    $w = $site.AllWebs[$_.WebID]
    $l = $w.Lists[$_.ListID]
    Write-Host "Web:" $w.Title "List:" $l.Title "ListUrl:" $l.RootFolder.Url

So I am not iterating lists but instead I am using SPField.ListsFieldUsedIn method to find all field usages.

|improve this answer|||||
  • Hi Vedran, seems like a feasible solution. My problem pertains to SP2007, but should still work. I will test this, thank you. – JadedEric Sep 6 '11 at 9:25

I think the line

$l = $web.Lists[$_.ListID]

should be

$l = $w.Lists[$_.ListID]
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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