Updating links in xhtml-fields at runtime

Vote:
 

Hi.

I am trying to update links in xhtml-content during the SavingContent event using the IContentSoftLinkRepository, but cant seem to get it to work. I have been unable to track down any documentation for it so I have just been playing around, trying and failing. One of my (failed) approaches included below. This one increases the number of softlinks tied to a page, but in the xhtml the link does not change.

Any and all pointers would be appreciated.

        private void SavingContent(object sender, ContentEventArgs contentEventArgs)
        {
            var repo = ServiceLocator.Current.GetInstance<IContentSoftLinkRepository>();
            var links = repo.Load(contentEventArgs.ContentLink);
            var modified = new List<SoftLink>();
            
            foreach(var link in links.Where(p => p.SoftLinkType == ReferenceType.PageLinkReference))
            {
                if (link.ReferencedContentLink.ID == 4121)
                {
                    link.ReferencedContentLink = new ContentReference(1441);
                    modified.Add(link);
                }
            }

            repo.Save(contentEventArgs.ContentLink,
                      contentEventArgs.Content.Language(),
                      modified,
                      false);
        }
#311388
Oct 24, 2023 7:39
Vote:
 

My current iteration, which permanently changes the target of the link in the softlinkrepo, but the changes are not reflected in the xhtml-field.. IE the old link is still there when I inspect the html in the wysiwyg

        var links = repo.Load(contentEventArgs.ContentLink);

        foreach(var link in links.Where(p => p.SoftLinkType == ReferenceType.PageLinkReference))
        {
            if (link.ReferencedContentLink.ID == 3134)
            {
                var clone = link.ReferencedContentLink.CreateWritableClone();
                clone.ID = 241;
                link.ReferencedContentLink = clone;
            }
        }

        if (links.Count > 0) repo.Save(contentEventArgs.ContentLink, null, links, false);
#311419
Oct 25, 2023 9:35
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.