1

I am trying to display list items on a popup triggered by a button...

   <script type="text/javascript">
   $(document).ready(function()
           {  
                $( "#divList" ).dialog({ autoOpen: false });     
    $( "#btnGetList" ).click(function() 
     { alert("inside btngetList"); 
   var context = SP.ClientContext.get_current();// new SP.ClientContext;
       var list = context.get_web().get_lists().getByTitle("listname");
       var query = SP.CamlQuery.createAllItemsQuery();
       var allList = list.getItems(query);
       context.load(allList );
   context.executeQueryAsync(Function.createDelegate (this,getDocsAllItemsSuccess),
                                            Function.createDelegate(this, getDocsAllItemsFailure));

     Function getDocsAllItemsSuccess(sender, args) {
                                                                                        var listEnumerator = allList.getEnumerator();
                                                                                                                             while (listEnumerator.moveNext()) {
                                                                                        $('#divGetList').append(listEnumerator.get_current().get_item("FileLeafRef") + '<br />');
                                                                                                                                    }
                                                                                                                            $("#divGetList").dialog( "open" );
                                                                                            }

                                            function getDocsAllItemsFailure(sender, args)
     {   alert('Failed to get items.'+args.get_message);  
                                            }

    references used are:

   <script type="text/javascript"    src="//code.jquery.com/jquery-2.1.4.min.js"></script>
  <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
  <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
 <script type="text/javascript" src="/_layouts/15/sp.js"></script>
 <script type="text/javascript" src="/_layouts/15/SP.RequestExecutor.js"></script>

since these do not work, I use

<script type="text/javascript" src="{URL}/Style% 
  20Library/Scripts/jquery-1.8.3.min.js">

When I use this I can manipulate controls using jquery. Only getting listitems is a problem, which is why I think sp.js file is not loaded Aside, I am able to get site URL by putting the code without $(document).ready(function(){}) . Please help.....

2 Answers 2

6

Instead of $(document).ready() use

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
    // Your code goes here
});

$(document).ready() does not ensure that ClientContext is loaded.

3
  • No. I had tried earlier and now also. It does not work ..
    – GK70
    Commented Mar 18, 2016 at 14:06
  • It was my mistake .. yes it does work. And if have to use jquery-ui, after adding reference ,do I need to nest SOD as done earlier ? I want to avoid adding reference like code. <script src="code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">. I prefer putting those .js in script library and referencing ..
    – GK70
    Commented Mar 18, 2016 at 17:43
  • I have accepted now
    – GK70
    Commented Mar 20, 2016 at 12:50
0

I would rather just use CDN for the Sp.Js, because I had the same issue as you trying to get it to load.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.min.js" type="text/javascript"></script> //Call Jquery SPServices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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