2

I am creating a SharePoint 365 list programmatically. For each field in the list, I am using list.Fields.AddFieldAsXml. Once the list is created, I can go to the list settings, pick a column and format it using JSON.

Is there a way to do that in the code, so that it doesn't have to be added manually to each column that requires formatting.

1 Answer 1

3

You need to set the CustomFormatter attribute to specify your custom JSON formatting of the column like below sample code:

var jsonSchema = "<your-json>";

string fieldSchema = "<Field Type='Text' Name='ProjectName' StaticName='ProjectName' 
DisplayName='Project Name' CustomFormatter=" + jsonSchema + "/>";
var simpleTextField = list.Fields.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType);
context.ExecuteQuery();

or if you want to update it PnP PowerShell, you can do that as below:

Connect-PnPOnline -Url 'https://<tenant>.sharepoint.com/sites/testSiteCollection'

Set-PnPField -Identity "ProjectName" -List "Custom List" -Values @{CustomFormatter = @'
 <your json>
'@
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.