Sunday 22 April 2012

Retrieve items from view using SPquery\Caml sharepoint 2010


I have written a post earlier about "Retrieve items from a View with CAML in Javascript Client Object model - SharePoint 2010" the code works fine only when you just need the item count but if you try to retrive single items from the collection it does not return any value esp. when using enumerator to the item collection. To overcome this issue i have rewritten it check out the complete code below to retrive items from a view using CAML

code -
function runCode()
{
    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle("listName");
    var view = list.get_views().getByTitle("All Items");
    context.load(view);
    context.executeQueryAsync(
        function (sender, args) { getItemsFromList("listname", "<View><Query>" + view.get_viewQuery() + "</Query></View>") },
        function (sender, args) { alert("error: " + args.get_message()); }
    );
}
function getItemsFromList(listTitle, queryText)
{
    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);
    var query = new SP.CamlQuery();
    query.set_viewXml(queryText);
    var items = list.getItems(query);
    context.load(items);
    context.executeQueryAsync(
        function () {
            var text = "";
            var itemCount = items.get_count();
                     
            for (var i = 0; i < itemCount; i++) {
                text += items.get_item(i).get_item("Title") +";";
                }
        },
        function (sender, args) { alert("error in inner request: " + args.get_message()); }
   );
}


Ads by Google

No comments:

Post a Comment