I haven’t posted in a while, but behind the scenes I have been working with SharePoint 2013 Client Object Model. The SharePoint 2013 Client Obect Model is a set of libraries and classes with which you can consume SharePoint data through a specific object model that is a subset of the SharePoint Server Object Model.
I’ve ran into a few issue trying to use the sp.js file on custom aspx pages within SharePoint Designer. After creating a new aspx file add the following lines between the <head></head> tags.
<SharePoint:ScriptLink Name="MicrosoftAjax.js" runat="server" Defer="False" Localizable="false"/> <SharePoint:ScriptLink Name="SP.core.js" runat="server" Defer="False" Localizable="false"/> <SharePoint:ScriptLink Name="SP.js" runat="server" Defer="True" Localizable="false"/> Javascript
jQuery(document).ready(function($) { SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadSPData); }); /* END Document Ready */
Note: SharePoint requires the CAML query to be wrapped in <View></View> tags. CAML is case sensitive. Here is an example I use in my JavaScript. <View><Query><Where><Neq><FieldRef Name=\'Published\' /><Value Type=\'Boolean\'>False</Value></Neq></Where><OrderBy><FieldRef Name=\'Created\' Ascending=\'False\' /></OrderBy></Query><RowLimit>5</RowLimit></View> function loadSPData() { var clientContext = new SP.ClientContext("/PATH/TO/SITE"); var oList = clientContext.get_web().get_lists().getByTitle('LIST TITLE'); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<CAML QUERY>'); this.collListItem = oList.getItems(camlQuery); clientContext.load(collListItem, 'Include(ID, Title, Created)'); clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); }
function onQuerySucceeded(sender, args) { var listItemEnumerator = collListItem.getEnumerator(); while (listItemEnumerator.moveNext()) { Do something } /* End listItemEnumerator MoveNext */ }
function onQueryFailed(sender, args) { console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }