Developer forum

Forum » Development » Creating Pages and Paragraph with the API

Creating Pages and Paragraph with the API

Daniel Hollmann
Reply

Hi.

 

I have a business case, where we must create pages based on external events. I then must create a page with some standard paragraph using code and the Dynamicweb API.

I have gotten some way with this code.

internal static bool CreateEventPage()

        {

            try

            {


                IPageService service = new PageService();

                IParagraphService paragraphService = new ParagraphService();

                Page newPage = new Page(6, 484);

                newPage.ItemType = "myItemType";

                var pageId = service.SavePage(newPage).ID;

                

                // Item types

                newPage.Item["Title"] = "From My Api -----!";

                newPage.Item["Thumpnail_Title"] = "Thumpnail";

                newPage.Item["TeaserText"] = "Teaser!";

                newPage.Item["Thumpnail_Alt_Text"] = "Alt text";

                newPage.Item.Save();


                //Works fine to this point!


                //Paragraph

                var MyParagraph = new Paragraph(pageId);

                MyParagraph.ItemType = "R4_1ColumnFull";

                paragraphService.SaveParagraph(MyParagraph);

                return true; 

            }

            catch(Exception ex)

            {

                ErrorLogger.LogError("Failed to create event", ex);

                return false;

            }

        }

 

But when creating paragraphs, I get into some trouble. I’m trying to create a page that supports the visual builder, but when my paragraph is saved it is saved like this:

(See attachement)
 

As you can I’m trying to first create a row that is used in Visual builder, but instead of it being part of the “Grid” it is added to the “Not in template” area of the page, which is not shown. Is there any way to add to a row to a grid on a page, and then add a paragraph to that row through code?



Best regards
Daniel

DW_Photo.png

Replies

 
Nicolai Pedersen
Reply

You also have to create a gridrow on that page: https://doc.dynamicweb.com/api/html/9061f0a2-8a54-7232-fccb-14f250733ece.htm using the gridservice: https://doc.dynamicweb.com/api/html/74367174-96f6-d0e3-2b17-e51362e00dc5.htm

The gridrow has a page id which has to match your page of course.

Then the paragraph you are creating needs the GridRow.ID to be on that row: https://doc.dynamicweb.com/api/html/260b9c71-8c7a-55b0-8b96-cf94a690afa1.htm

And then you also have to specify GridRowColumn: https://doc.dynamicweb.com/api/html/5d701666-e654-79bf-e774-e5bffd4148dc.htm - if your row has 2 columns you can create 2 paragraphs - with GridRowColumn=1 and GridRowColumn=2

 
Daniel Hollmann
Reply

Thanks! I will post my code, once I have everything working!

 
Daniel Hollmann
Reply

I might go for a different strategy, where I create a template and then using that to create the new page - editing the properties.

This would be simple to the function Create new Page and then selecting a template.

However I have not found an easy way to do this using the API. I can find the template using the IPageService method "GetPageTemplatesForArea", but how do I then create a page based on that template? Seems there are no easy way through the IPageSerivce?

 
Nicolai Pedersen
Reply

A template is rendering. So that does not describe anything about the underlying item type (Except it renders the data of the itemtype).

 
Daniel Hollmann
Reply


You can also save a page as a template (Under Tools, Save as A Template) , to use for creating other pages.

This is the behavior I want to mimic using code:

https://www.screencast.com/users/daniel9094/folders/Default/media/d06bb202-69d9-4307-badd-a4e4ea24a7f2

 
Nicolai Pedersen
Reply

Ah.

IsTemplate on the page is the property to look at.

The easiest way is to create a page template and look at it in the database and see what values the fields has. Its parentid has to be correct as well - the templates folder in the tree is also a page record.

BR Nicolai

 
Daniel Hollmann
Reply

Ok, that will take a lot of mapping, but alright :)

I also tried this approach  
There is a method in the IpageService called : "CopyTo" - https://doc.dynamicweb.com/api-docs#article=21ca9ee9-ff9b-c5ad-9c69-818bbdf0e92c 
That take a "CopyPageInfo" object https://doc.dynamicweb.com/api-docs#article=344b4676-50f0-6ff6-5d4d-1983dbdb5ffe

But Every time I use that, I get the error message "Standard Page is not allowed." 
I only have the names of the methods to go by, since no documentation for the method is mention in the documentation API.

 
Nicolai Pedersen
Reply

It is because you are hit by item restrictions. That depends on your item restriction setup and you might have the setting "Disable standard page":

See https://doc.dynamicweb.com/documentation-9/platform/advanced-settings/editing#3525

And restrictions: https://doc.dynamicweb.com/documentation-9/content/items/item-types#2396

 
Daniel Hollmann
Reply

Great. Using the "Disable Standard Page" setting, I managed to get the "CopyTo" method to work. (Eventhough the page i'm copying is not a stanard page)

It's not pretty code, and it needs proper error handling but I managed to get something working with this code:
 

        internal static bool CopyPageFromTemplate() //Eventually will take some parameters

        {

            try

            {


                IPageService service = new PageService();

                IParagraphService paragraphService = new ParagraphService();

                ItemService itemService = new ItemService();

                Page PageTemplate = service.GetPagesByTitle("MyTemplate").FirstOrDefault();


                if(PageTemplate == null)

                {

                    return false;

                }


                // Copy Page to Sub Page

                CopyPageInfo copyInfo = new CopyPageInfo(PageTemplate.ID, 484); //484 is the id of the parrentpage

                copyInfo.CopyParagraphs = true;

                var applicationResponse = service.CopyTo(copyInfo);


                // Editing the Page Item Attributes.

                if(applicationResponse.Succeeded)

                {

                    var newPage = service.GetPage(applicationResponse.Data.ID);

                    newPage.Item["Title"] = "New Title from Backend Service";

                    newPage.Item["Thumpnail_Title"] = "Thumpnail From Service";

                    newPage.Item["TeaserText"] = "Updated from Service";

                    newPage.Item["Thumpnail_Alt_Text"] = "Alt text from Service";

                    newPage.Publish();

                    newPage.Item.Save();


                // Editing the paragraph Attributes:

                IEnumerable<Paragraph> allParagraphsForPage = paragraphService.GetParagraphsByPageId(newPage.ID);


                    foreach (Paragraph paragraph in allParagraphsForPage)

                    {

                        if (paragraph.ItemType.Equals("My_First_Item_Type"))

                        {

                            paragraph.Item["Headline"] = "Set from PageCreation Service";

                        }

                        else if (paragraph.ItemType.Equals("My_Second_Item_Type"))

                        {

                            paragraph.Item["Title"] = "Set from PageCreation Service";

                            paragraph.Item["Text_Header"] = "Page Creation is setting this";

                            paragraph.Item["Body_Text"] = "This is my Body Body Set From PageCreation Service";

                            paragraph.Item["External_ID"] = "823981";

                        }

                        itemService.SaveItem(paragraph.Item);

                        paragraphService.SaveParagraph(paragraph);

                    }

                }

                

                return true;

            }

            catch (Exception ex)

            {

                ErrorLogger.LogError("Failed to create event", ex);

                return false;

            }

        }

 

Thanks for the Help, and hopefully this will also help other! :)

 
Nicolai Pedersen
Reply

Glad you made it work - and thank you for sharing!

 

You must be logged in to post in the forum