3

I am able to successfully create a new List column using the SharePoint REST API with the following JSON:

{"Title":"My Title",
 "StaticName":"MyTitle",
 "Required":"false",
 "FieldTypeKind":9,
 "__metadata":{"type":"SP.FieldNumber"}
}

However, when I try to set the ShowAsPercentage property like this

{"Title":"My Title",
 "StaticName":"MyTitle",
 "Required":"false",
 "FieldTypeKind":9,
 "ShowAsPercentage":"true",
 "__metadata":{"type":"SP.FieldNumber"}
}

I get the following error:

{"error":
    {"code":"-1,Microsoft.SharePoint.Client.InvalidClientQueryException",
     "message":
        {"lang":"en-US",
         "value":"The property 'ShowAsPercentage' does not exist on type 'SP.FieldNumber'. 
                  Make sure to only use property names that are defined by the type."
        }
    }
}

It seems pretty clear from the MSDN documentation that ShowAsPercentage is a valid property on SP.FieldNumber. Can anyone shed some light on this for me?

I have tried using the Call HTTP Web Service workflow action (my ultimate aim for this project), JavaScript (see @vishal-m-patil answer below) and sending the request from a REST API test client (specifically the Restlet Chrome extension) but all get the same 'ShowAsPercentage does not exist' error.

  • Please add the entire ajax request code – Priyaranjan KS Aug 7 '17 at 15:54
  • can you try it as "ShowAsPercentage":true, , without double quotes around true ? – Gautam Sheth Aug 7 '17 at 17:22
  • @PriyaranjanKS -- I'm not using ajax in this case but Call HTTP Web Service workflow action. – 5eleven7 Aug 7 '17 at 17:57
  • @GautamSheth -- I've tried string and boolean and get the same error message. – 5eleven7 Aug 7 '17 at 17:58
  • What's your SharePoint version? – Atish Dipongkor - MVP Aug 9 '17 at 7:27
3

ShowAsPercentage does not work in SharePoint 2013. It is supported in SharePoint Online only.

You can set it in SchemaXml. Try with following request body.

{
  "__metadata": {
    "type": "SP.FieldNumber"
  },
  "Required": false,
  "SchemaXml": "<Field Type=\"Number\" DisplayName=\"{Your Field Name}\" Required=\"FALSE\" EnforceUniqueValues=\"FALSE\" Indexed=\"FALSE\" Percentage=\"TRUE\"/>",
  "Title": "Your Field Name",
  "FieldTypeKind": 9
}

NB: Replace {Your Field Name} by yours.

  • I confirmed this is working for me using all three methods: JavaScript, Call HTTP Web Service action, and REST API Client. Thank you! – 5eleven7 Aug 9 '17 at 19:07
  • Also, just to note, when you are building your request body for the Call HTTP Web Service action, leave out the backslashes in the SchemaXml value. SharePoint escapes the quotations for you. – 5eleven7 Aug 9 '17 at 19:21
3

Try below solution, It should help (Tested & Working)

function createField() {
    var webUrl = _spPageContextInfo.webAbsoluteUrl;
    var formdigestValue = $("#__REQUESTDIGEST").val();
    var fieldMetadata = { __metadata: { 'type': 'SP.FieldNumber' }, 'FieldTypeKind': 9, 'Title': 'MyNumerField', 'ShowAsPercentage': true };
    $.ajax
    ({
        url: webUrl + "/_api/web/lists/getbytitle('Testlist')/fields",
        type: "POST",
        data: JSON.stringify(fieldMetadata),
        headers:
        {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": formdigestValue,
            "content-type": "application/json;odata=verbose",
        },

        success: function (data, status, xhr) {
            alert("field created");
        },
        error: function (data, errorCode, errorMessage) {
            alert("Error while field creation: " + errorMessage);
        }
    });

}
  • I tried your code and got this error The property 'ShowAsPercentage' does not exist on type 'SP.FieldNumber'. Make sure to only use property names that are defined by the type. – 5eleven7 Aug 8 '17 at 16:32
  • can u please post your full code? – SP Developer Aug 9 '17 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.