Question

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.

Was it helpful?

Solution

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....
                web.Dispose();
            }
    }

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.

OTHER TIPS

I think the line

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

should be

$l = $w.Lists[$_.ListID]
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top