Ecmascript Object Model

Source: Internet
Author: User

I studied ecmascript this afternoon and found that the advantage is that if there is no development environment in the production environment, this technology can be used because it has a JavaScript Object client model, record some temporarily used instances and methods!

The URL below is the URL of the Javascript classi library. You can learn about the entire architecture from the URL below

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

 

Currently, sp. JS is used. The reference JS Code below may be used in development. Generally,. Application page is not used.

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

Generally, a clientcontext is obtained first.

VaR clientcontext = new sp. clientcontext. get_current (); or var clientcontext = new sp. clientcontext ();

Obtain the Web

VaR web = clientcontext. get_web (); // obtain the current web

Listcollection

this.listCollection = web.get_lists();

List traversal of an instance

    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);    }

  

Column list

 var targetlist = clientContext.get_web().get_lists().getByTitle('testEcaml');
Targetlist. set_title ('new announcements'); // set the title

 

Listitem list item

This. Item = List. getitembyid (Itemid );

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

Fieldname is the internal name of the field.

VaR value = olistitem. get_item (fieldname );
Olistitem. set_item (fieldname, value );

VaR idvalue = olistitem. get_id (); // obtain the ID

Some common methods:

Olistitem. Update ();

Olistitem. refleshload ();

Olistitem. deleteobject ();

Olistitem. Recycle ();

 

Query example

<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>

 

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

List level

Create a list and add a record

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 can be found

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

List item level

Add new record

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()" />

Other pending .......

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.