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

Was it helpful?

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
scroll top