Reputation: 91
I have to read Title and Location from a picture library and display using CEWP.
Can someone suggest how to read SharePoint list item values using Javascript.
Upvotes: 4
Views: 59801
Reputation: 11
if _spPageContextInfo.pageItemId is undefined.
Use this function
function getUrlVars() {
var vars = [],
hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
//THEN DO THIS
var id = getUrlVars()["ID"];
//THEN DO YOUR MAGIC
var context = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var list = context.get_web().get_lists().getById(_spPageContextInfo.pageListId);
var item = list.getItemById(id);
Upvotes: 1
Reputation: 159
You can use the JavaScript Client Object Model. Assuming the window's _spPageContextInfo
object is set with the webServerRelativeUrl
, pageListId
, and pageItemId
properties initialized:
var context = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var list = context.get_web().get_lists().getById(_spPageContextInfo.pageListId);
var item = list.getItemById(_spPageContextInfo.pageItemId);
Then you can load the fields you need:
context.load(item, "Title", "Location");
context.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction), Function.createDelegate(this, this.myErrorFunction));
item
will now be populated with the fields you requested, and you can retrieve them like so:
var itemTitle = item.get_item("Title");
var itemLocation = item.get_item("Location");
Note that you should use the display, not internal, names of the fields you want to load.
Upvotes: 14
Reputation: 18290
In SharePoint 2010 there are three different types of Client Object Model extension you can use. They are Managed Client Object Model, ECMAScript and silverlight extension.
This link more close to your requirement How to: Retrieve Lists Using JavaScript and How do you get the current list item in JavaScript?
SP.ListOperation.Selection Methods
var value = SP.ListOperation.Selection.getSelectedItems();
Check following links for more information:
SharePoint 2010: Use ECMAScript to manipulate (Add/Delete/Update/Get) List Items
Accessing List Data using the JavaScript Client OM
Using the SP2010 Client Object Model to update a list item
How to – SharePoint 2010 – JS client object model and UI advancements
Upvotes: 3
Reputation: 708
In SP2010 there is new client APIs that allow you to interact with SharePoint sites from JavaScript
SharePoint 2010 Client Object Model
Upvotes: -1
Reputation: 1456
You can use jQuery to access the list items via SharePoint Web Services. Refer for example here - SharePoint Web Services with jQuery
Upvotes: 2