Question

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!

Was it helpful?

Solution

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

 <script type="text/javascript">
  window.onload = function(){ ExecuteOrDelayUntilScriptLoaded(ViewItem, "sp.js"); };
</script>

OTHER TIPS

Here is Kai's comment with solution

 function myfunc(){
   ExecuteOrDelayUntilScriptLoaded(listLists, "sp.js");
 }
 _spBodyOnLoadFunctionNames.push("myfunc");
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top