• ECMAScript 对象模型


    今天下午研究了一下ECMAScript,暂时发现有一个好处是,如果生产生境的过程中,没有开发环境,可以用到这个技术,因为这个有Javascript Object Client Model,暂时把一些暂时用过的实例及方法做一下记录!

    下面的网址是Javascript Classi Library的地址,你可以从以下网址了解其整个架构

    http://msdn.microsoft.com/en-us/library/ee538253.aspx

    暂时用到是SP.js, 下面的引用js代码有可能会在开发中用到,一般情况下. Application Page 都不会用到。

    <sharepoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" Localizable="False"></sharepoint:ScriptLink>


    一般来说首先会得到一个ClientContext

            var clientContext = new SP.ClientContext.get_current();
    或者
          var clientContext = new SP.ClientContext();

    得到web

    var web = clientContext.get_web(); //得到当前Web

    列表集合listCollection

    this.listCollection = web.get_lists();

    列表遍历的一个实例

        function onQuerySucceeded() {
            var listInfo = 'Lists on the current site:' + '\n\n';
            var listEnumerator = this.listCollection.getEnumerator();
            while (listEnumerator.moveNext()) {
                var list = listEnumerator.get_current();
                listInfo += list.get_title() + '\n';
            }
            alert(listInfo);
        }
    

      

    列list

     var targetlist = clientContext.get_web().get_lists().getByTitle('testEcaml');
    targetlist.set_title('New Announcements'); //设置Title

    listItem列表项

    This.item = list.getItemById(itemId);

    var value = SP.ListCollection.get_item(index);

    fieldName都是Field的内部名称

    var value = olistItem.get_item(fieldName);
    olistItem.set_item(fieldName, value);

    var idValue= olistItem.get_id(); //取Id

    一些常见方法:

    olistItem.update();

    olistItem.refleshload();

    olistItem.deleteObject();

    olistItem.recycle();

    query 例子

    <script type="text/javascript">
        function GetItemTest(queryId) {
            clientContext = SP.ClientContext.get_current();
            var list = clientContext.get_web().get_lists().getByTitle('#Your List Name#');
            var camlQuery = new SP.CamlQuery();
            var strCaml = "<View><ViewFields><FieldRef Name='LinkTitle'/><FieldRef Name='Field1' /><FieldRef Name='Field2'/></ViewFields><Query><Where><Eq><FieldRef Name='LinkTitle' /><Value Type='Text'>" + queryId + "</Value></Eq></Where></Query></View>";
            camlQuery.set_viewXml(strCaml);
            this.collListItem = list.getItems(camlQuery);
            clientContext.load(collListItem);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
        function onQuerySucceeded(sender, args) {
            var listItemEnumerator = collListItem.getEnumerator();
    
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                var item = oListItem.get_item('Field1');
            }
        }
    
        function onQueryFailed(sender, args) {
            alert('Request failed' + args.get_message() + '\n' + arg.get_stackTrace());
        }
    
    
        var collListItem = null;
        var clientContext = null;
    </script>

     具体详细地址:http://msdn.microsoft.com/en-us/library/ee552478.aspx

    列表级别

    创建列表,并添加了一条记录

    View Code
     function createList(listName) {
            //Create client context. 
            var clientContext = new SP.ClientContext();
            var oWebsite = clientContext.get_web();
    
            //Let's create list creation information object
            var listCreationInfo = new SP.ListCreationInformation();
            listCreationInfo.set_title(listName);
            listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
            listCreationInfo.set_quickLaunchOption(SP.QuickLaunchOptions.on);
    
            this.oList = oWebsite.get_lists().add(listCreationInfo);
    
            //Let's create also new item to the list to be created 
            var itemCreateInfo = new SP.ListItemCreationInformation();
            this.oListItem = oList.addItem(itemCreateInfo);
            oListItem.set_item('Title', 'Example item for ' + listName);
            oListItem.set_item('Body', 'Hello seminar audience. From list ' + listName);
            oListItem.update();
    
            clientContext.load(oListItem);
            clientContext.load(oList);
    
            //Execute the actual script 
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }

    SP.ListTemplateType Enumeration 可以参考以下网址

    http://msdn.microsoft.com/zh-cn/library/ee549420.aspx

    列表项级别

    添加新的记录

    View Code
    <script type="text/javascript">
    
     function runCode() {
     
         var clientContext = new SP.ClientContext();
         var targetlist = clientContext.get_web().get_lists().getByTitle('testEcaml');
         var itemCreateInfo = new SP.ListItemCreationInformation();
         this.newItem = targetlist.addItem(itemCreateInfo);
         newItem.set_item('Title', 'test');
         newItem.update();
         clientContext.load(newItem);
         clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),
         Function.createDelegate(this, this.onQueryFailed));
    
     }
    
        function onQuerySucceeded() {
    
            alert('Announcement created!\n\nId: ' + newItem.get_id() + '\nTitle: ' + newItem.get_item('Title'));
        }
    
        function onQueryFailed(sender, args) {
    
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    
    </script>
    
        <input id="Button1" type="button" value="Run Code" onclick="runCode()" />


    其他的待续.......

  • 相关阅读:
    C#中使用My实现单例应用程序
    喝着啤酒学Python(2):第一个HelloWorld
    再读《精通css》04:盒模型和空白边叠加
    再读《精通css》07:圆角
    再读《精通css》08:阴影
    @ResponseBody 乱码
    再读《精通css》05:定位、浮动与清理
    关于javascript面向对象的一点思考
    再读《精通css》06:背景图片
    【求解释】关于第三方接口调用中安全的疑问
  • 原文地址:https://www.cnblogs.com/gzh4455/p/2475376.html
Copyright © 2020-2023  润新知