2

I am new to SP Dev and I am trying to add some CEWPs that reference javascript I have placed in text files and uploaded to a document library. For bland functions it seems to work fine, however for working with data and lists, I can't get it to work.

Below is the javascript where I am just trying to grab all the names of the lists:

<script type='text/javascript'>

function listLists()
{
alert("In the function");   
var ctx = new SP.ClientContext.get_current();
    this.oListColl = ctx.get_web().get_lists();
    ctx.load(this.oListColl);

    ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
}

function onSuccess(sender, args)
{
    var listEnum = this.oListColl.getEnumerator();
    while(listEnum.moveNext())
    {
        var oList = listEnum.get_current();
        alert(oList.get_title());
    }
}

function onFail(sender, args)
{
    alert('FAIL!');
}

listLists();
</script>

So I have been able to do this within SharePoint Designer, however I would like to learn how to make it work with the library approach as well. I know that in the SPD I need to add an entire SharePoint:scriptlink reference to 'sp.js' under the meta tags, and I am guessing that the reason this doesn't seem to work is in this type of approach there is no reference??? If so I just don't know how or if I can do that?

So the alert was just so I could tell how far it was getting, though the next line seems to be where it stopped (used another alert of setting context and it didn't return). I will also just go ahead and ask, if this works I would like to replace the alert with a document.write kind of thing...could anyone help with an example of that?

Thanks for any help; always appreciated!

2 Answers 2

2

Yes, you do need to add a reference to SP.js

 <script type="text/javascript">
  window.onload = function(){ ExecuteOrDelayUntilScriptLoaded(ViewItem, "sp.js"); };
</script>
2
  • does it work as expected? Commented Jun 10, 2012 at 23:50
  • @Falak...yes! lol...after I fixed my own script a bit. exactly what I needed thanks for the help!
    – Justin
    Commented Jun 10, 2012 at 23:59
1

Here is Kai's comment with solution

 function myfunc(){
   ExecuteOrDelayUntilScriptLoaded(listLists, "sp.js");
 }
 _spBodyOnLoadFunctionNames.push("myfunc");
2
  • thanks for the help! hmmm...this still doesn't seem to work. perhaps something else in my code above is incorrect?
    – Justin
    Commented Jun 10, 2012 at 23:47
  • yeah it was, fixed and working now. now I just need to figure out how to get document write out of it instead of alerts! thanks again.
    – Justin
    Commented Jun 10, 2012 at 23:59

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.