I am trying to setup a cross-site lookup field using XML (which I have been able to do in past SharePoint On-Premises environments). I am having some difficulties however creating a similar configuration in my SharePoint Online tenant based environment.
My target configuration has the following details:
- Two Team (modern) sites, both at same site level, and both associated with a Team (hub) site.
- SiteA has a list of projects/products.
- SiteB has a list of records containing a lookup field which needs to point to the Project list on SiteA.
Right now, I am just trying to create a Site Column on SiteB that will be a lookup field pointing to the Project List on SiteA. Since I know that this configuration cannot be done in the OOB user interface, I am using PowerShell 5.1 commands and PnP library cmdlets to do this via XML. When I execute the lines below, a Site Column is created at SiteB, but it does not actually appear to be connected to the Project List (SiteA) so there are no values when I use it to create a column in a list. I have also tried directly creating a column in a list (to see if that would work better).
To the XML below, I am specifying the GUID of the list on SiteB which contains the list of projects and the GUID of the Web context for SiteB. I am using NewGuild to generate a new GUID for the new site column.
Some samples of my PS code:
$newguid = [GUID]::NewGuid()
$fieldXML = '<Field ID="' + $newguid + '"
Type="Lookup" Name="TestProj4"
DisplayName="TestProj4" Static
Name="TestProj4" Group="TestColumns"
List="{7840346f-d83a-4111-806b-6f97db6f0bc6}"
WebId="e3ab472f-0146-4229-abdd-36f9b4aabeb8"
ShowField="Title" Required="TRUE" EnforceUniqueValues="FALSE" />'
Add-PnPFieldFromXml -FieldXml $fieldXML
One thing that I have noticed (and which has puzzled me) is that when I use Get-PnPWeb to get (what I believe to be) the WebID, the GUID returned is exactly the same for both SiteA and SiteB. I have used separate PowerShell consoles to extract information for the sites separately to make sure that the commands are operating on the correct sites. So I am wondering if I am using the correct GUID in my XML (for WebId) to point to the Web (or site) containing the project list.
I am going to keep trying things, but I am hoping that someone might have some suggestions or insights for how to configure the XML for SharePoint Online and set the cross-site lookup successfully.
UPDATE to Original Question
I have been continuing to work on this issue. I had been working with two Team sites which are both associated with a Hub site. But then I decided to try working with two different Team sites - not associated with any Hub sites. In this case, the WebID (LookupWebIDs) were different from each other. So, I reconfigured my XML and used Add-PnPFieldFromXML which created a new Site Column (no errors), and property values for new column checked out as desired.
$fieldXML = ‘<Field Type="Lookup" DisplayName="TestColumnA" Required="FALSE" EnforceUniqueValues="FALSE" List="{9e887b1f-12ad-4feb-a1f8-a9fe5d15b6ab}" WebId="8ff42b00-5858-4f6d-8295-80aff7950373" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE" Group="TestColumns" ID="{e06edaad-7b83-4be9-9fec-52e2eb9b92f0}" SourceID="{8ff42b00-5858-4f6d-8295-80aff7950373}" StaticName="TestColumnA" Name="TestColumnA" Description="Test column for cross-site lookup" />’
Add-PnPFieldFromXML -fieldXML $FieldXML (to create the new Test Column on Site A)
*Check properties*
PS C:> $testCol.LookupList
{9e887b1f-12ad-4feb-a1f8-a9fe5d15b6ab}
PS C:> $testCol.LookupWebID
Guid
----
8ff42b00-5858-4f6d-8295-80aff7950373
But when I tried to use the new column in a list - the pulldown has no values (should be 3 values) - so still does not seem to be correctly connecting to list on other site.
UPDATE 2 - One possible solution -----------------------------------
I was finally able to get the cross-site capability using PowerShell/GUID/XML to set the SchemaXML property of a lookup field in my list (SiteB) to point to list of values (SiteA). GUIDs for List, WebID, and SourceID were set to cross-site values and everything worked as expected. However, I first had to create a Classic Site Collection (on my Online tenant) so that I could then create two Team sites using the Classic SharePoint Team Site template.
No matter what I tried, I could not get the cross-site capability working with a Modern Team Site template. For me, it only seems to work in sites created with the Classic template. It is still important to find a solution that will allow cross-site lookup capability using the Modern configuration, so this issue is still unresolved. I just wanted to share what did work - in case it helps someone else.