How to: Work with Websites

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

To work with Web sites, start by using the ClientContext() constructor and passing a URL or URI to return a specific request context.

Retrieving the properties of a Web site

Use the Web property of the ClientContext class to specify the properties of the Web site object that is located at the specified context URL. After you load the Web site object through the Load<T>(T, []) method and then call ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler), you acquire access to all the properties of that Web site. The following example displays the title and description of the root Web site in a specified site collection, although all other properties that are returned by default become available after you load the Web site object and execute the query. For a list of properties that are not available by default when you retrieve specific client objects, see Data Retrieval Overview.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveWebsite
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);

            Web oWebsite = clientContext.Web;

            clientContext.Load(oWebsite);

            clientContext.ExecuteQuery();

            Console.WriteLine("Title: {0} Description: {1}", oWebsite.Title, oWebsite.Description);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveWebsite

        Shared Sub Main()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            clientContext.Load(oWebsite)

            clientContext.ExecuteQuery()

            Console.WriteLine("Title: {0} Description: {1}", oWebsite.Title, oWebsite.Description)
        End Sub
    End Class
End Namespace

Retrieving only specified properties of a Web site

To reduce unnecessary data transference between client and server, you might want to return only specified properties of the Web site object, not all of its properties. In this case, use LINQ query or lambda expression syntax with the Load<T>(T, []) method to specify which properties to return from the server. In the following example, only the title and creation date of the Web site object become available after ExecuteQuery() is called. If you try to write, for example, oWebsite.Description to the console, you receive a PropertyOrFieldNotInitializedException .

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveWebsiteProperties
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;

            clientContext.Load(
                oWebsite,
                website => website.Title,
                website => website.Created);

            clientContext.ExecuteQuery();

            Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveWebsiteProperties

        Shared Sub Main()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            clientContext.Load(oWebsite, _ 
                           Function(website) website.Title, _ 
                           Function(website) website.Created)

            clientContext.ExecuteQuery()

            Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created)
        End Sub
    End Class
End Namespace

Updating the title and description of a Web site

To modify a Web site, you set its properties and call the Update() method, similarly to how the server object model functions. However, in the client object model you must call ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) to request batch processing of all commands that you specify. The following example changes the title and description of a specified Web site.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class UpdateWebSite
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = context.Web;

            oWebsite.Title = "Updated Web Site";
            oWebsite.Description = "This is an updated Web site.";

            oWebsite.Update();

            clientContext.ExecuteQuery();
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class UpdateWebSite

        Shared Sub Main()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            oWebsite.Title = "Updated Web Site"
            oWebsite.Description = "This is an updated Web site."

            oWebsite.Update()

            clientContext.ExecuteQuery()
        End Sub
     End Class
End Namespace

Creating a Web site

To create a Web site object, use the WebCreationInformation class to define its properties, and then pass this object to the Add(WebCreationInformation) method of the WebCollection class. The following example creates a new blog Web site within a site collection.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateWebSite
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";
            string blogDescription = "A new blog Web site.";
            int blogLanguage = 1033;
            string blogTitle = "Blog Web Site";
            string blogUrl = "blogwebsite";
            bool blogPermissions = false;
            string webTemplate = "BLOG#0";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;

            WebCreationInformation webCreateInfo = new WebCreationInformation();
            webCreateInfo.Description = blogDescription;
            webCreateInfo.Language = blogLanguage;
            webCreateInfo.Title = blogTitle;
            webCreateInfo.Url = blogUrl;
            webCreateInfo.UseSamePermissionsAsParentSite = blogPermissions;
            webCreateInfo.WebTemplate = webTemplate;

            Web oNewWebsite = oWebsite.Webs.Add(webCreateInfo);

            clientContext.Load(
                oNewWebsite,
                website => website.ServerRelativeUrl,
                website => website.Created);

            clientContext.ExecuteQuery();

            Console.WriteLine("Server-relative Url: {0} Created: {1}", oNewWebsite.ServerRelativeUrl, oNewWebsite.Created);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class CreateWebSite

        Shared Sub Main()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim blogDescription As String = "A new Blog Web site."
            Dim blogLanguage As Integer = 1033
            Dim blogTitle As String = "Blog Web Site"
            Dim blogUrl As String = "blogwebsite"
            Dim blogPermissions As Boolean = False
            Dim webTemplate As String = "BLOG#0"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            Dim webCreateInfo As New WebCreationInformation()
            webCreateInfo.Description = blogDescription
            webCreateInfo.Language = blogLanguage
            webCreateInfo.Title = blogTitle
            webCreateInfo.Url = blogUrl
            webCreateInfo.UseSamePermissionsAsParentSite = blogPermissions
            webCreateInfo.WebTemplate = webTemplate

            Dim oNewWebsite As Web = oWebsite.Webs.Add(webCreateInfo)

            clientContext.Load(oWebsite, _ 
                               Function(website) website.ServerRelativeUrl, _ 
                               Function(website) website.Created)

            clientContext.ExecuteQuery()

            Console.WriteLine("Server-relative Url: {0} Created: {1}", oWebsite.ServerRelativeUrl, oWebsite.Created)
        End Sub
    End Class
End Namespace

The previous example uses a LINQ query expression to return the ServerRelativeUrl and Created properties, whose values are not available for display unless you explicitly ask for them.

Note

When you use LINQ to create queries against the client object model, you are using LINQ to Objects, not the LINQ to SharePoint provider, which can only be used when you write code against the server object model.

For information and an example about how to work with Web sites within the context of the SharePoint Foundation Silverlight object model, see Using the Silverlight Object Model.

See Also

Concepts

Data Retrieval Overview

SharePoint Client Object Creation

SharePoint 2010 Client Object Model Guidelines

Common Programming Tasks in the Managed Client Object Model

Other Resources

Client Class Library

JavaScript Class Library