Export (0) Print
Expand All

SP.ListCollection Class

SharePoint 2010

Last modified: May 26, 2011

Applies to: SharePoint Foundation 2010

Represents a collection of SP.List Class objects.

SP.ListCollection

The following example adds an input button on an application page that creates two new announcements lists and adds them to the list collection of the current web site.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

    var listCollection;
    function runCode() {

        var clientContext = new SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            var web = clientContext.get_web();
            this.listCollection = web.get_lists();

            // Specify the title and template of the new lists.
            var lci1 = new SP.ListCreationInformation();
            lci1.set_title('New Announcements');
            lci1.set_templateType(SP.ListTemplateType.announcements);
            this.listCollection.add(lci1);

            var lci2 = new SP.ListCreationInformation();
            lci2.set_title('Old Announcements');
            lci2.set_templateType(SP.ListTemplateType.announcements);
            this.listCollection.add(lci2);

            clientContext.load(this.listCollection);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }

    function onQuerySucceeded() {
        var listInfo = 'Lists on the current site:' + '\n\n';
        var listEnumerator = this.listCollection.getEnumerator();
        while (listEnumerator.moveNext()) {
            var list = listEnumerator.get_current();
            listInfo += list.get_title() + '\n';
        }
        alert(listInfo);
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

</asp:Content>
Show:
© 2014 Microsoft