PublishingWeb - Classe

Fournit le comportement de publication d'une instance de SPWeb qui prend en charge la publication.

Hiérarchie d’héritage

System.Object
  Microsoft.SharePoint.Publishing.PublishingWeb

Espace de noms :  Microsoft.SharePoint.Publishing
Assembly :  Microsoft.SharePoint.Publishing (dans Microsoft.SharePoint.Publishing.dll)

Syntaxe

'Déclaration
Public NotInheritable Class PublishingWeb
'Utilisation
Dim instance As PublishingWeb
public sealed class PublishingWeb

Remarques

La classe PublishingWeb fournit le comportement de publication spécifique pour une SPWeb que prend en charge la publication, notamment l'accès aux enfants, PublishingPage et PublishingWeb instances, variations prennent en charge, les paramètres de navigation, les PageLayout et les restrictions du modèle Web et les paramètres de la page d'accueil. Cette classe encapsule une instance de SPWeb qui a activé la fonctionnalité de publication.

Instanciez cette classe à l'aide de la méthode statique GetPublishingWeb(SPWeb) ou en le récupérant à partir d'une collection de PublishingWebCollection .

La classe PublishingWeb encapsule la classe SPWeb . Il expose également directement le sous-jacent SPWeb par le biais de la propriété Web pour que les fonctionnalités supplémentaires SPWeb facilement accessibles.

Les membres exposés par cette classe sont optimisés pour le SPSite de contexte en cours. Essayez d'utiliser des méthodes dans la classe PublishingWeb pour renvoyer des objets à partir d'une autre collection de sites peuvent entraîner des comportements inattendus, et cela est donc pas reconnu.

Exemples

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

Cohérence de thread

Tous les membres statique (Partagé dans Visual Basic)s publics de ce type sont thread-safe. Cela n’est pas garanti pour les membres d’instance.

Voir aussi

Référence

PublishingWeb - Membres

Microsoft.SharePoint.Publishing - Espace de noms

GetPublishingWeb

GetPublishingWebs