SharePoint Client Object Creation

SharePoint 2010

Published: May 2010

This topic describes creating a SharePoint 2010 client object.

Available in SharePoint Online

If a client object can be created, such as a List object (JavaScript: List), its corresponding client object collection, such as ListCollection (JavaScript: ListCollection), has an Add method that accepts a ClientObjectCreationInformation object, for example, ListCreationInformation (JavaScript: ListCreationInformation), which contains all the information required to create the new object.

The following example uses WebCreationInformation (JavaScript: WebCreationInformation) with the Add(WebCreationInformation) method (JavaScript: add(parameters)) to create a child Web site under a specified Web site .

function createWebsite() {

    var clientContext = new SP.ClientContext('http://MyServer/sites/MySiteCollection/MyWebSite');
    var collWeb = clientContext.get_web().get_webs();

    var webCreationInfo = new SP.WebCreationInformation();
    webCreationInfo.set_title('My New Web Site');
    webCreationInfo.set_description('Description of new Web site...');
    webCreationInfo.set_language(1033);
    webCreationInfo.set_url('MyNewWebSite');
    webCreationInfo.set_useSamePermissionsAsParentSite(true);
    webCreationInfo.set_webTemplate('STS#0');

    var oNewWebsite = collWeb.add(webCreationInfo);


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

function onQuerySucceeded() {
    alert("Created Web site.");
}

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

In the previous example, the Add(WebCreationInformation) method (JavaScript: add(parameters)) creates a Web site immediately in the client object collection, without waiting for subsequent collection data retrieval or for a call to ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)).

You can use a ClientObjectCreationInformation object to create numerous objects. Creating a navigation node, for example, also involves using a ClientObjectCreationInformation object. The following example defines a navigation node by means of a NavigationNodeCreationInformation object (JavaScript: NavigationNodeCreationInformation), and uses this object to add a link within the Quick Launch area of the specified Web site. The example adds a link to a list on another Web site as the second child node under the top-level Lists node.

function getNavNodes() {
    this.clientContext = new SP.ClientContext('/sites/MySiteCollection/MyWebSite');
    this.collNavNode = clientContext.get_web().get_navigation().get_quickLaunch();

    clientContext.load(collNavNode);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.getChildNavNodes), Function.createDelegate(this, this.onQueryFailed));
}

function getChildNavNodes() {

    this.collChildNavNode = collNavNode.get_item(1).get_children();
    clientContext.load(collChildNavNode);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.createNavNode), Function.createDelegate(this, this.onQueryFailed));
}

function createNavNode() {

    var navNodeCreationInfo = new SP.NavigationNodeCreationInformation();
    navNodeCreationInfo.set_previousNode(collChildNavNode.get_item(0));
    navNodeCreationInfo.set_title('My Navigation ECMA Node');
    navNodeCreationInfo.set_url('http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx');

    collChildNavNode.add(navNodeCreationInfo);

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

function onQuerySucceeded() {

    alert("Created navigation node.");
}

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

The previous example creates a link to another Web site within a site collection. To create an external link to a location outside the site collection, set the IsExternal property (JavaScript: IsExternal) of the NavigationNodeCreationInformation object (JavaScript: NavigationNodeCreationInformation) to true before you call the Add(NavigationNodeCreationInformation) method (JavaScript: add(parameters)).

For information about creating client objects in the context of a Silverlight application, see Using the Silverlight Object Model.

Community Additions

ADD
Show:
© 2014 Microsoft