Export (0) Print
Expand All

How to: Work with Websites

SharePoint 2010

Last modified: April 29, 2011

Applies to: SharePoint Foundation 2010

In this article
Retrieving the properties of a Web site
Retrieving only specified properties of a Web site
Updating the title and description of a Web site
Creating a Web site

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.

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

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

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

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

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 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.

Show:
© 2014 Microsoft