none
get page layout name of publishing page from a folder

    Question

  • Hello,

    i have created an event receiver in "pages" library and creating a page with custom publishing page layout.

    so on "ItemAdded" event of event receiver, i have to get page layout name.

    it is working perfectly if i create a page directky in library but if i have to create a page under a folder then i am receiving error

    "value doesn't fall within the exacted range" .

    so any idea why it is not working if i am creating page under a folder ?

    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        using (SPWeb oSPWeb = properties.OpenWeb())
        {
            try
            {
                oSPWeb.AllowUnsafeUpdates = true;
                SPListItem item = properties.ListItem;
                SPList list = properties.List;
                string contentTypeName = properties.ListItem.ContentType.Name;
                if (item.ContentType.Name == "Page" || item.ContentType.Name == "Article Page")
                {
                    SPFile file = item.File;
                    if (PublishingPage.IsPublishingPage(item))
                    {
                        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(oSPWeb);
                        PublishingPage publicPage = null;
    
                        PublishingPageCollection pages = pubWeb.GetPublishingPages();
                        //publicPage = pages["http://win-9hb5545kuls:3567/sites/SamLayo/Pages/77.aspx"];
    
                        publicPage = pages[item.Url];//===== here i am getting error ======//
                        if (publicPage != null)
                        {
                            string result = publicPage.Layout.Name;
                            string purl = publicPage.Url;
                        }
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }
    }


    anuj

    Friday, November 9, 2018 1:53 PM

Answers

  • Hi anuj,

    This error is because pages collection not include the files under sub folder, add CAMLQuery to include all the publishing page files wtihin folders like this:

    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        using (SPWeb oSPWeb = properties.OpenWeb())
        {
            try
            {
                oSPWeb.AllowUnsafeUpdates = true;
                SPListItem item = properties.ListItem;
                SPList list = properties.List;
                string contentTypeName = properties.ListItem.ContentType.Name;
                if (item.ContentType.Name == "Page" || item.ContentType.Name == "Article Page")
                {
                    SPFile file = item.File;
                    if (PublishingPage.IsPublishingPage(item))
                    {
                        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(oSPWeb);
                        PublishingPage publicPage = null;
    					SPQuery query = new SPQuery();
    					query.ViewAttributes = "Scope=\"Recursive\""; 
                        PublishingPageCollection pages = pubWeb.GetPublishingPages(query);
                        //publicPage = pages["http://win-9hb5545kuls:3567/sites/SamLayo/Pages/77.aspx"];
    
                        publicPage = pages[item.Url];
                        if (publicPage != null)
                        {
                            string result = publicPage.Layout.Name;
                            string purl = publicPage.Url;
                        }
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }
    }

    Then this error will be resolved.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, November 12, 2018 9:15 AM

All replies

  • Hello,

    can any one please suggest about my issue ?

    i am stuck in development because of this. 


    anuj

    Monday, November 12, 2018 5:42 AM
  • Hi anuj,

    This error is because pages collection not include the files under sub folder, add CAMLQuery to include all the publishing page files wtihin folders like this:

    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        using (SPWeb oSPWeb = properties.OpenWeb())
        {
            try
            {
                oSPWeb.AllowUnsafeUpdates = true;
                SPListItem item = properties.ListItem;
                SPList list = properties.List;
                string contentTypeName = properties.ListItem.ContentType.Name;
                if (item.ContentType.Name == "Page" || item.ContentType.Name == "Article Page")
                {
                    SPFile file = item.File;
                    if (PublishingPage.IsPublishingPage(item))
                    {
                        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(oSPWeb);
                        PublishingPage publicPage = null;
    					SPQuery query = new SPQuery();
    					query.ViewAttributes = "Scope=\"Recursive\""; 
                        PublishingPageCollection pages = pubWeb.GetPublishingPages(query);
                        //publicPage = pages["http://win-9hb5545kuls:3567/sites/SamLayo/Pages/77.aspx"];
    
                        publicPage = pages[item.Url];
                        if (publicPage != null)
                        {
                            string result = publicPage.Layout.Name;
                            string purl = publicPage.Url;
                        }
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }
    }

    Then this error will be resolved.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, November 12, 2018 9:15 AM
  • Hey Jerry,

    thanks, it works.


    anuj

    Tuesday, November 13, 2018 7:55 AM