locked
Get the Parent List of a child list's lookup column in code RRS feed

  • Question

  • Guys,

    all i want to do is get the parent list and then get the parent list item of a  child list's lookup column in c#.

    Not sure why this is a big deal.

    any ideas?

    thanks

    Tuesday, February 14, 2012 10:32 PM

Answers

  • You can use the SPFieldLookupValue class to read the SPListItem ID of the item that is selected in the lookup column on the child list.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookupvalue.aspx

    It´s possible to determine the parent list by inspecting the lookup column configuration. Take a look at this sample:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookup.lookuplist.aspx


    SharePoint Developer | @zeemanj | Blog

    Tuesday, February 14, 2012 11:00 PM
  • Hi

    Please refer to the following script:

    $web = (get-spsite "your site url here").OpenWeb()
    $childList = $web.Lists.TryGetList("your child list name here")

    $lookupFieldName = "the name of the lookup field in the child list"
    $lookupField = $childList.Fields.GetField($lookupFieldName)

    $parentListGUID = [GUID]$lookupField.LookupList
    $parentList = $childList.ParentWeb.Lists.TryGetList($parentListGUID)
    $parentfield = $lookupField.LookupField
    $parentField = $parentList.Fields.GetField($strfield)
    $type = $parentField.TypeAsString

    $item = $childList.Items[0]
    $fieldValue = New-Object Microsoft.Sharepoint.SPFieldLookupValue(($item[$lookupFieldName] -as [string]))
    $value = $fieldValue.LookupValue
    $spQuery = New-Object Microsoft.SharePoint.SPQuery
    $camlQuery = "<Where><Eq><FieldRef Name = `"$parentfield`"/><Value Type=`"$type`">$value</Value></Eq></Where>"
    $spQuery.Query = $camlQuery
    $spListItems = $parentList.GetItems($spQuery)


    SharePoint 2010 PowerShell

    Wednesday, February 15, 2012 2:29 AM
  • Hello,

    Here is the C# code for getting the Parent List of a Look Up column

                using (SPSite site = new SPSite("http://rams"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list  = web.Lists["CUSTOM_LIST"];
                        SPFieldLookup lookup = list.Fields["LookUp Column Name"] as SPFieldLookup;
                        SPList ChildList = lookup.ParentList;
                    }
                }


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010


    Wednesday, February 15, 2012 4:08 AM

All replies

  • You can use the SPFieldLookupValue class to read the SPListItem ID of the item that is selected in the lookup column on the child list.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookupvalue.aspx

    It´s possible to determine the parent list by inspecting the lookup column configuration. Take a look at this sample:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookup.lookuplist.aspx


    SharePoint Developer | @zeemanj | Blog

    Tuesday, February 14, 2012 11:00 PM
  • Hi

    Please refer to the following script:

    $web = (get-spsite "your site url here").OpenWeb()
    $childList = $web.Lists.TryGetList("your child list name here")

    $lookupFieldName = "the name of the lookup field in the child list"
    $lookupField = $childList.Fields.GetField($lookupFieldName)

    $parentListGUID = [GUID]$lookupField.LookupList
    $parentList = $childList.ParentWeb.Lists.TryGetList($parentListGUID)
    $parentfield = $lookupField.LookupField
    $parentField = $parentList.Fields.GetField($strfield)
    $type = $parentField.TypeAsString

    $item = $childList.Items[0]
    $fieldValue = New-Object Microsoft.Sharepoint.SPFieldLookupValue(($item[$lookupFieldName] -as [string]))
    $value = $fieldValue.LookupValue
    $spQuery = New-Object Microsoft.SharePoint.SPQuery
    $camlQuery = "<Where><Eq><FieldRef Name = `"$parentfield`"/><Value Type=`"$type`">$value</Value></Eq></Where>"
    $spQuery.Query = $camlQuery
    $spListItems = $parentList.GetItems($spQuery)


    SharePoint 2010 PowerShell

    Wednesday, February 15, 2012 2:29 AM
  • Hello,

    Here is the C# code for getting the Parent List of a Look Up column

                using (SPSite site = new SPSite("http://rams"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list  = web.Lists["CUSTOM_LIST"];
                        SPFieldLookup lookup = list.Fields["LookUp Column Name"] as SPFieldLookup;
                        SPList ChildList = lookup.ParentList;
                    }
                }


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010


    Wednesday, February 15, 2012 4:08 AM
  • Hello,

    Here is the C# code for getting the Parent List of a Look Up column

                using (SPSite site = new SPSite("http://rams"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list  = web.Lists["CUSTOM_LIST"];
                        SPFieldLookup lookup = list.Fields["LookUp Column Name"] as SPFieldLookup;
                        SPList ChildList = lookup.ParentList;
                    }
                }


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010


    The list and ChildList is the same object

    SharePoint 2010 PowerShell

    Wednesday, February 15, 2012 5:16 AM
  • Thanks for pointing that out...here is the updated code

                using (SPSite site = new SPSite("http://rams"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list  = web.Lists["CUSTOM_LIST"];
                        SPFieldLookup lookup = list.Fields["LookUp Column Name"] as SPFieldLookup;
                        String strGuid = lookup.LookupList;
                        SPList ChildList = web.Lists[new Guid(strGuid)];
                    }
                }


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010

    • Proposed as answer by nuclear Thursday, July 5, 2012 9:24 AM
    Wednesday, February 15, 2012 5:37 AM