PublishingSite Constructor
Constructs an instance of a PublishingSite object that wraps the given SPSite object.
Namespace: Microsoft.SharePoint.Publishing
Assembly: Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)
Syntax
'Declaration
Public Sub New ( _
site As SPSite _
)
'Usage
Dim site As SPSite
Dim instance As New PublishingSite(site)
public PublishingSite(
SPSite site
)
Parameters
- site
Type: Microsoft.SharePoint.SPSite
SPSite to retrieve a PublishingSite wrapper for.
Remarks
Use this method to instantiate a PublishingSite object that wraps an SPSite that you have already retrieved. The caller must clean up the original SPSite by calling the Close method when this PublishingSite object and the SPSite object passed are no longer in use. The site parameter should be a non-null SPSite instance for which the PublishingResources feature is active. Before instantiating a PublishingSite object, first check the IsPublishingSite method to confirm that publishing behavior is supported on the SPSite. If publishing is not supported on the SPSite, the methods and properties of the PublishingSite wrapper may produce unexpected behavior.
Examples
using SPSite = Microsoft.SharePoint.SPSite;
using PublishingSite = Microsoft.SharePoint.Publishing.PublishingSite;
using SPWeb = Microsoft.SharePoint.SPWeb;
using PublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb;
using PublishingWebCollection = Microsoft.SharePoint.Publishing.PublishingWebCollection;
using SPWebTemplate = Microsoft.SharePoint.SPWebTemplate;
namespace Microsoft.SDK.SharePointServer.Samples
{
public static class PublishingWebCollectionCodeSamples
{
// This method creates a new PublishingWeb below
//the root Web of a PublishingSite object.
//
// Prerequisites:
// The SPSite passed should be a site that supports publishing.
//
// This sample demonstrates use of:
// PublishingSite.IsPublishingSite( SPSite )
// PublishingSite constructor
// PublishingSite.RootWeb
// PublishingWeb.GetPublishingWebs()
// PublishingWeb.Web
// PublishingWebCollection.Add(string, uint, string)
public static void CreatePublishingWebBelowRoot( SPSite site, SPWebTemplate webTemplate )
{
// TODO: Replace these variable values and
// input parameters with your own values.
// Your Web URL name for the PublishingWeb object to create.
string yourWebUrlName = "yourWebUrl";
PublishingWeb newWeb = null;
try
{
//
// Validate the input parameters.
//
if (null == site)
{
throw new System.ArgumentNullException("site");
}
if (null == webTemplate)
{
throw new System.ArgumentNullException("webTemplate");
}
PublishingSite publishingSite = null;
if (!PublishingSite.IsPublishingSite(site))
{
throw new System.ArgumentException(
"The SPSite is expected to be a PublishingSite",
"site");
}
publishingSite = new PublishingSite( site );
SPWeb rootWeb = publishingSite.RootWeb;
if( !PublishingWeb.IsPublishingWeb( rootWeb ))
{
throw new System.ArgumentException(
"The SPSite.RootWeb is expected to be a PublishingWeb",
"site");
}
PublishingWeb rootPublishingWeb =
PublishingWeb.GetPublishingWeb( rootWeb );
PublishingWebCollection publishingWebs =
rootPublishingWeb.GetPublishingWebs();
//
// Create the new PublishingWeb object by using
/. the sample values provided.
//
newWeb = publishingWebs.Add(
yourWebUrlName,
rootWeb.Language, // TODO: Replace with your own language value.
webTemplate.Name ); // TODO: Replace with your Web template name.
// The new PublishingWeb has the Publishing feature active.
System.Diagnostics.Debug.Assert(
null != newWeb.Web.Features[Microsoft.SharePoint.Publishing.FeatureIds.Publishing]);
}
finally
{
//
// Always close the SPWeb object when done to release memory.
//
if( null != newWeb )
{
newWeb.Web.Close();
}
}
}
}
}