SharePoint Client Object Creation

This topic describes creating a SharePoint 2010 client object.

Applies to: SharePoint Foundation 2010

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 .

ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");
WebCollection collWeb = clientContext.Web.Webs;

WebCreationInformation webCreationInfo = new WebCreationInformation();
webCreationInfo.Title = "My New Web Site";
webCreationInfo.Description = "Description of new Web site...";
webCreationInfo.Language = 1033;
webCreationInfo.Url = "MyNewWebSite";
webCreationInfo.UseSamePermissionsAsParentSite = true;
webCreationInfo.WebTemplate = "STS#0";

Web oNewWebsite = collWeb.Add(webCreationInfo);
clientContext.ExecuteQuery();
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite")
Dim collWeb As WebCollection = clientContext.Web.Webs
         
Dim webCreationInfo As New WebCreationInformation()
webCreationInfo.Title = "My New Web Site"
webCreationInfo.Description = "Description of new Web site..."
webCreationInfo.Language = 1033
webCreationInfo.Url = "MyNewWebSite"
webCreationInfo.UseSamePermissionsAsParentSite = True
webCreationInfo.WebTemplate = "STS#0"
         
Dim oNewWebsite As Web = collWeb.Add(webCreationInfo)
clientContext.ExecuteQuery()
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.

ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");

NavigationNodeCollection collNavNode = clientContext.Web.Navigation.QuickLaunch;

clientContext.Load(collNavNode);
clientContext.ExecuteQuery();

NavigationNodeCollection collChildNavNode = collNavNode[1].Children;

clientContext.Load(collChildNavNode);
clientContext.ExecuteQuery();

NavigationNodeCreationInformation navNodeCreationInfo = new NavigationNodeCreationInformation();

navNodeCreationInfo.PreviousNode = collChildNavNode[0];
navNodeCreationInfo.Title = "My Navigation Node";
navNodeCreationInfo.Url = "http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx";

collChildNavNode.Add(navNodeCreationInfo);

clientContext.ExecuteQuery();
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite")
         
Dim collNavNode As NavigationNodeCollection = clientContext.Web.Navigation.QuickLaunch
         
clientContext.Load(collNavNode)
clientContext.ExecuteQuery()
         
Dim collChildNavNode As NavigationNodeCollection = collNavNode(1).Children
         
clientContext.Load(collChildNavNode)
clientContext.ExecuteQuery()
         
Dim navNodeCreationInfo As New NavigationNodeCreationInformation()
         
navNodeCreationInfo.PreviousNode = collChildNavNode(0)
navNodeCreationInfo.Title = "My Navigation Node"
navNodeCreationInfo.Url = "http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx"
         
collChildNavNode.Add(navNodeCreationInfo)
         
clientContext.ExecuteQuery()
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.

See Also

Concepts

How to: Work with Websites

How to: Create, Update, and Delete Lists

How to: Create, Update, and Delete List Items

How to: Work with Users and Groups

SharePoint 2010 Client Object Model Hierarchy and Identity

Client Context as Central Object

Client Objects, Value Objects, and Scalar Properties

Data Retrieval Overview

SharePoint 2010 Client Object Model Guidelines

Differences Between Managed and JavaScript Object Models

Common Programming Tasks in the Managed Client Object Model

Other Resources

Client Class Library

JavaScript Class Library

Using the SharePoint Foundation 2010 Managed Client Object Model

Client Object Model Resource Center