Home > Software design >  How to add multiple values to a lookup column in a SharePoint list using the XML SOAP API (SharePlum
How to add multiple values to a lookup column in a SharePoint list using the XML SOAP API (SharePlum

Time:06-09

I was able to update/add a single value into a lookup field using SharePlum using this code:

sp_list = site.List('My_SP_List') # this creates SharePlum object

#100 is the GUID # of the value I want to add
update_data = [{'ID': '1', 'Lookup_field': "100"}]

sp_list.UpdateListItems(data=update_data, kind='Update')

Now, I am trying to add multiple values to the same lookup field. I have tried the following, but they have not worked:

update_data = [{'ID': '1', 'Lookup_field': "100;#101"}]

update_data = [{'ID': '1', 'Lookup_field': "100;#;#101"}]

update_data = [{'ID': '1', 'Lookup_field': "100,101"}]

update_data = [{'ID': '1', 'Lookup_field': ["100","101"]}]

update_data = [{'ID': '1', 'Lookup_field': "100",";#101"}]

update_data = [{'ID': '1', 'Lookup_field': [100,101]}]

update_data = [{'ID': '1', 'Lookup_field': "100", 'Lookup_field': "101"}]

After looking into the source code this is not possible to do without making changes. This is what I learned:

The value in the field must be a string. Removing the str() in the code below will make the API return a 500 Server Error.

for key, value in row.items():
    field = etree.SubElement(method, "Field")
    field.set("Name", key)
    field.text = str(value)

Adding field.set("Type" "lookup), together with using the double (;#;#) delimiter in between the GUIDs will not fix the problem.

update_data = [{'ID': '1', 'Lookup_field': "100;#;#101"}]

for key, value in row.items():
    field = etree.SubElement(method, "Field")
    field.set("Name", key)
    field.set("Type" "lookup")
    field.text = str(value)

The code above gets converted into this XML before sending the request to SharePoint:

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
        <ns1:UpdateListItems>
            <ns1:listName>My_SP_List</ns1:listName>
            <ns1:updates>
                <Batch one rror="Continue" ListVersion="1">
                    <Method ID="1" Cmd="Update">
                        <Field Name="ID">1</Field>
                        <Field Name="Lookup_field" Type="Lookup">
                            "100;#;#101"
                        </Field>
                    </Method>
                </Batch>
            </ns1:updates>
        </ns1:UpdateListItems>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I also tried modifying the XML before sending the request to SharePoint. These are changes to the XML that I have tried without success:

Changing the name of the tag from Field to LookupColumn and to FieldLookupValueCollection resulted in a 500 Server Error.

<LookupColumn Name="Lookup_field">"100;#;#101"</LookupColumn>

<FieldLookupValueCollection Name="Lookup_field">
    "100;#;#101"
</FieldLookupValueCollection>

Nesting Field tags, produce no errors but SP column was not updated as intended (I also tried with the GUID type attribute, rather than Choice and removing all attributes as well):

<Field Name="Lookup_field" Type="Lookup">
    <Field Name="Lookup_field" Type="Choice">"100"</Field>
    <Field Name="Lookup_field" Type="Choice">"101"</Field>
</Field>

If you have any suggestions, please let me know and I will try them.

Thank you for your help!

CodePudding user response:

After a lot of hours of trial and error, I found that using the double (;#;#) divider in between the GUIDs actually works, but that if you previously tried adding the GUIDs using a single divider (;#) you need to clear all data in the field, before running the code.

To update multiple values on a lookup column in a SharePoint list using SharePlum, use this code:

sp_list = site.List('My_SP_List')
update_data = [{'ID': '1', 'Lookup_field': "100;#;#101"}]
sp_list.UpdateListItems(data=update_data, kind='Update')

Hopefully, this will save someone's time in the future.

  • Related