Search code examples
sharepoint-onlineoffice365-rest-client

Office365-REST-Python-Client seems unable to update Lookup Columns in Sharepoint


I managed to follow another users question about uploading files and set metadata here :- Office365-REST-Python-Client 401 on File Update

And I am now able to do what it says on the tin and upload files and set metadata. However I'm struggling when using the following code, kindly offered up by one of the contributors to the question I linked to above. (Vadim I think).

 list_item = target_file.listitem_allfields # get associated list item 
 list_item.set_property("Title", "New title")
 list_item.update()
 ctx.execute_query()

This works fine for normal metadata, but I have a column which is a lookup column, and I want to update that one.

If I use the Shareplum library, I can do this by setting the column to be the ID of the list item the lookup column points to, and that works fine.

So I tried using :-

list_item.set_property("Title",1)

...... as the ID of the lookup list item was 1, but this doesn't work using the Office365-REST-Python-Client package.

Is there a way to set a lookup column using this package? I tried using an ID, and also the Text value, but nothing seemed to work.

I'd much rather use this package than Shareplum, as Shareplum seems deficient in other ways.

Any pointers would be much appreciated.


Solution

  • The answer to this was fairly straightforward -

    field_value = FieldLookupValue(lookup_id=1)
    item_to_update.set_property("Department", field_value).update().execute_query()