Sorry for this basic question.I want to read list items. I will use CAML query . But i dont want to use Visual Studio and C#. Is there any way to do this?
-
What version of sharepoint you using? Each version can have differing techniques – Ali Jafer Nov 16 '17 at 7:33
-
I have got Sharepoint 2010. – f_gulay Nov 16 '17 at 7:35
<script type="text/javascript" src="filelink/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="filelink/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices({
operation: "GetListItems",
async: false,
listName: "Announcements",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
$("#tasksUL").append(liHtml);
});
}
});
});
</script>
<ul id="tasksUL"/>
Yes user Javascript Jquery and write your query in there. Better dont use CAML query, want to read your items use REST api if you can..it is much faster. check out the tutorials on how to use REST in sharepoint
-
1Depends on what wants to be done. Both caml within jsom vs rest within javascript have for and against depending on what you want to do. – Ali Jafer Nov 16 '17 at 7:35
You can use the SharePoint JavaScript Object Model to query using CAML. The syntax is more cumbersome than using the REST API, but it has some advantages, sometimes.
First define your view XML. Here you can limit which fields to return, how to order them, and how many. All of this is optional except for the enclosing <View></View>
tags.
var viewXml = [
"<View>",
"<Query>",
"<OrderBy><FieldRef Type='Text' Name='Title'/></OrderBy>",
"</Query>",
"<ViewFields>",
"<FieldRef Name='Title'/>",
"<FieldRef Name='Created'/>",
"<FieldRef Name='Modified'/>",
"<FieldRef Name='ID'/>",
"</ViewFields>",
"<RowLimit>10</RowLimit>",
"</View>"
].join('');
Then construct the API call. This makes use of SP.SOD.executeFunc
, which ensures that the required library (sp.js
) is loaded before we attempt to make the call.
The key method here is list.renderListData
. (Documentation)
// Ensure SP.js is loaded before attempting the query
SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
// Get current context
var ctx = SP.ClientContext.get_current();
// Get the List
var list = ctx.get_web().get_lists().getByTitle('List Display Name');
// Get list items, filtered by the view XML above
var result = list.renderListData(viewXml);
// Execute the Query
ctx.executeQueryAsync(
function () {
// parse the list data
var parsedData = JSON.parse(result.get_value());
// Do something with the data
},
function (s, a) {
// Error occurred
}
);
});