ECMAScript 物件模型

來源:互聯網
上載者:User

今天下午研究了一下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()" />

其他的待續.......

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.