0

Before I write my own, I was wondering if there are any good JS libraries for SP that can be used to get data from an SP list given the list and view name.

I know it is something anyone can do themselves but I wanted to simplify it into a generic library so you can make a call like getListItems(listName, viewName) and get an array of objects for each item in the list.

I like CSOM/JSOM because it gives JavaScript friendly objects of fields (i.e. a Date field yields a Date object) but having to write all the code every time I need to get items for a view is a PITA.

I was going to write a library where you'd provide the list and view name and it'd return an array of objects for the rows in that view. I figured before I do that I should check if one already exists.

My Google searches yielded nothing so I thought I'd check here too.

2

Get sharepoint list items array of objects by list name and view name:

Using Javascript:

function getItemsFromView(listTitle, viewTitle) {

    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);
    context.load(list);
    var view = list.get_views().getByTitle(viewTitle);
    context.load(view);

    context.executeQueryAsync(
        function (sender, args) {
            var query = new SP.CamlQuery();
            query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query></View>");

            var items = list.getItems(query);

            context.load(items);
            context.executeQueryAsync(
                function () {
                    var listEnumerator = items.getEnumerator();
                    return listEnumerator; // Return List Item array
                },
                function (sender, args) { alert("error in inner request: " + args.get_message()); }
        );
        },
        function (sender, args) { alert("error: " + args.get_message()); }
    );
}

//Example of usage    
var listItems = getItemsFromView("Tasks", "My Tasks");
var i = 0;
while (listEnumerator.moveNext()) {
    i++;
}
alert("items retrieved: " + i);

Using CSOM:

Public ListItemCollection GetSPItems(listName, viewName){

ClientContext context = new ClientContext("http://SiteUrl"); 

List list1 = context.Web.Lists.GetByTitle(listName); 

View view = list1.Views.GetByTitle(viewName);
clientContext.Load(view);
clientContext.ExecuteQuery();
CamlQuery query = new CamlQuery();
camlQuery.ViewXml = view.ViewQuery;
ListItemCollection items = announcementsList.GetItems(query); 

context.Load(items); 
context.ExecuteQuery(); 
retun items;

}

//Example of usage:

ListItemCollection listItems = GetSPItems("Tasks", "My Tasks");
  • Thanks but I am asking if there are any libraries someone has written to do this instead of having to writing the whole code every time I need it. – IMTheNachoMan Oct 16 '16 at 22:08
  • put JS function in external js & call external js on your page to use function,.. same make common class for CSOM function & call the method of the class.. – Ronak Patel Oct 17 '16 at 4:09
  • very good! thank you! :) Now I can prepare queries as list views! :D – Pedro Jun 16 '17 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.