python-o365
python-o365 copied to clipboard
Update_fields does not work on a field initially set to None
Hi,
in a Sharepoint list, if we have a field that is not initially set, it is not possible to update it to a new value.
Context : i = l.get_item_by_id(x) i.fields # > does not contain any "Status" field as "Status" is not set for this item i._parent.column_name_cw.value() # > but this contain "Status" value
in that case, update_field raise an error :
i.update_fields({'Status': 'Inactif'}) Traceback (most recent call last): File "
", line 1, in File "/home/bruno/src/o365/lib/python3.12/site-packages/O365/sharepoint.py", line 154, in update_fields raise ValueError('"{}" is not a valid internal field name'.format(field))
Explanation : This is due to the fact that self.fields exists, but not contains the field, and second condition is never tested.
def _valid_field(self, field):
# Verify the used field names are valid internal field names
valid_field_names = self.fields if self.fields \
else self._parent.column_name_cw.values() \
if self._parent \
else None
if valid_field_names:
return field in valid_field_names
Solution: I would suggest to switch the 2 conditions like this :
def _valid_field(self, field):
# Verify the used field names are valid internal field names
valid_field_names = self._parent.column_name_cw.values() if self._parent \
else self.fields if self.fields \
else None
# If no parent is given, and no internal fields are defined assume correct, API will check
if valid_field_names is None:
return True
return field in valid_field_names
Is it posible to have a field in self.fields that is not in self._parent.column_name_cw.values()?
I don't see any usual case, except perhaps when a field is added.
I have always seen at least self.fields or parent.column_name set but self.fields contain only fields that have values in Sharepoint list record. (A bit annoying to rely on that if we want to set a such field).
Can you make a PR with this changes? Thanks