Check if number field is formatted as percentage
-
04-10-2020 - |
Question
I am trying to figure out a clever way to tell if a field in a SharePoint (Online) list is marked as "Format as percentage".
Is there any way to do this? I can of course parse the SchemaXml property on the Field class for existance of
PERCENTAGE="TRUE"
So the code would be something like:
// see if the configuration is marked as percentage
if (spField.SchemaXml.ToLowerInvariant().Contains(@"percentage=""true"""))
{
}
But that does simply not feel right.
Any suggestions?
Thanks,
Jesper - Copenhagen
Solution
You can use the following JSOM to check if the number field is set to show as percentage.
var ctx = new SP.ClientContext(appweburl);
var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
ctx.set_webRequestExecutorFactory(factory);
var appctx = new SP.AppContextSite(ctx, hostweburl);
var field = appctx.get_web()
.get_lists()
.getByTitle('My List')
.get_fields()
.getByInternalNameOrTitle("My Field");
ctx.load(field, "SchemaXml");
ctx.executeQueryAsync(function () {
var schema = field.get_schemaXml();
if (schema.toLowerCase().indexOf("percentage=false") > 0) {
// .... Field is set as percentage
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange