Search code examples
sharepointsharepoint-2013crudsite-column

Sharepoint 2013: CRUD operations on Site Columns


So far, I'm able to perform CRUD operations on Lists, I was wondering if it is possible to do it on Site Columns, I've try to find it on the web but I literally found nothing, I dediced to drop a question here just to understand if it is possible or not, in case it is possible can please anyone provide me some sources so I can learn? thanks in advance.


Solution

  • This is doing exactly what you need, I've done it for work as well couple of weeks ago.

    var ctx;
            var web;
            var fieldChoice;
            var fieldName;
            var values;
    
            $(function () {
                SP.SOD.executeOrDelayUntilScriptLoaded(Function.createDelegate(this, function () {
                    fieldName = $('#dropdown').find(":selected").text();
                    populateValues(fieldName);
                }), 'SP.js');
            });
    
            function selection() {
                fieldName = $('#dropdown').find(":selected").text();
                populateValues(fieldName);
            }
    
            function populateValues(fieldName) {
                ctx = SP.ClientContext.get_current();
                web = ctx.get_web();
                fieldChoice = ctx.castTo(web.get_availableFields().getByTitle(fieldName), SP.FieldChoice);
                ctx.load(this.fieldChoice);
                ctx.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
            }
            /* Displays the vaules of the array in the textarea */
            function OnLoadSuccess() {
                values = fieldChoice.get_choices();
                $("textarea#textareadisplay").val(values.join("\n"));
    
            }
    
            function OnLoadFailed(e, args) {
                alert();
            }
    
            /* Push the textarea values in an array */
            function addItemsToColumns() {
                values = $('textarea#textareadisplay').val().split('\n');
                columnSpaceDelete();
                updateFieldChoice();
            }
    
            /* Function to delete empty values in the array */
            function columnSpaceDelete() {
                for (x = 0; x <= values.length - 1; x++) {
                    var a = values.indexOf("");
                    if (a !== -1) {
                        values.splice(a, 1);
                    }
                }
            }
    
            /* Update the columns values whit the values in the array */
            function updateFieldChoice() {
                fieldChoice.set_choices(values);
                fieldChoice.update();
                ctx.executeQueryAsync(function () { }, function () { });
            }
    

    And the relative HTML:

    <select id="dropdown" name="dropdown" onchange="selection()">
                <option value="EngineType_Cylinders">EngineType_Cylinders</option>
                <option value="EngineType_EngineCycle">EngineType_EngineCycle</option>
                <option value="EngineType_EngineFamily">EngineType_EngineFamily</option>
                <option value="EngineType_Euro">EngineType_Euro</option>
                <option value="EngineType_FamilyEvolution">EngineType_FamilyEvolution</option>
                <option value="EngineType_GasEmissionLevel">EngineType_GasEmissionLevel</option>
                <option value="EngineType_Power">EngineType_Power</option>
                <option value="EngineType_PowerSupply">EngineType_PowerSupply</option>
                <option value="EngineType_Use">EngineType_Use</option>
            </select><br />
    
            <textarea id="textareadisplay"></textarea><br />
            <input type ="button" id="updatebtn" value="Update values" onclick="addItemsToColumns()" />