views:

2640

answers:

3

I'm getting an error when creating a MOSS publishing page (this is a completely clean MOSS install, no site built into it yet). I'm using code which I've found on plenty of blogs, eg:

var pubWeb = PublishingWeb.GetPublishingWeb(Site.RootWeb);
SPContentTypeId ctId = new SPContentTypeId(contentTypeId);
var layouts = pubWeb.GetAvailablePageLayouts(ctId);
var layout = layouts[0];

var url = pageTitle.EndsWith(".aspx") ? pageTitle : pageTitle + ".aspx";
var newPage = pubWeb.GetPublishingPages().Add(url, layout);

But when I do the pubWeb.GetPublishingPages().Add method call I get the following error:

FormatException - Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

I've checked the following:

  • ContentTypeId is valid
  • layout has a value
  • pubWeb.GetPublishingPages().Count == 1

I can't seem to find anything useful via Google nor can I find anything in Reflector which could help.

+1  A: 

Hi,

try adding a site to the farm and then, use this code from social msdn:

public void FillPublishingWebWithPages 
    (string publishingSiteCollection, int pagesToCreate)
        {
        try
        {
           using ( SPSite site = new SPSite( publishingSiteCollection ) )
          {
             using ( SPWeb web = site.OpenWeb() )
             {
                  PublishingSite pubSite = new PublishingSite( site );
                  PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb( web );
                  SPContentTypeId articleContentTypeID = 
                  new SPContentTypeId( "0x010100C568DB52D9D0A14D9B2FDCC96666E9F"+ 
                      "2007948130EC3DB064584E219954237AF3900242457EFB8B242478" );

        PageLayout[] layouts = pubWeb.GetAvailablePageLayouts( articleContentTypeID );
        PageLayout articlePageLayout = layouts[ 1 ];
        // create a temp name...
        string pageName = DateTime.Now.ToString( "yyyyMMdd-HHmmss" );
        // create the specified number of pages
        for ( int i = 0; i < pagesToCreate; i++ )
        {
              PublishingPage newPage = 
pubWeb.GetPublishingPages().Add( string.Format( "{0}_Gend_Page_{1}.aspx", pageName, i ), articlePageLayout );

          newPage.Title = "Hello";
          newPage.ListItem[ "PublishingContactName" ] = "valuetest";    
          newPage.Update();
          newPage.ListItem.File.CheckIn( "created" );
          newPage.ListItem.File.Publish( "created" );
          newPage.ListItem.File.Approve( "created" );
          pubWeb.Update();
        }

        web.Update();

        }

        }

        }

        catch ( Exception ex )

        {

          throw new Exception( 
          "Error in Page CREATION ----FillPublishingWebWithPages----", ex );

        }

        return;

        }

Example Call:

FillPublishingWebWithPages( http://server:12345/sites/test/subsite1/subsite2/Pages/, 5 );
Ric Tokyo
+1  A: 

Check that the web that you are using is a Publishing Web. To quote MSDN's article on GetPublishingWeb:-

Before you use this method, check the IsPublishingWeb method to confirm that publishing behavior is supported on this instance of the SPWeb class. If publishing is not supported on the SPWeb, then the methods and properties of the PublishingWeb wrapper may behave unexpectedly.

// Get the PublishingWeb wrapper for the SPWeb that was passed in.
PublishingWeb publishingWeb = null;
if (PublishingWeb.IsPublishingWeb(web))
{
    publishingWeb = PublishingWeb.GetPublishingWeb(web);
}
else
{
    throw new System.ArgumentException("The SPWeb must be a PublishingWeb", "web");
}
AboutDev
+1  A: 

I found out what the problem was, the ContentType I was using was corrupt. Because of another problem I'm having deploying ContentTypes (see this question) I'm programatically creating the ContentType, but deploying the PageLayout with CAML. This resulted in the AssociatedContentType being incorrect, so when I created the page using it MOSS was unable to determine what ContentType to use and fell over.

Slace