关于Metadata Client Object Model 的一些操作

 一、查询指定的Termset及子项

技术分享
<script type="text/javascript" src="/Style%20Library/aaaa/Scripts/jquery-1.11.1.min.js" language="javascript"></script>
    <script type="text/javascript">RegisterSod("sp.taxonomy.js", "\u002f_layouts\u002f15\u002fsp.taxonomy.js?rev=l0dFB37050OQxbmrgu6z7Q\u00253D\u00253D");</script>

    <script type="text/javascript">
        $(document).ready(function () {
            var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";
            $.getScript(scriptbase + "SP.Runtime.js",
                function () {
                    $.getScript(scriptbase + "SP.js", function () {
                        $.getScript(scriptbase + "SP.Taxonomy.js", execOperation);
                    });
                }
            );
        });
        function execOperation() {
            //Current Context
            var context = SP.ClientContext.get_current();
            //Current Taxonomy Session
            var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
            //Term Stores
            var termStores = taxSession.get_termStores();
            //Name of the Term Store from which to get the Terms.
            var termStore = termStores.getByName("IdeaTracker_MMS");
            //GUID of Term Set from which to get the Terms.
            var termSet = termStore.getTermSet("bcf031d3-7eb2-433a-b5ff-b92398758431");
            var terms = termSet.getAllTerms();
            context.load(terms);
            context.executeQueryAsync(function () {
                var termEnumerator = terms.getEnumerator();
                var termList = "Terms: \n";
                while (termEnumerator.moveNext()) {
                    var currentTerm = termEnumerator.get_current();
                    termList += currentTerm.get_name() + "\n";
                }
                alert(termList);
            }, function (sender, args) {
                console.log(args.get_message());
            });
        }
    </script>
View Code

二、创建Term Group

技术分享
<script type="text/javascript">
        $(document).ready(function () {
            var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";
            $.getScript(scriptbase + "SP.Runtime.js",
                function () {
                    $.getScript(scriptbase + "SP.js", function () {
                        $.getScript(scriptbase + "SP.Taxonomy.js", execOperation);
                    });
                }
            );
        });

        function execOperation() {
            //Current Context
            var context = SP.ClientContext.get_current();
            //Current Taxonomy Session
            var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
            //Term Stores
            var termStores = taxSession.get_termStores();
            //Term Store under which to create the group.
            var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");
            //New Group with name and new GUID
            var newGroup = termStore.createGroup("New Group Name", "b300304a-1693-4629-a1c0-dff7bda644ff");
            context.load(newGroup);
            context.executeQueryAsync(function () {
                console.log(newGroup.get_name());
            }, function (sender, args) {
                console.log(args.get_message());
            });
        }
    </script>
View Code

三、创建Term Set

技术分享
<script type="text/javascript">
        $(document).ready(function () {
            var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";
            $.getScript(scriptbase + "SP.Runtime.js",
                function () {
                    $.getScript(scriptbase + "SP.js", function () {
                        $.getScript(scriptbase + "SP.Taxonomy.js", execOperation);
                    });
                }
            );
        });

        function execOperation() {
            //Current Context
            var context = SP.ClientContext.get_current();
            //Current Taxonomy Session
            var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
            //Term Stores
            var termStores = taxSession.get_termStores();
            //Term Store under which to create the Term Set.
            var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");
            //Get group by GUID
            var peopleGroup = termStore.getGroup("97eaa7b8-9778-4f61-acb3-7f47abba13c3");
            //Create New Term Set in Group with Name, New GUID and LCID
            var newTermSet = peopleGroup.createTermSet("New TermSet Name", "49dac247-d315-4065-8718-e8c3f50e7dcd", 1033);
            context.load(newTermSet);
            context.executeQueryAsync(function () {
                console.log(newTermSet.get_name());
            }, function (sender, args) {
                console.log(args.get_message());
            });
        }
    </script>
View Code

四、创建Term

技术分享
<script type="text/javascript">
        function execOperation() {
            //Current Context
            var context = SP.ClientContext.get_current();
            //Current Taxonomy Session
            var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
            //Term Stores
            var termStores = taxSession.get_termStores();
            //Term Store under which to create the term.
            var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");
            //Term Set under which to create the term.
            var termSet = termStore.getTermSet("b49f64b3-4722-4336-9a5c-56c326b344d4");
            //Name of the term, LCID and a new GUID for the term.
            var newTerm = termSet.createTerm("India", 1033, "b49f64b3-4722-4336-9a5c-56c326b344a9");
            //newTerm.set_isAvailableForTagging(true);
            context.load(newTerm);
            context.executeQueryAsync(function () {
                alert("Term Created: " + newTerm.get_name());
            }, function (sender, args) {
                console.log(args.get_message());
            });
        }
    </script>
View Code

五、获取单一值的字段

技术分享
<script type="text/javascript">
        function execOperation() {
            var context = SP.ClientContext.get_current();
            var list = context.get_web().get_lists().getByTitle(TaxonomyCustomList);
            var listItem = list.getItemById(1);
            context.load(listItem);
            context.executeQueryAsync(function () {
                //Single Value Taxonomy Column
                var label = listItem.get_item("MyTaxColumn").get_label();
                //Term GUID
                var termGUID = listItem.get_item("MyTaxColumn").get_termGuid();
                //Type ID
                var typeID = listItem.get_item("MyTaxColumn").get_typeId();
                //WSS ID
                var wssID = listItem.get_item("MyTaxColumn").get_wssId();
            }, function (sender, args) {
                console.log(args.get_message());
            });
        }
    </script>
View Code

六、获取多选值的字段

技术分享
<script type="text/javascript">
        function execOperation() {
            var context = SP.ClientContext.get_current();
            var list = context.get_web().get_lists().getByTitle(TaxonomyCustomList);
            var listItem = list.getItemById(1);
            context.load(listItem);
            context.executeQueryAsync(function () {
                //Multivalue Taxonomy Column
                var taxEnumerator = listItem.get_item("MyTaxColumn").getEnumerator();
                while (taxEnumerator.moveNext()) {
                    //Label
                    var currentTerm = taxEnumerator.get_current();
                    //Label
                    var label = currentTerm.get_label();
                    //Term GUID
                    var termGUID = currentTerm.get_termGuid();
                    //Type ID
                    var typeID = currentTerm.get_typeId();
                    //WSS ID
                    var wssID = currentTerm.get_wssId();
                }
            }, function (sender, args) {
                console.log(args.get_message());
            });
        }
    </script>
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。