Tuesday 8 May 2012

Get sharepoint list data using Javascript

Using Javasctipt Script  getting sharepoint list data


            var clientContext = null;
      var web = null;

      ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");

        function Initialize()
        {
         clientContext = new SP.ClientContext.get_current();
         web = clientContext.get_web();
         this.list = web.get_lists().getByTitle("ListName");
         clientContext.load(list, 'Title', 'Id');
         clientContext.executeQueryAsync(Function.createDelegate(this,        this.onListLoadSuccess),

       Function.createDelegate(this, this.onQueryFailed));
        }

        function onListLoadSuccess(sender, args) {
         alert("List title : " + this.list.get_title() + "; List ID : "+  this.list.get_id());
        }

        function onQueryFailed(sender, args) {
          alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
        }​


This technique is called client object model using ECMA script

We can also get list data using web-service



$(document).ready(function()
{
    var soapEnv =
        "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
            <soapenv:Body>
                 <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
                    <listName>ListName</listName>
                    <viewFields>
                        <ViewFields>
                           <FieldRef Name='Title' />
                       </ViewFields>
                    </viewFields>
                </GetListItems>
            </soapenv:Body>
        </soapenv:Envelope>";

    $.ajax({
        url: "http://my_site/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset="utf-8""
    });
});

function processResult(xData, status) {
    $(xData.responseXML).find("z\:row").each(function() {
        alert($(this).attr("ows_Title"));

    });
}

3 comments:

  1. Does the above code work for MOSS 2007?

    ReplyDelete
  2. ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
    here what is sp.js

    ReplyDelete