简体   繁体   English

Sharepoint 2013:网站栏上的CRUD操作

[英]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? 到目前为止,我能够在列表上执行CRUD操作,我想知道是否可以在“网站栏”上进行此操作,我尝试在网络上找到它,但实际上我什么都没找到,我只想提出一个问题这里只是为了了解是否有可能,如果有可能请任何人向我提供一些资源以便我学习? thanks in advance. 提前致谢。

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: 和相对的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()" />

On MSDN site you can find useful infos about CSOM: 在MSDN站点上,您可以找到有关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 如何:在SharePoint 2013中 使用JavaScript库代码完成基本操作如何:在SharePoint 2013中使用JavaScript库代码完成基本操作

And on the second link you can find clues about CRUD operation on Lists 在第二个链接上,您可以找到有关列表上CRUD操作的线索

If you want to perform operations on settings of Site Column you can see on MSDN FieldCollection methods . 如果要对“网站栏”的设置执行操作,则可以在MSDN FieldCollection方法上看到。

There also equivalent namespace for javascript. 还有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 . 基本上,您可以使用AddFieldAsXml添加Site Column,可以使用方法GetByID (或Title或InternalName)进行更新/删除,并对返回的Field执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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