0

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

このエラーを回避するために、クラスを 3 つの異なる方法でコーディングしました。3 つすべてが VM で機能しましたが、開発サーバーで使用すると、3 つすべてが例外をスローしました。例:

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;
        }
4

1 に答える 1

1

これが私が一週間かそこら後に見つけた解決策でした:

完全に取得されたSPWebオブジェクトを渡すPublishingWeb.GetPublishingWeb(SPWeb)メソッドを呼び出して、「PublishingWeb」オブジェクトを取得していることを確認してください。具体的には、次のように、どのWebサイトでも必ず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();
             }

この簡単な変更を行うと、「GetPageLayouts」または「GetAvailablePageLayouts」と呼んだコンテキストに関係なく、質問に記載されているすべてのエラーがクリアされました。メソッドのドキュメントにはこれが記載されており、実際には次のことを意味します。

すでに取得されているSPWebクラスのインスタンスのPublishingWeb動作にアクセスするには、このメソッドを使用します。

于 2011-08-04T20:57:05.213 に答える