IT tutorials
 
Technology
 

Sharepoint 2013 : Overview of The Client-Side Object Model and Rest APIs - JAVASCRIPT (part 2) - Querying

4/25/2014 3:39:47 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Querying

Similarly to the Managed CSOM, the JS CSOM also includes built-in batching and filtering. These features are especially important in client applications where the browser executing the code is always remote and often on an unknown quality of connection. To ensure your application stays responsive and fast you must take measures to make as few requests as possible and only return the data you really need.

Prior to making any requests you must set up a ClientContext. To get the context associated with the site and page the script is running on, use the get_Current() method:

var clientContext = context = new SP.ClientContext.get_current();

You can then start requesting data. One of the simplest requests you can make is for the information about the site, like so:

var web;
web = clientContext.get_web();
clientContext.load(web);

Now you are ready to make the request to the server. Because you don’t want to block the application thread processing while this potentially long-running task is in progress, you provide callback functions that will be called either after the operation completes or if an error occurs.

To do this, make a call to executeQueryAsync like this:

clientContext.executeQueryAsync(
myQuerySucceeded,
myQueryFailed);

In the myQuerySucceeded function you can start using the objects you asked for:

function onQuerySucceeded() {alert(this.web.Title);}

Additionally, your application should be able to handle when errors occur. This might be due to connectivity issues, for example, like the following:

function myQueryFailed(sender, args) {alert('Call to SharePoint failed :(');}

When querying lists and libraries you have the choice of querying for a specific item whose ID you already know using the getItemById(id) method, or with a query using the getItems(query) method. The latter is slightly more complex in that you need to set up the query against that list using CAML syntax. The following sample code shows querying a simple list for all items whose Title column equals “Foo”:

    var list = listContext.get_web().get_lists().getByTitle('My Custom List');

var camlQuery = new SP.CamlQuery();

camlQuery.set_viewXml(
'<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' +
'<Value Type=\'Text\'>New Item 1</Value></Eq></Where></Query>' +
'<RowLimit>10</RowLimit></View>' );

items = list.getItems(camlQuery);

context.load(items);
context.executeQueryAsync(myQuerySucceeded, myQueryFailed);

You can use the CAML query to carefully construct the exact query against the list you want to make and also the data you want to bring back.

To create and manipulate list items, use the ListItemCreationInformation object as shown in the following code example. This gives you a context object that allows you set up the item prior to sending it to the server along with the data you want to create:

var newListItem;

function createItems()
{
var listContext = new SP.AppContextSite(context, hostUrl);
var list = listContext.get_web().get_lists().getByTitle('My Custom List');

var itemCreateInfo = new SP.ListItemCreationInformation();
newListItem = list.addItem(itemCreateInfo);
newListItem.set_item('Title', 'Created via JS CSOM!');
newListItem.update();
context.load(newListItem);
context.executeQueryAsync(onCreateListItemsSuccess,onCreateListItemsFail);
}

function onCreateListItemsSuccess() {
alert('New ListItem created: ' + newListItem.get_id());
}

// This function is executed if the above call fails
function onCreateListItemsFail(sender, args) {
alert('Failed to create list item. Error:' + args.get_message());
}

For updates you simply get the list item in question, make the update to the data, and then call update() on it like so:

var listItem = list.getItemById(1);
listItem.set_item('Title', 'Updated via JS CSOM');
listItem.update();

You can try out these APIs and techniques in the following exercise to get a better feel for how they work with SharePoint Online using the new Napa developer tools.


 
Others
 
- Sharepoint 2013 : Overview of The Client-Side Object Model and Rest APIs - JAVASCRIPT (part 1) - Setup
- Active Directory 2008 Optimization and Reliability : Monitoring and Troubleshooting Active Directory Components (part 4) - The Event Viewer
- Active Directory 2008 Optimization and Reliability : Monitoring and Troubleshooting Active Directory Components (part 3) - The Network Monitor, The Task Manager
- Active Directory 2008 Optimization and Reliability : Monitoring and Troubleshooting Active Directory Components (part 2) - Monitoring Active Directory Performance with Performance Monitor
- Active Directory 2008 Optimization and Reliability : Monitoring and Troubleshooting Active Directory Components (part 1) - Monitoring Domain Controller Performance
- Sharepoint 2013 : Overview of The Client-Side Object Model and Rest APIs - MANAGED CODE (.NET)
- Sharepoint 2013 : Overview of The Client-Side Object Model and Rest APIs - CLIENT-SIDE OBJECT MODEL (CSOM) BASICS
- Windows Server 2012 : Managing virtual machines (part 5) - Monitoring virtual machines
- Windows Server 2012 : Managing virtual machines (part 4) - Managing snapshots
- Windows Server 2012 : Managing virtual machines (part 3) - Optimizing virtual disks
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us