Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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);
        }
    });

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top