Sharepoint 2013: CRUD operations on Site Columns

383 Views Asked by At

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.

2

There are 2 best solutions below

2
FabioEnne On BEST ANSWER

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()" />
3
Max On

On MSDN site you can find useful infos about CSOM:

How to: Complete basic operations using JavaScript library code in SharePoint 2013 How to: Complete basic operations using JavaScript library code in SharePoint 2013

And on the second link you can find clues about CRUD operation on Lists

If you want to perform operations on settings of Site Column you can see on MSDN FieldCollection methods.

There also equivalent namespace for javascript.

basically you can add Site Column with AddFieldAsXml, you can Update/Delete using method GetByID (or Title or InternalName) and perform operation on returned Field.