I want to periodically add choices to a Sharepoint List with Python, but when I do so the choices are formatted in the same way. I tried to add random colors by
-
Generating the json schema
unique_values = df['Department'].unique() colors = {value: generate_random_hex_color() for value in unique_values} # Define the base structure of the JSON schema json_schema = { "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "@currentField", "style": { "color": "" } } # Create the condition string for colors color_conditions = [] for value, color in colors.items(): color_conditions.append(f"@currentField == '{value}' ? '{color}'") color_conditions_str = " : ".join(color_conditions) + " : 'green'" # Add the color conditions to the JSON schema json_schema["style"]["color"] = f"=if({color_conditions_str})" # Add other static styles json_schema["style"].update({ "background-color": "#f8f8f8", "border": "1px solid", "box-sizing": "border-box", "padding": "4px 8px 5px", "border-radius": "16px", "margin": "4px" }) # Convert the JSON schema to a string json_schema_str = json.dumps(json_schema, indent=2)
Which results into something like that
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": "@currentField",
"style": {
"color": "=if(@currentField == 'USER1' ? '#e1ce9c' : @currentField == 'USER2' ? '#4d43ae' : @currentField == 'USER3' ? '#8747aa' :
- Applying it to the list
sp_list = ctx.web.lists.get_by_title(list_name)
field = sp_list.fields.get_by_title(column_name)
ctx.load(field)
ctx.execute_query()
# Update the field's custom formatter
field.CustomFormatter = json_schema_str
field.update()
ctx.execute_query()
I tried various alterations for hours now, without any effect, the style of the entries never changed when I viewed the list, it’s always a white blackground with grey font. Help please!!