Need to add only one channel to a page while creating through code

I am creating a page through code and setting only one channelLink to it,
when I publish the page, all channels are active in backoffice.

    var newPage = new Page(_newFieldTemplate.SystemId,
					newListPage.SystemId);

if (fiPage != null)
				{
					newPage.AccessControlList = GetAccesControlList(fiPage);
					newPage.ChannelLinks.Add(new PageToChannelLink(_fiChannel.SystemId));
				}
newPage.Status = ContentStatus.Published;
				_pageService.Create(newPage);

newPage have only one ChannelLinks before calling _pageService.Create(newPage);
image

newPage has now 3 ChannelLinks after calling _pageService.Create(newPage);
image

Litium version: 7

You should try to use the DraftPage entity instead of the Page and then when it is ready you will do a publish on the draft. Then also back office will be updated with the same content as you have publish.

Hi Thanks for the reply,
I tried to run _draftPageService.Publish(newPage);
but exception is thrown “object refrence set to null”.

could you provide any examples or links.

Something like the following

    var newPage = new Page(_newFieldTemplate.SystemId, newListPage.SystemId);
	_pageService.Create(newPage);

    if (fiPage != null)
	{
        var draftPage = _draftPageService.Get(newPage.SystemId).MakeWritableClone();
        draftPage.AccessControlList = GetAccesControlList(fiPage);
        draftPage.ChannelLinks.Clear();
	    draftPage.ChannelLinks.Add(new PageToChannelLink(_fiChannel.SystemId));
        _draftPageService.Update(draftPage);
	}

    _draftPageService.Publish(draftPage);
2 Likes