Sample how to use EPiServers CMD LinkCollection property in a more advanced way.
Extract page references
This example extract page references from a LinkCollection.
public static EPiServer.Core.PageReferenceCollection GetLinks(LinkItemCollection value)
{
EPiServer.Core.PageReferenceCollection ret = new PageReferenceCollection();
foreach (LinkItem link in value)
{
Guid id = EPiServer.Web.PermanentLinkUtility.GetGuid(link.Href);
PageReference pageLink =
EPiServer.Web.PermanentLinkUtility.FindPageReference(id);
if (PageReference.IsNullOrEmpty(pageLink))
{
//This sample only extract page references
continue;
}
ret.Add(pageLink);
}
return ret;
}
Programmatically update a LinkCollection
A sample to programmatically add the start page to existing LinkCollection
Var toPage = DataFactory.Instance.GetPage(PageReference.StartPage));
var existingLink = page[Tag.RelatedPages] != null
? (LinkItemCollection)page.Property[Tag.RelatedPages].Value
: new LinkItemCollection();
PageData pd = page.CreateWritableClone();
existingLink.Add(new LinkItem() { Text = toPage.PageName, Href = toPage.LinkURL });
((PropertyLinkCollection)pd.Property[Tag.RelatedPages]).ParseToSelf(existingLink.ToString());
DataFactory.Instance.Save(pd, SaveAction.Publish, AccessLevel.NoAccess);