Skip to content

Need to add item to SharePoint List with lookup column #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pavanbangad opened this issue Jan 13, 2022 · 8 comments
Closed

Need to add item to SharePoint List with lookup column #459

pavanbangad opened this issue Jan 13, 2022 · 8 comments
Labels

Comments

@pavanbangad
Copy link

Hi,

I would like to add a item to a list in which few of the columns are lookup columns. do you have any code sinppet to perfrom it?

@Dskpupper
Copy link

Hey , were u able to read files from item list?

@pavanbangad
Copy link
Author

Yes, I able to read the data from sharepoint list

@pavanbangad pavanbangad changed the title Need to add item to Sharepoint with lookup column Need to add item to SharePoint List with lookup column Jan 13, 2022
@vgrem vgrem added the question label Jan 19, 2022
@vgrem
Copy link
Owner

vgrem commented Jan 19, 2022

Hey,

the following examples demonstrates how to set lookup field value:

list_tasks = ctx.web.lists.get_by_title("Tasks")
items = list_tasks.items.get().top(1).execute_query()
if len(items) == 0:
    sys.exit("No items was found")


field_value = FieldLookupValue(lookup_id)
items[0].set_property(lookup_field_name, field_value).update().execute_query()

and multi lookup field value:

field_value = FieldMultiLookupValue()
field_value.add(FieldLookupValue(lookup_id))
items[0].set_property(multi_lookup_field_name, field_value).update().execute_query()

@vgrem vgrem closed this as completed Jan 19, 2022
@lealcastillo1996
Copy link

I think this is not working in the most recent version:

indexes_look = dict_lookup_fields[field_name][index]  # List with the IDs of the items to be added
                print(indexes_look)
                if indexes_look and isinstance(indexes_look, list) and len(indexes_look) > 0:
                    field_value = FieldMultiLookupValue()
                    for lookup_id in indexes_look:
                        field_value.add(FieldLookupValue(lookup_id=lookup_id))

            
                    item.set_property(field_name, field_value).update()
                    ctx.execute_query()

And getting back
('-1, Microsoft.SharePoint.Client.InvalidClientQueryException', "Incompatible type kinds were found. The type 'Collection(Edm.Int32)' was found to be of kind 'Collection' instead of the expected kind 'Primitive'.", "400 Client Error: Bad Request for url: [https:...

@bizzinho
Copy link

I have the same issue @lealcastillo1996 :) were you able to fix it?

@lealcastillo1996
Copy link

Hey @bizzinho — unfortunately not. This is my most recent post where I reported the bug:
#946

I’ve tried multiple libraries and notations for posting the JSON directly, spending hours and hours on it, but nothing worked. It seems like the Microsoft backend notation has changed, and there’s not much documentation available about it.

If @vgrem and the team manage to solve this one, they honestly deserve a monument!

@bizzinho
Copy link

bizzinho commented Apr 16, 2025

Thanks @lealcastillo1996 , not sure this will help you, but in my case the problem seems to have been something with the list itself i found out today. Even manually I wasn't able to add more than one lookup element to fields. The items would simply disappear! The behavior stopped when I toggled the "multiple values" switch in the sharepoint UI off and on again. Suddenly the elements would stay after adding and the above code worked again....

@lealcastillo1996
Copy link

@bizzinho Thanks so much for the tip!. I did the same "multiple values" de-activation and activation back via code and it seems to work now, crazy hahaha. So I will call it problem solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants