Share via


PublishingWeb class

Provides publishing behavior for an SPWeb instance that supports publishing.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.Publishing.PublishingWeb

Namespace:  Microsoft.SharePoint.Publishing
Assembly:  Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)

Syntax

'Declaration
Public NotInheritable Class PublishingWeb
'Usage
Dim instance As PublishingWeb
public sealed class PublishingWeb

Remarks

The PublishingWeb class provides publishing-specific behavior for an SPWeb that supports publishing, including access to child PublishingPage and PublishingWeb instances, variations support, navigation settings, PageLayout and Web template restrictions, and Welcome page settings. This class wraps an SPWeb instance that has the publishing feature activated.

Instantiate this class by using the static GetPublishingWeb(SPWeb) method or by retrieving it from a PublishingWebCollection collection.

The PublishingWeb class wraps the SPWeb class. It also directly exposes the underlying SPWeb through the Web property so that additional SPWeb functionality can be easily accessed.

The members exposed by this class are optimized for the current SPSite context. Trying to use methods in the PublishingWeb class to return objects from a different site collection can result in unexpected behaviors, and doing so is not supported.

Examples

using System;
using System.IO;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace Microsoft.SDK.SharePoint.Samples.PublishingWebSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("--------------------------------------");
            Console.WriteLine("Starting sample tests execution...");

            #region  PublishingWeb example

            //
            // ---------------------------------------------------------------------------------------
            // PublishingWeb example
            // ---------------------------------------------------------------------------------------
            // Passing the full url of a PublishingWeb.
            //
            // To compile add additional references to 
            // - System.Web.dll
            // - Microsoft.SharePoint.dll
            // - Microsoft.SharePoint.Publishing.dll
            //

            //
            // You need to pass one parameter which is the full url of a PublishingWeb.
            //
            if (args.Length < 1)
            {
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("You need to pass the full url of a PublishingWeb.");
                FinishSampleTest();
                return;
            }

            string url = args[0];

            //
            // The url must be in the correct format.
            //
            Uri webUri = null;
            if (!Uri.TryCreate(url, UriKind.Absolute, out webUri))
            {
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("The url is not in correct format.");
                FinishSampleTest();
                return;
            }

            SPSite site = null;
            try
            {
                site = new SPSite(url);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("The url is not a valid PublishingWeb.");
                FinishSampleTest();
                return;
            }

            using (SPWeb web = site.OpenWeb(HttpUtility.UrlDecode(webUri.AbsolutePath)))
            {
                PublishingWeb area = null;

                //
                // The url must be the full url of a PublishingWeb.
                //
                if (!web.Exists || !PublishingWeb.IsPublishingWeb(web))
                {
                    Console.WriteLine("--------------------------------------");
                    Console.WriteLine("The url is not a valid PublishingWeb.");
                    FinishSampleTest();
                    return;
                }

                area = PublishingWeb.GetPublishingWeb(web);
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("Print out some properties of the current PublishingWeb:");
                PrintPublishingWebProperty(area);


                //
                // Create a PublishingPage examples: 
                //

                Console.WriteLine("--------------------------------------");
                Console.WriteLine("Adding the 1st PublishingPage...");

                //
                // Create a new PublishingPage using default PageLayout.
                // 
                PublishingPage page1 = area.AddPublishingPage();
                Console.WriteLine("Sucessfully added the 1st PublishingPage.");

                // 
                // Check in the 1st PublishingPage.
                //
                if (page1.ListItem.File.CheckOutType != SPFile.SPCheckOutType.None)
                {
                    Console.WriteLine("Check-in the 1st PublishingPage...");
                    page1.CheckIn("The PublishingPage page1 1st check in.");
                    Console.WriteLine("Successfully checked-in the 1st PublishingPage.");
                }

                Console.WriteLine("--------------------------------------");
                Console.WriteLine("Try to add the 2nd PublishingPage using the default PageLayout of the PublishingWeb...");
                PageLayout defaultPageLayout = area.DefaultPageLayout;
                PublishingPage page2 = null;

                string page2Url = area.Web.Url + "/Pages/Page2.aspx";

                // 
                //  Get a PublishingPage by its url.
                // 
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("First try to see whether the PublisingPage with the same Url already exists:");
                page2 = area.GetPublishingPage(page2Url);
                Console.WriteLine("The Url of the PublishingPage is :" + page2Url);

                if (page2 != null)
                {
                    Console.WriteLine("The PublishingPage with Url {0} already exists.", page2Url);
                }
                else
                {
                    Console.WriteLine("The PublishingPage with Url {0} does not exist.", page2Url);
                    Console.WriteLine("Adding the 2nd PublishingPage...");
                    page2 = area.AddPublishingPage("Page2.aspx", defaultPageLayout);
                    Console.WriteLine("Sucessfully added the 2nd PublishingPage.");

                    // 
                    // Check in the 2nd PublishingPage.
                    //
                    if (page2.ListItem.File.CheckOutType != SPFile.SPCheckOutType.None)
                    {
                        Console.WriteLine("Check-in the 2nd PublishingPage...");
                        page2.CheckIn("The PublishingPage page2 1st check in.");
                        Console.WriteLine("Successfully checked-in the 2nd PublishingPage.");
                    }
                }

                //
                // Get a PublishingPage examples: 
                //

                // 
                // Get a PublishingPage by its ID.
                // 
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("Try to retrieve the PublisingPage by its ID:");
                int page1ID = page1.ListItem.ID;
                Console.WriteLine("The ID of the PublishingPage is :" + page1ID);
                PublishingPage page3 = area.GetPublishingPage(page1ID);

                // 
                //  Check out the PublishingPage, and edit its properties, then check it in.
                //
                if (page3 != null)
                {
                    Console.WriteLine("The PublishingPage with ID {0} is found.", page1ID);

                    if (page3.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)
                    {
                        Console.WriteLine("Check-out the PublishingPage...");
                        page3.CheckOut();
                        Console.WriteLine("Successfully checked-out the PublishingPage.");
                    }

                    Console.WriteLine("Updating the title of the PublishingPage to be 'Page3'...");
                    page3.Title = "Page3";
                    page3.Update();
                    Console.WriteLine("Successfully updated the PublishingPage.");

                    if (page3.ListItem.File.CheckOutType != SPFile.SPCheckOutType.None)
                    {
                        Console.WriteLine("Check-in the PublishingPage...");
                        page3.CheckIn("Update the title to be Page3.");
                        Console.WriteLine("Successfully checked-in the PublishingPage.");
                    }
                }

                //
                // Iterate the whole PublishingPage List.
                //
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("Iterate through the Pages library:");
                area.IterateOverAllPages(
                 delegate(PublishingPage page)
                 {
                     PrintPageUrl(page);
                 });
            }

            if (site != null)
            {
                site.Close();
            }

            #endregion

            Console.WriteLine("--------------------------------------");
            Console.WriteLine("Sample tests completed.");

            FinishSampleTest();
        }

        static void PrintPublishingWebProperty(PublishingWeb area)
        {
            PrintPublishingWebProperty(area, "ImagesLibrary", area.ImagesLibrary.Title);
            PrintPublishingWebProperty(area, "DocumentsLibrary", area.DocumentsLibrary.Title);
        }

        static void PrintPublishingWebProperty(PublishingWeb area, string propertyName, string propertyValue)
        {
            Console.WriteLine("The property {0} of PublishingWeb {1} is: {2}.", propertyName, area.Name, propertyValue);
        }

        static void PrintPageUrl(PublishingPage page)
        {
            Console.WriteLine("The Url of PublishingPage " + page.Name + " is " + page.Url + ".");
        }

        static void FinishSampleTest()
        {
            Console.WriteLine("--------------------------------------");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}
// Access the Pages list information without creating a PublishingWeb
// object.
using (SPSite site = new SPSite("http://myteam/team/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        if (PublishingWeb.IsPublishingWeb(web))
        {
// Get the ID for the Pages list if we 
// know that Web is a PublishingWeb.
Guid pagesListId = PublishingWeb.GetPagesListId(web);

// Get the list by way of the ID.
SPList pagesList = web.Lists[pagesListId];

// Get the Pages list URL. Note:
// PublishingWeb.GetPagesListName(web)
// is equivalent to pagesList.RootFolder.Url.
string pagesListUrl = PublishingWeb.GetPagesListName(web);
        }
        else
        {
// If the SPWeb is not a PublishingWeb, 
// then GetPagesListName returns the URL
// that would be used by the Pages list 
// if the Publishing feature were to be
// activated.
string pagesListName = PublishingWeb.GetPagesListName(web);
        }
    }
}

// You can also create a PublishingWeb and access
// the properties from it.
using (SPSite site = new SPSite("http://myteam/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);

        // Get the ID for the Pages list.
        Guid pagesListId = publishingWeb.PagesListId;

        SPList pagesList = publishingWeb.PagesList;

        // The PublishingWeb.PagesListName is equivalent 
        // to PublishingWeb.PagesList.RootFolder.Url.
        string pagesListUrl = publishingWeb.PagesListName;
    }
}
' Access the Pages list information without creating a PublishingWeb
' object.
Using site As New SPSite("http://myteam/team/")
    Using web As SPWeb = site.OpenWeb()
        If PublishingWeb.IsPublishingWeb(web) Then
' Get the ID for the Pages list if we 
' know that Web is a PublishingWeb.
Dim pagesListId As Guid = PublishingWeb.GetPagesListId(web)

' Get the list by way of the ID.
Dim pagesList As SPList = web.Lists(pagesListId)

' Get the Pages list URL. Note:
' PublishingWeb.GetPagesListName(web)
' is equivalent to pagesList.RootFolder.Url.
Dim pagesListUrl As String = PublishingWeb.GetPagesListName(web)
        Else
' If the SPWeb is not a PublishingWeb, 
' then GetPagesListName returns the URL
' that would be used by the Pages list 
' if the Publishing feature were to be
' activated.
Dim pagesListName As String = PublishingWeb.GetPagesListName(web)
        End If
    End Using
End Using

' You can also create a PublishingWeb and access
' the properties from it.
Using site As New SPSite("http://myteam/")
    Using web As SPWeb = site.OpenWeb()
        Dim publishingWeb As PublishingWeb = PublishingWeb.GetPublishingWeb(web)

        ' Get the ID for the Pages list.
        Dim pagesListId As Guid = publishingWeb.PagesListId

        Dim pagesList As SPList = publishingWeb.PagesList

        ' The PublishingWeb.PagesListName is equivalent 
        ' to PublishingWeb.PagesList.RootFolder.Url.
        Dim pagesListUrl As String = publishingWeb.PagesListName
    End Using
End Using

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See also

Reference

PublishingWeb members

Microsoft.SharePoint.Publishing namespace

GetPublishingWeb

GetPublishingWebs