22 Jun 2010

SetAvailablePageLayouts generates “Failed to compare two elements in the array”

Posted by Alexander

SharePoint offers a great method on the PublishingWeb object called SetAvailablePageLayouts. With this method you are able to set the list of page layouts available to the users of your SharePoint website. Mostly we use this method inside the FeatureActivated method of an SPFeatureReceiver derived class. The code is as follow:

System.Collections.Generic.List<PageLayout> allowedPageLayouts = new System.Collections.Generic.List<PageLayout>();
allowedPageLayouts.Add(publishingSite.PageLayouts["/_catalogs/masterpage/Home.aspx"]);
allowedPageLayouts.Add(publishingSite.PageLayouts["/_catalogs/masterpage/Article.aspx"]);
allowedPageLayouts.Add(publishingSite.PageLayouts["/_catalogs/masterpage/News.aspx"]);
allowedPageLayouts.Add(publishingSite.PageLayouts["/_catalogs/masterpage/FAQ.aspx"]);
publishingWeb.SetAvailablePageLayouts(allowedPageLayouts.ToArray(), true);

Last week i did implemented this again for a project and it causes a nasty error “Failed to compare two elements in the array”. Looking on the internet was not really helpfull till somebody pointed me out that it could be caused by an invalid page i was trying to set as available. The strange thing was that i tested all page templates and could not find any mistake.

After some time i suddenly saw the problem. The page templates where added using a feature. In the elements.xml file each page template was defined by a <Module />, <File /> and <Property /> elements. One of the properties refers to a Content Type by its name and its Id. It seems that i forgot an# sign  in the PublishingAssociatedContentType element.

<File Path="News.aspx" Url="News.aspx" Type="GhostableInLibrary">
<Property Name="Title" Value="$Resources:mysolution,PageLayout_News_Title" />
<Property Name="MasterPageDescription" Value="$Resources:mysolution,PageLayout_News_Description;" />
<Property Name="ContentType" Value="$Resources:mysolution,ContentType_News_DisplayName" />
<Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/News.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/News.png" />
<Property Name="PublishingAssociatedContentType" Value=";#$Resources:svn,ContentType_SVnNews_Name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D0041b83af6a3dd4e90a5e3552f14065687008437ce3f2cca491eadb900969009f447;#"/> </File>

It still added the page template and looking closer to the template list you could see that it wasn’t attached to its Content Type. Hopefully this post will help you when you encounter the same error in your solution.

Leave a Reply

Message: