Saturday, August 6, 2011

Value does not fall within the expected range - Edit aspx in MOSS 2007

When you edit an aspx page in MOSS 2007 you might get "Value does not fall within the expected range". One of the reason for this issue is the incorrect top-level site URL of the publishing page. If you download the page & open it in notepad, you can find the below line consisting of incorrect top level site URL.
Just update it and upload the page again, your issue should be resolved.

<mso:PublishingPageLayout msdt:dt="string">http://incorrect_servername/_catalogs/masterpage/PageLayout.aspx, Article page</mso:PublishingPageLayout>


If you have many pages with the sample problem, manually updating each & every page could be tedious. In such case you can execute the sample, which will save your time.



const string MASTER_PAGE_LIB = "_catalogs/masterpage";

            string SiteCollectionUrl = "http://siteURL/";

            using (SPSite site = new SPSite(SiteCollectionUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    try
                    {

                        if (PublishingWeb.IsPublishingWeb(web))
                        {
                            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);


                            foreach (PublishingPage page in pubWeb.GetPublishingPages())
                            {

                                string pageLayout = page.ListItem.Properties["PublishingPageLayout"] as string;


                                if (pageLayout != null)
                                    pageLayout = pageLayout.ToLower();

                                if (String.IsNullOrEmpty(pageLayout))
                                {
                                    Console.WriteLine("Error: Page \"{0}\" does not have a Page Layout assigned.\n", page.Uri);
                                }
                                else if (!pageLayout.Contains(MASTER_PAGE_LIB))
                                {
                                    Console.WriteLine("Error: The Page Layout {0} for Page \"{1}\" does not point to the masterpage document library.\n",
                                        pageLayout, page.Uri);
                                }
                                else if (!pageLayout.StartsWith(SiteCollectionUrl) && pageLayout.StartsWith("http"))
                                {
                                    // here we have a page which has a page layout that has a different URL which we have to fix
                                    Console.WriteLine("Page {0} has incorrect PageLayout Url", page.Uri);
                                    Console.WriteLine("Old URL: {0}", pageLayout);
                                    string pageLayoutWithoutPrefix = pageLayout.Substring(pageLayout.IndexOf(MASTER_PAGE_LIB));
                                    string newPageLayout = SiteCollectionUrl + pageLayoutWithoutPrefix;

                                    try
                                    {
                                        int version = page.ListItem.File.MinorVersion;
                                        page.CheckOut();
                                        page.ListItem.Properties["PublishingPageLayout"] = newPageLayout;
                                        page.ListItem.File.Properties["PublishingPageLayout"] = newPageLayout;
                                        page.ListItem.Update();
                                        page.CheckIn("PublishingPageLayout corrected");

                                        //major version means that the item was published. Let's publish it again.
                                       if (version == 0)
                                            page.ListItem.File.Publish("PublishingPageLayout corrected");

                                        Console.WriteLine("Fixed URL: {0}\n", newPageLayout);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("An error occurred while trying to fix the URL to the page layout:\n{0}", e);
                                    }
                                }
                            }
                        }//if Publishing web
                    }
                }
            }


There is a KB available explicitly for this issue. http://support.microsoft.com/kb/953445
The above sample is based on the KB only.

Hope it helps :)

No comments:

Post a Comment