none
Pulling Data from Lists using Jquery Client Object Model

    Soru

  • Hi, I'm trying to pull data into a list using the Jquery Client Object Model.  For the life of me i can't get this to work and I'm not having any luck with using the Developer Tools on Mozilla. Any Jquery experts out there can help me? Thanks in advance!

    <script src="/_layouts/SP.js" type="text/ecmascript"></script>
    <script type="text/javascript">
     
    function ViewItem() {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('TestList');
        var query = SP.CamlQuery.createAllItemsQuery();
        allItems = list.getItems(query);
        context.load(allItems, 'Include(PublishingRollupImage, Title)');
        context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
    }
     
    function success() {
        var ListEnumerator = this.allItems.getEnumerator();
        while(ListEnumerator.moveNext()) {
            var currentItem = ListEnumerator.get_current();
            $("<li>" + currentItem.get_item('PublishingRollupImage') + currentItem.get_item('Title') + "</li>").appendTo('#Data');
    
        }
     }
    function failed(sender, args) {
        alert("failed. Message:" + args.get_message());
    }
    
    </script>
    
    
    Unordered list goes here:<br/>
    <ul id="Data"></ul>

    15 Haziran 2012 Cuma 16:57

Yanıtlar

  • Hi,

    Before calling Sharepoint ECMA script, you have to verify the SP.js is loaded or not. Or you have to wait until the SP.js file loaded.

    Try the following javascript method, which loads our function after the SP.js.

    ExecuteOrDelayUntilScriptLoaded( <your function> , "SP.js");

    For that, we have to call the above method within the jQuery ready method. I have modifed your javascript as follows,

    <script src="/_layouts/SP.js" type="text/ecmascript"></script>
    <script type="text/javascript">
     
    $(document).ready(function() {
    ExecuteOrDelayUntilScriptLoaded(loaditems, "SP.js");
    });

    function loaditems()
    {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('Homepage Slider');
        var query = SP.CamlQuery.createAllItemsQuery();
        allItems = list.getItems(query);
        context.load(allItems, 'Include(PublishingRollupImage, Title)');
        context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
    }
    function success() {
        var ListEnumerator = this.allItems.getEnumerator();
        while(ListEnumerator.moveNext()) {
            var currentItem = ListEnumerator.get_current();
            $("<li>" + currentItem.get_item('PublishingRollupImage') + '-' +currentItem.get_item('Title') + "</li>").appendTo('#Data');
        }
    }
    function failed(sender, args) {
        alert("failed. Message:" + args.get_message());
    }

    </script>

    I'll hope, the above point help you


    Regards,
    Shantha Kumar .T | MCPD - SharePoint Developer 2010 | MCITP - SharePoint Administrator 2010
    (B) Shantha Kumar's Blog | (T)Follow me

    16 Haziran 2012 Cumartesi 01:03

Tüm Yanıtlar

  • What happens when you try use like you did?

    15 Haziran 2012 Cuma 17:15
  • I realized there's nothing to initiate the function so I edited the script to initiate on page load as follows with no returning data. I also tested it by pushing the data into an alert and it worked fine (the alert have both site columns of data).

    <script src="/_layouts/SP.js" type="text/ecmascript"></script> <script type="text/javascript"> $(document).ready(function() { var context = new SP.ClientContext.get_current(); var web = context.get_web(); var list = web.get_lists().getByTitle('Homepage Slider'); var query = SP.CamlQuery.createAllItemsQuery(); allItems = list.getItems(query); context.load(allItems, 'Include(PublishingRollupImage, Title)'); context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); }); function success() { var ListEnumerator = this.allItems.getEnumerator(); while(ListEnumerator.moveNext()) { var currentItem = ListEnumerator.get_current(); $("<li>" + currentItem.get_item('PublishingRollupImage') + '-' +currentItem.get_item('Title') + "</li>").appendTo('#Data'); } } function failed(sender, args) { alert("failed. Message:" + args.get_message()); } </script>

    Unordered list goes here:<br/>
    <ul id="Data"></ul>


    • Düzenleyen Sol Diablo 15 Haziran 2012 Cuma 18:06
    15 Haziran 2012 Cuma 17:42
  • Hi,

    Before calling Sharepoint ECMA script, you have to verify the SP.js is loaded or not. Or you have to wait until the SP.js file loaded.

    Try the following javascript method, which loads our function after the SP.js.

    ExecuteOrDelayUntilScriptLoaded( <your function> , "SP.js");

    For that, we have to call the above method within the jQuery ready method. I have modifed your javascript as follows,

    <script src="/_layouts/SP.js" type="text/ecmascript"></script>
    <script type="text/javascript">
     
    $(document).ready(function() {
    ExecuteOrDelayUntilScriptLoaded(loaditems, "SP.js");
    });

    function loaditems()
    {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('Homepage Slider');
        var query = SP.CamlQuery.createAllItemsQuery();
        allItems = list.getItems(query);
        context.load(allItems, 'Include(PublishingRollupImage, Title)');
        context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
    }
    function success() {
        var ListEnumerator = this.allItems.getEnumerator();
        while(ListEnumerator.moveNext()) {
            var currentItem = ListEnumerator.get_current();
            $("<li>" + currentItem.get_item('PublishingRollupImage') + '-' +currentItem.get_item('Title') + "</li>").appendTo('#Data');
        }
    }
    function failed(sender, args) {
        alert("failed. Message:" + args.get_message());
    }

    </script>

    I'll hope, the above point help you


    Regards,
    Shantha Kumar .T | MCPD - SharePoint Developer 2010 | MCITP - SharePoint Administrator 2010
    (B) Shantha Kumar's Blog | (T)Follow me

    16 Haziran 2012 Cumartesi 01:03