简体   繁体   中英

How to create a site column type as choice using javascript object model in sharepoint 2013?

I need to create a site column of type choice. Which will contain "Yes" and "No" as two options and default value as "empty" using javascript object model. Can any one please suggest.Thanks in advance!!!!

Regards, Rajkishore

Take a look at this code :

<script>
$(document).ready(function(){
    $("#btnCreateSiteColumn").click(function(){
        var model = { 
            'parameters': { 
                '__metadata': { 'type': 'SP.FieldCreationInformation' }, 
                'Title':'My Choice Column', 
                'FieldTypeKind': 6, 
                'Required': true, 
                'Choices': { 
                    '__metadata': { 'type':'Collection(Edm.String)' }, 
                    'results': [ 'Yes', 'No' ] 
                } 
            } 
        };

        $.ajax({
            url: 'http://Url.Of.Your.Site.Collection/_api/web/fields/addfield',
            type: "POST",
            data: JSON.stringify(model),
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function(data) {
                alert('Site column created');
            },
            error: function(error) {
                alert('Error creating site column');
            }
        });
    }); 
});
</script>
<a id="btnCreateSiteColumn">Create Site Column</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM