2011-08-04 14 views
0

サブサイトのカスタムレイアウトページからパブリッシュサイト(Microsoft.SharePoint.Publishing.PublishingWeb)のすべてのページレイアウトを取得しようとすると、最近このエラーが発生しました。このコードは、エンタープライズWikiサイトテンプレートから作成されたサイトを使用してVMサーバー上で動作しました。しかし、開発サーバー上で実行しているときには、コードでさまざまな例外が発生しました。"GetAvailablePageLayouts"でNullReferenceExceptionがスローされました

エラーを回避しようとすると、クラスを3つの異なる方法でコーディングしました。 3人すべてがVMで働いていましたが、3人すべてが開発サーバーで使用されたときに例外を投げました。例:

はあなたがしていることPublishingWeb.GetPublishingWeb(SPWeb)メソッドを呼び出すことで、あなたの「PublishingWeb」オブジェクトを取得しているときことを確認してください:ここで

private PageLayout FindPageLayout(PublishingWeb pubWeb, string examplePage) 
     { 
      /* The error occurs in this method */ 
      if (pubWeb == null) 
       throw new ArgumentException("The pubWeb argument cannot be null."); 
      PublishingSite pubSiteCollection = new PublishingSite(pubWeb.Web.Site); 
      string pageLayoutName = "EnterpriseWiki.aspx"; // for testing purposes 
      PageLayout layout = null; 

      /* Option 1: Get page layout from collection with name of layout used as index 
      * Result: System.NullReferenceException from GetPageLayouts() 
      */ 

      layout = pubSiteCollection.PageLayouts["/_catalogs/masterpage/"+pageLayoutName]; 

      /* Option 2: Bring up an existing publishing page, then find the layout of that page using the Layout property 
      * Result: Incorrect function COM exception 
      */ 

      SPListItem listItem = pubWeb.Web.GetListItem(examplePage); 
      PublishingPage item = PublishingPage.GetPublishingPage(listItem); 
      layout = item.Layout; 

      /* Option 3: Call "GetPageLayouts" and iterate through the results looking for a layout of a particular name 
      * Result: System.NullReferenceException thrown from Microsoft.SharePoint.Publishing.PublishingSite.GetPageLayouts() 
      */ 

      PageLayoutCollection layouts = pubSiteCollection.GetPageLayouts(true); 
      for(int i = 0; i < layouts.Count; i++) 
      { 
       // String Comparison based on the Page Layout Name 
       if (layouts[i].Name.Equals(pageLayoutName, StringComparison.InvariantCultureIgnoreCase)) 
        layout = layouts[i]; 
      } 

      return layout; 
     } 

答えて

1

は、私は一週間かそこら後に発見されたソリューションでしたそれに完全に取り込まれたSPWebオブジェクトを渡します。次のように具体的には、私は、任意のウェブサイト上でSPSite.OpenWebを呼び出してくださいになるだろう:

using (SPSite site = new SPSite(folder.ParentWeb.Url)) 
      { 
       SPWeb web = site.OpenWeb(); 
       PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); 
       /* work with PublishingWeb here ... */ 
       web.Close(); 
      } 

私はこの単純な変更を行ったら、質問に記載されたすべてのエラーは、どのような文脈でどんなにをクリアしたI 「GetPageLayouts」または「GetAvailablePageLayouts」と呼ばれます。 method documentationは、この氏は述べています、そしてそれは実際にそれを意味します

すでに取得されたSPWebクラスの 例えばPublishingWebの振る舞いにアクセスするために、このメソッドを使用します。

関連する問題