zoukankan      html  css  js  c++  java
  • ECMAScript Query实例

    下面是Query的一个实例 ,用通过查询Title中包含要查询的值,下面是页面的代码

    View Code
    <%@ Assembly Name="ClientOMUIActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4efe8d1641f2f2df" %>
    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
    <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
    <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    <%@ Import Namespace="Microsoft.SharePoint" %> 
    <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ClientObjAddItemUserControl.ascx.cs" Inherits="ClientOMUIActions.ClientObjAddItem.ClientObjAddItemUserControl" %>
     <sharepoint:ScriptLink ID="ScriptLink1" Name="SP.js" runat="server" OnDemand="true" Localizable="False"></sharepoint:ScriptLink>
    <style>
        body
        {
             font-family:Verdana;
             font-size:12px;
        }
    .btn1
    {
        font-family: "Verdana";
        font-size: 9pt;
        color: #000000;
        border-color: #666666 #666666 #666666 #666666;
        border-style: solid;
        border-top- 1px;
        border-right- 1px;
        border-bottom- 1px;
        border-left- 1px;
         60px;
        cursor: hand;
    }
    #list
    {
       margin-top:20px;
    }
    #list table
    {
       100%
    }
    #list table tr th
    {
       background-color:#6660FF;
       text-align:certer;
       font-size:12px;
       line-height:24px;
       padding:1px 8px;
       color:#ffffff;
       border:1px solid #808080;
    }
    #list table tr td
    {
          border:1px solid #E7E7E7; 
          line-height:20px;
          height:20px;
          padding:1px 8px;
          background-color:#f5f5f5;
          font-size:12px;
    }
    </style>
    <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());
        }
    
    
        function GetItemByID() {
            var clientContext = new SP.ClientContext();
            var olist = clientContext.get_web().get_lists().getByTitle('testEcaml');
            this.olistItem = olist.getItemById(1);
            clientContext.load(olistItem);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.OnSucceed),
            Function.createDelegate(this, this.onQueryFailed));
        }
        function OnSucceed() {
    
            alert('testEcaml created!\n\nId: ' + olistItem.get_id() + '\nTitle: ' + olistItem.get_item('Title'));
        }
    
        function Search() {
    
            var searchValue = document.getElementById("txtSearch").value;
            if (searchValue == "") return;
            var clientContext = new SP.ClientContext();
            var olist = clientContext.get_web().get_lists().getByTitle('testEcaml');
            var camlQuery = new SP.CamlQuery();
            var sCamlString = "<View><ViewFields><FieldRef Name='Title'/><FieldRef Name='Author'/><FieldRef Name='Created'/></ViewFields><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + searchValue + "</Value></Contains></Where></Query></View>";
            //document.getElementById("EM").innerHTML = sCamlString;
            camlQuery.set_viewXml(sCamlString);
            this.items = olist.getItems(camlQuery);
            clientContext.load(items);
    
            clientContext.executeQueryAsync(Function.createDelegate(this, this.OnQueryS),
             Function.createDelegate(this, this.onQueryFailed));
    
            // alert("Hello");
        }
        function OnQueryS(sender, args) {
    
            var sTable = "<table><tr><th>Title</th><th>Author</th><th>Created</th></tr>";
    
            var listItemEnumerator = items.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                sTable + "<tr>";
    
                var listitem = listItemEnumerator.get_current();
    
                sTable += "<td>" + listitem.get_item('Title') + "</td>";
    
                sTable += "<td>" + listitem.get_item('Author')["$b_1"] + "</td>";
                // var obj=listitem.get_item('Author');
                //  for(var o in obj)
                //  {
                //   alert("Key:"+o.toString()+",Value:"+obj[o]);
                //}
                // alert(Object.prototype.toString.apply(listitem.get_item('Author')));
                var date = new Date(listitem.get_item('Created'));
                sTable += "<td>" + date.toLocaleString() + "</td>";
                sTable += "</tr>";
    
            }
            sTable += "</table>";
            document.getElementById("list").innerHTML = sTable;
    
            //alert('testEcaml created!\n\nId: ' + olistItem.get_id() + '\nTitle: ' + olistItem.get_item('Title'));
        }
        
    </script>
    <div id="EM"></div>
        <div><input type="text" id="txtSearch" style=" 250px;" /> &nbsp; <input type="button"  value="Search" id="btnSearch" onclick="Search();" class="btn1"/></div>
        <input id="Button1" type="button" value="Run Code" onclick="runCode()"  style=" display:none"/>
        <input id="btnGetItem" type="button" value=" Get Item By ID" onclick="GetItemByID()" style=" display:none"/>
        <div id="list">
        </div>

    效果图:

    注意:

    1. 值得注意的一点是,取Author值,是怎么样取的通过Obj["$b_1"],这样取对象取出来的,可能是返回的是Json数据,如果想看其他数据,需要遍历。

    2. 现在暂时才知道用InnerFieldName来得到值 ,用DisplayName暂时不清楚。

    备注:

    下面是Author字段遍历后的结果

    Key:customWriteToXml,Value:function(){ULSnd3:;return false}
    Key:writeToXml,Value:function(b,a){ULSdih:;if(!b)throw Error.argumentNull("writer");if(!a)throw Error.argumentNull("serializationContext");var c=["LookupValue","LookupId"];SP.DataConvert.writePropertiesToXml(b,this,c,a)}
    Key:get_typeId,Value:function(){ULSdih:;return "{c956ab54-16bd-4c18-89d2-996f57282a6f}"}
    Key:set_lookupId,Value:function(a){ULSdih:;this.$L_1=a;return a}
    Key:get_lookupId,Value:function(){ULSdih:;return this.$L_1}
    Key:get_lookupValue,Value:function(){ULSdih:;return this.$b_1}
    Key:$4D,Value:function(a){ULSdih:;this.$b_1=a}
    Key:customFromJson,Value:function(){ULSnd3:;return false}
    Key:fromJson,Value:function(a){ULSnd3:;this.initPropertiesFromJson(a)}
    Key:initPropertiesFromJson,Value:function(b){ULSdih:;var a;a=b.LookupValue;if(!SP.ScriptUtility.isUndefined(a)){this.$b_1=a;delete b.LookupValue}a=b.LookupId;if(!SP.ScriptUtility.isUndefined(a)){this.$L_1=a;delete b.LookupId}}
    Key:$b_1,Value:系统帐户
    Key:$L_1,Value:1073741823
  • 相关阅读:
    迎接2019多校联合新生训练赛(2018/12/31)
    Educational Codeforces Round 57 (Rated for Div. 2) 前三个题补题
    睡学预习(4)
    睡学预习(3)
    Python学习笔记(3)动态类型
    Maya Calendar POJ
    睡学预习(2)——数列极限
    2018-2019赛季多校联合新生训练赛第八场(2018/12/22)补题题解
    蓝桥杯入门题:Hello, world!
    蓝桥杯:P1103
  • 原文地址:https://www.cnblogs.com/gzh4455/p/2490558.html
Copyright © 2011-2022 走看看