python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

Update_fields does not work on a field initially set to None

Open bip91 opened this issue 1 year ago • 3 comments

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

bip91 avatar Jun 24 '24 21:06 bip91

Is it posible to have a field in self.fields that is not in self._parent.column_name_cw.values()?

alejcas avatar Jul 04 '24 14:07 alejcas

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).

bip91 avatar Jul 05 '24 07:07 bip91

Can you make a PR with this changes? Thanks

alejcas avatar Jul 05 '24 07:07 alejcas