IT tutorials
 
Technology
 

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

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

Alongside the Managed .NET Client-Side Object Model (Managed CSOM) is the JavaScript Client Object Model (JS CSOM).


NOTE See the previous section, “Managed Code (.NET),” for background foundational information about the CSOM that isn’t repeated in this section.

The primary purpose of the JS CSOM is to allow JavaScript code running on pages within the context of SharePoint to talk back to SharePoint without requiring a full-page postback. It is not designed or intended for developers to use outside of the context of pages served from SharePoint.

Similar to the Managed CSOM, the JS CSOM also is built to batch requests to ensure performance. However, one of the fundamental differences is that the JS CSOM is designed with asynchronous calls and callbacks in mind. This ensures that transactions that take some time to complete don’t potentially block the calling thread, possibly impacting the UI of your application.

To access the JS CSOM the calling page must reference the following two files:

  • SP.Runtime.js
  • SP.js

Each of these is located in the /_layouts/15/ directory under each SharePoint site; for example, http://myserver/sitename/_layouts/15/SP.js.

You can also find these files on any SharePoint 2013 server in the following location:

%ProgramFiles%\Common Files\Microsoft Shared\
web server extensions\15\TEMPLATE\LAYOUTS

NOTE Although the JS CSOM libraries are available, sometimes simple REST/OData HTTP-based calls might be just as easy for you to use. If you are familiar with making REST/JSON-based calls then they might also be worth considering. However, you would lose some of the benefits of the CSOM, such as automatic batching.

Setup

By default, pages that run within the context of SharePoint typically have the appropriate JavaScript references already defined in the master page. This means you shouldn’t need to reference them in each page of your application. However, understanding what it means to be within the context of SharePoint is important. It means that the page is served from the same domain as SharePoint, rather than from another location such as in Azure. In cloud-hosted SharePoint apps, this is the default. Pages are loaded and served from SharePoint, which is possible because they can contain only out-of-the-box SharePoint controls, HTML, and JavaScript, so no custom code runs within the SharePoint process itself.

One way to tell whether your page uses the SharePoint master page is to look for the following Page declaration at the top of the page:

<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, 
Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c"
MasterPageFile="~masterurl/default.master" language="C#" %>

If you have a page that doesn’t inherit the SharePoint master page then you must include references to the appropriate JavaScript CSOM files. An example of this would be in an App Part when you don’t want the full SharePoint master page wrapping your App Part’s user interface.

To add the references manually you must include the following script references:

<script type="text/javascript" 
src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.debug.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.debug.js"></script>

If your page is served from outside of the SharePoint site, such is the case with Autohosted apps (served from Azure) or Provider-hosted apps, then you must dynamically generate the script includes in JS to ensure they include the fully qualified domain name (FQDN) in the URL. This is because you need to have the JS files served from the SharePoint Server itself to ensure the browser doesn’t block the JS CSOM calls because of cross-site scripting protections.

To do this you can use the SPHostUrl query string parameter passed to your SharePoint app as follows:

<script type="text/javascript">
var hosturl;
$(document).ready(function () {
hosturl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var scriptbase = hostweburl + "/_layouts/15/";

$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", scriptLoaded);
}
);
});

function scriptLoaded () {
// your code goes here
}

function getQueryStringParameter(paramToRetrieve) {
var params = document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
</script>

In the preceding code the JQuery $(document).ready method is called when the page has loaded and it’s safe for the script to continue. The code then uses the getScript method to dynamically load the JavaScript files. It also uses the SPHostUrl query string parameter passed to dynamically ensure it is retrieving the files from the originating SharePoint site. After that call completes, the scriptLoaded method is called. The ScriptLoaded method is the location you can safely place your JS CSOM code.

 
Others
 
- 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
- Windows Server 2012 : Managing virtual machines (part 2) - Optimizing network performance
 
 
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