SharePoint Client Object Creation

SharePoint 2010

This topic describes creating a SharePoint 2010 client object.

Last modified: May 27, 2011

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();

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();

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.

Show: