Link Page to a Second Site via API

Sharon Parry asked on July 19, 2022 22:35

This is a v9.0.51 project. Not possible to upgrade so have to work with what we have.

There are >60 sites in the application, with one of them acting as the "master" site. A lot (but not all) of the content from the master site is linked through to some or all of the other sites, and the other sites can also have their own individual content.

It's way too time consuming creating one-off linked pages through to all the relevant sites every time something new is added, so I'm trying to find a way for the site administrator to be able to create linked pages via the api.

Using the example from the documentation, I've tried the following, but the "parentPage" (the parent node on another site which will be the parent of the linked node) is always null. Am I doing something wrong? Or is it not possible to create a linked page on another site?

// Creates an instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
SiteInfoIdentifier secondSite = new SiteInfoIdentifier("anothersite");
// Gets the page that will be linked
TreeNode originalPage = tree.SelectNodes()
    .Path("/My-Dashboard/Events/Last-One")
    .OnCurrentSite()
    .Culture("en-au")
    .FirstObject;
// Gets the page that will be used as a parent page for the linked page
    TreeNode parentPage = tree.SelectNodes()
    .Path("/My-Dashboard/Events")
    .OnSite(secondSite)
    .Culture("en-au")
    .FirstObject;
if ((originalPage != null) && (parentPage != null))
{
    // Inserts a new linked page under the parent page
    originalPage.InsertAsLink(parentPage);
}

Correct Answer

Brenden Kehren answered on July 20, 2022 00:45

Then start digging into your variables.

Make sure the site code name is correct.

Make sure there is actually a page on the path you're specifying in the pPage object.

Make sure the culture you're working with is assigned to the given site in pPage.

All of the above will result in a null object.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on July 19, 2022 23:13

Does your authenticated user have access to both sites?

Does the page type have permissions to be created on the site you're copying it to?

I'm willing to be the user does not have permissions but could be wrong.

0 votesVote for this answer Mark as a Correct answer

Sharon Parry answered on July 19, 2022 23:17

Yes, user has full administrator privileges on ALL sites, and all sites have the page type assigned.

Currently I'm just trying to get it to work for just a single site.

0 votesVote for this answer Mark as a Correct answer

Sharon Parry answered on July 19, 2022 23:18

As a matter of fact, I'm currently trying to get it to work as the Global Admin!

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 19, 2022 23:20 (last edited on July 19, 2022 23:21)

I'd verify the MembershipContext.AuthenticatedUser is truly the global admin and not someone else.

If it is, then it's likely linked pages are not allowed between sites.

0 votesVote for this answer Mark as a Correct answer

Sharon Parry answered on July 19, 2022 23:29

Changed to:

UserInfo ui = UserInfoProvider.GetUserInfo("administrator");
TreeProvider tree = new TreeProvider(ui);

ui is definitely Global Admin:

{CMS.Membership.UserInfo}: "administrator" ("Global Administrator") - {9842647c-37e2-4f9b-bc4b-0ad94a9fc92e}

Can manually link pages through to multiple sites. Have been doing that with all types of pages (ie, tree nodes) with ALL the sites for the last 7 or 8 years! No problem creating them manually - even linking a single node and selecting to link all child nodes.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 20, 2022 00:24

Is the site identifier actually finding a valid running site in your instance?

Not that it's much different but worth a try.

var oPage = DocumentHelper.GetDocuments()
    .Path("/My-Dashboard/Events/Last-One", PathTypeEnum.Single)
    .OnCurrentSite()
    .Culture("en-au")
    .FirstOrDefault();

var pPage = DocumentHelper.GetDocuments()
    .Path("/My-Dashboard/Events", PathTypeEnum.Single)
    .OnSite("SiteCodeName")
    .Culture("en-au")
    .FirstOrDefault();

if (oPage != null && pPage != null)
{
    oPage.InsertAsLink(pPage);
}

Sometimes I've found .SelectNodes() doesn't work as I want

0 votesVote for this answer Mark as a Correct answer

Sharon Parry answered on July 20, 2022 00:41 (last edited on July 20, 2022 00:42)

Hmmmm, I got a bit excited then because the logic of that seemed to be ok, but sadly, NO, pPage is always null!

0 votesVote for this answer Mark as a Correct answer

Sharon Parry answered on July 20, 2022 01:07

OMG - the page (node) on the second site was changed, but the original "/My-Dashboard/Events" path still existed as an alias. As soon as I changed that line to the newer page alias, it works - even the original code!

I will just have to impress on the administrator that the path of the relevant parent page on the second site(s) should not change.

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 20, 2022 01:09

So you could leave those as parameters and allow them to select that page on the site they want the linked page to happen on. May be worthwhile vs. worrying about hard coding it.

0 votesVote for this answer Mark as a Correct answer

Sharon Parry answered on July 20, 2022 01:16

Yes, but the idea is that they select a single item from the "Master" (in this case an Event) and then select all the sites from a checkboxlist where that Event should be linked to, and "Go". Linked pages will be created on all the selected sites - not just a single site.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.