Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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 = $web.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.

share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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