Saturday 19 January 2013

Fetching the list view using ECMASCRIPT


Hi Kind Readers,

This is the first blog from me on OFFICE 365. So maybe what I am trying to tell here is quite simple who everybody working on sandbox solutions know but since I am new to this domain everything seems to me worth sharing. :)

Lately I had a requirement where I had to create a custom webpart which will show the data from the list and will be able to perform various list operations.

I thought of various options like creating a custom webpart that will have the entire html dynamically generated or can think in terms of having something like a list view webpart. Now in sandbox ListViewWebpart class is not available. So, I thought of giving the second approach a try and see how to work with SP.View and fetch this generated view and bind it as html in the webpart.

function getMyList() {

        var ctx = SP.ClientContext.get_current();

        var web = ctx.get_web();

        var list = web.get_lists().getByTitle("ListName");

        var views = list.get_views();

        var view = views.getByTitle("All Items");

        view.update();

        ctx.load(view);

        myview = view.renderAsHtml();

        ctx.executeQueryAsync(OnSuccess, OnFailure);

 }

function OnSuccess() {

        myview = myview.get_value().toString();

        jQuery("div id").append(myview);

 }

function OnFailure() {

 }

In the above code snippet , I am fetching the OOB All Items view and then rendering it as HTML and then finally binding the generated html to the div in the webpart. This will help us creating a custom webpart similar to a list view webpart using Client Object Model. :)



Cheers,

Geetanjali

No comments:

Post a Comment