2

How to find the list that a particular lookup column in a list is bound to, programatically?

  • Please provide more details about your requirement. – Karthik Jaganathan Feb 16 '14 at 14:42
  • It's impossible for us to provide a good answer as we cannot know which API you might be using. Are you using JSOM, REST API in 2013, the REST API in 2010, the Managed CSOM via C#, or the server OM? Please add more detail. – Robert Kaucher Feb 16 '14 at 15:57
2

The easiest way is to look in MSDN documentation http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookup.lookuplist.aspx

In the link you can find the below example which I guess is what you are looking for.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("http://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    SPList list = site.Lists["Pending Orders"];

                    foreach (SPField item in list.Fields)
                    {
                        if (item is SPFieldLookup)
                        {
                            SPFieldLookup field = (SPFieldLookup)item;

                            if (!String.IsNullOrEmpty(field.LookupList) && !String.IsNullOrEmpty(field.LookupField))
                            {
                                // Is this the primary or secondary field for a list relationship?
                                string strRelationship = field.IsRelationship ? "Primary":"Secondary";

                                // Print the display name of the field.
                                Console.WriteLine("\nField: {0} ({1} Field)", field.Title, strRelationship);

                                // Is this a secondary field in a list relationship?
                                if (field.IsDependentLookup)
                                {
                                    SPField primaryField = list.Fields[new Guid(field.PrimaryFieldId)];
                                    Console.WriteLine("Primary Field: {0}", primaryField.Title);
                                }

                                // Get the site where the target list is located.
                                using (SPWeb targetSite = siteCollection.AllWebs[field.LookupWebId])
                                {
                                    // Get the name of the list where this field gets information.
                                    SPList targetList = targetSite.Lists[new Guid(field.LookupList)];
                                    Console.WriteLine("Related list: {0}", targetList.Title);

                                    // Get the name of the field where this field gets information.
                                    SPField targetField = targetList.Fields.GetFieldByInternalName(field.LookupField);
                                    Console.WriteLine("Related field: {0}", targetField.Title);
                                }
                            }
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

Quick and DIRTY Pseudocode example:

function IsFieldInUse(splist, spfield, currentweb)
{
    foreach (field in currentweb.fields)
    {
        if (field is spfieldlookup)
        {
            if (field.lookuplist==splist && field.lookupfield==spfield) return true;
        }
    }

    foreach (list in currentweb.lists)
    {
        foreach (field in list.fields)
        {
            if (field is spfieldlookup)
            {
                if (field.lookuplist==splist && field.lookupfield==spfield) return true;
            }
        }
    }

    foreach (web in currentweb.webs)
    {
        if (IsFieldInUse(splist, spfield, web))
        {
            return true;
        }
    }
    return false;   
}

function IsFieldInUse(splist, spfield)
{
    return IsFieldInUse(splist, spfield, currentSite.rootweb)
}
| improve this answer | |
  • I have a slight feeling OP is looking for a way to find lists with lookup to a particular field, and if so it is much more complicated. In that case one might have to do a recursive search, very resource intensive. But I can only speculate because of the question vagueness – eirikb Feb 16 '14 at 9:49
  • ahh you might be right..or perhaps fields that lookup to a list. – Burrhus Feb 16 '14 at 9:57
  • Maybe, who knows :) I'll give you a vote though, until further specified this is IMHO the correct answer. – eirikb Feb 16 '14 at 10:02
  • 1
    Please try to elaborate your answer a bit more. A simple peace of code will do wonders :) – Robert Lindgren Feb 17 '14 at 8:23
  • 1
    I want to delete row in list.Before deleting I want to Check that particular row data is used by any other list. If it's used by another list i want to display message like the data is using by another list,You Can't delete this row data. How will be do it? – user3295668 Feb 21 '14 at 9:17
0

You can use: var value = SP.FieldLookup.get_lookupList ()

I found the answer here: https://msdn.microsoft.com/en-us/library/office/jj245483.aspx

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