# The correct way to add pages by code

**URL:** https://forum.litium.com/t/the-correct-way-to-add-pages-by-code/1398
**Category:** Questions
**Created:** [March 23, 2020, 10:44am UTC](https://forum.litium.com/t/the-correct-way-to-add-pages-by-code/1398 "2020-03-23T10:44:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![albarest](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/albarest/32/369_2.png) [@albarest](https://forum.litium.com/u/albarest)
#### Post date: [March 23, 2020, 10:44am UTC](https://forum.litium.com/t/the-correct-way-to-add-pages-by-code/1398/1 "2020-03-23T10:44:56Z")

</div>

What is the correct way of adding a page by code?  
Create a page.  
Set Visitor access for the page and all subpages.  
Set Write Access to Admins for the page and all subpages  
Handling different versions, publish, save a draft, etc.

Can’t find it in the documentation

Litium version: 7.4

Thank you!

---

<div class="post-metadata">

### Author: ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)
#### Post date: [March 24, 2020, 10:01am UTC](https://forum.litium.com/t/the-correct-way-to-add-pages-by-code/1398/2 "2020-03-24T10:01:14Z")

</div>

Some example here:

```auto
var page = new Page(fieldTemplateSystemId, parentPageSystemId);
            page.AccessControlList.Add(new AccessControlEntry(new Operation(Operations.Entity.Read), visitorGroupSystemId));
            page.AccessControlList.Add(new AccessControlEntry(new Operation(Operations.Entity.Write), adminGroupSystemId));
            page.Status = Common.ContentStatus.Published;
            _pageService.Create(page);

```

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [March 24, 2020, 10:04am UTC](https://forum.litium.com/t/the-correct-way-to-add-pages-by-code/1398/3 "2020-03-24T10:04:52Z")

</div>

In order for back office to have the same content from the changes you made in code, a good idea is to make the changes to a DraftPage of the new page you created and then publish that one.

```auto
var draftPage = _draftPageService.Get(page.SystemId).MakeWritableClone();
// make changes
_draftPageService.Update(draftPage);
_draftPageService.Publish(draftPage);

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)
#### Post date: [April 21, 2020, 10:05am UTC](https://forum.litium.com/t/the-correct-way-to-add-pages-by-code/1398/4 "2020-04-21T10:05:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
