Share via


PublishingSite.ContentTypes Property

Gets the collection of SPContentType objects from the RootWeb property.

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

Syntax

'Declaration
Public ReadOnly Property ContentTypes As SPContentTypeCollection
    Get
'Usage
Dim instance As PublishingSite
Dim value As SPContentTypeCollection

value = instance.ContentTypes
public SPContentTypeCollection ContentTypes { get; }

Property Value

Type: Microsoft.SharePoint.SPContentTypeCollection
The collection of SPContentType objects from the RootWeb property.

Remarks

This property wraps the ContentTypes property of the RootWeb property.

Examples

using SPContentTypeId = Microsoft.SharePoint.SPContentTypeId;
using SPContentType = Microsoft.SharePoint.SPContentType;
using SPSite = Microsoft.SharePoint.SPSite;
using SPFile = Microsoft.SharePoint.SPFile;
using SPWeb = Microsoft.SharePoint.SPWeb;
using PublishingSite = Microsoft.SharePoint.Publishing.PublishingSite;
using PublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb;
using PageLayoutCollection = Microsoft.SharePoint.Publishing.PageLayoutCollection;
using PageLayout = Microsoft.SharePoint.Publishing.PageLayout;

namespace Microsoft.SDK.SharePointServer.Samples
{
    public static class PublishingWebCodeSamples
    {
// This sample restricts the set of available page layouts used
// for creating pages in a publishing Web so that only page layouts
// associated with a certain content type are available.
        //
        // Prerequisites:
        // The associatedContentTypeId parameter is from a content
        // type on the root Web of the site.
        //
        public static void RestrictPageLayoutsByContentType(
            PublishingWeb publishingWeb, 
            SPContentTypeId associatedContentTypeId)
        {
            // TODO: Replace these variable values and input
            // parameters with your own values.
            bool excludeHiddenLayouts = true;
            bool resetAllSubsitesToInherit = true;

            //
            // Validate the input parameters.
            //
            if (null == publishingWeb)
            {
                throw new System.ArgumentNullException("publishingWeb");
            }

            SPSite site = publishingWeb.Web.Site;
            PublishingSite publishingSite = new PublishingSite(site);

            //
            // Retrieve a collection of all page layouts in the site
            // collection that match the content type.
            //
            SPContentType associatedContentType = publishingSite.ContentTypes[associatedContentTypeId];
            if (null == associatedContentType)
            {
                throw new System.ArgumentException(
                    "The SPContentTypeId did not match an SPContentType in the SPSite.RootWeb",
                    "associatedContentTypeId");
            }

            PageLayoutCollection pageLayoutsByContentType = 
                publishingSite.GetPageLayouts(associatedContentType, excludeHiddenLayouts);

            //
            // Update the Web to use these page layouts when
            // creating pages.
            //
            publishingWeb.SetAvailablePageLayouts(
                pageLayoutsByContentType.ToArray(),
                resetAllSubsitesToInherit);

            publishingWeb.Update();

            //
            // Verify the expected results (this is not required,
            // and simply demonstrates the results of calling the
            //  SetAvailablePageLayouts method).
System.Diagnostics.Debug.Assert(!publishingWeb.IsAllowingAllPageLayouts);
            System.Diagnostics.Debug.Assert(!publishingWeb.IsInheritingAvailablePageLayouts);

            PageLayout[] availablePageLayouts = publishingWeb.GetAvailablePageLayouts();
            foreach (PageLayout pageLayout in availablePageLayouts)
            {
                System.Diagnostics.Debug.Assert(
                    pageLayout.AssociatedContentType.Id == associatedContentTypeId);
            }

        }

    }
}

See Also

Reference

PublishingSite Class

PublishingSite Members

Microsoft.SharePoint.Publishing Namespace

PublishingSite