【问题标题】:Programmatically creating a MOSS publishing page以编程方式创建 MOSS 发布页面
【发布时间】:2009-02-12 11:19:39
【问题描述】:

创建 MOSS 发布页面时出现错误(这是一个完全干净的 MOSS 安装,尚未内置任何站点)。我正在使用我在很多博客上找到的代码,例如:

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);

但是当我调用pubWeb.GetPublishingPages().Add 方法时,我收到以下错误:

FormatException - 索引(从零开始)必须大于或等于零且小于参数列表的大小。

我已经检查了以下内容:

  • ContentTypeId 有效
  • 布局有值
  • pubWeb.GetPublishingPages().Count == 1

我似乎无法通过 Google 找到任何有用的信息,也无法在 Reflector 中找到任何可以提供帮助的信息。

【问题讨论】:

    标签: sharepoint moss


    【解决方案1】:

    我发现了问题所在,我使用的 ContentType 已损坏。由于另一个问题,我正在部署 ContentTypes (see this question),我正在以编程方式创建 ContentType,但使用 CAML 部署 PageLayout。这导致 AssociatedContentType 不正确,因此当我使用它创建页面时,MOSS 无法确定要使用的 ContentType 并失败了。

    【讨论】:

    • 谢谢!正是我需要的:)
    【解决方案2】:

    检查您正在使用的网站是否为发布网站。引用MSDN在GetPublishingWeb上的文章:-

    在使用此方法之前,请检查 IsPublishingWeb 方法来确认 支持发布行为 SPWeb 类的这个实例。如果 不支持发布 SPWeb,然后是方法和属性 PublishingWeb 包装器的 行为出乎意料。

    // 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");
    }
    

    【讨论】:

      【解决方案3】:

      尝试将站点添加到服务器场,然后使用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;
      
              }
      

      示例调用:

      FillPublishingWebWithPages( http://server:12345/sites/test/subsite1/subsite2/Pages/, 5 );
      

      【讨论】:

        猜你喜欢
        • 2010-11-23
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 2011-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多