Publishing from code doesn't update references

Vote:
 

Hi there!

I have a fairly straght forward code that replaces all references to page A with reference to page B in an Episerver 11 solution. The code below is simplified.

First I loop through all references with reference to page A

_repository.GetReferencesToContent(new ContentReference(pageA_id), true)

Then I fetch the original page in correct language, create clone, alter and save.

var original = _repository.Get(new ContentReference(referencePage_id), new CultureInfo(referencePage_lang));
var clone = (ContentData)original.CreateWritableClone();
clone["RelatedPage"] = new PageReference(pageB_id);
_repository.Save((IContent)clone, SaveAction.Publish, AccessLevel.NoAccess);

All of this works. Referenced page is published and now has reference to page B. However, if I try to delete page A or run GetReferencesToContent again I will still get that Refrences page has a reference to page A. If I in Edit mode alter the referenced page and publish, the reference will finally update correctly and reference to page A is finally gone. 

Am I missing something in my code that updates the references? I thought this was done by Episerver automatically when I do a Publish from code!?

#196844
Edited, Sep 13, 2018 9:46
Vote:
 

I encountered a similar issue to this which, for me, turned out to be down to the save action.  In addition to publish, I needed additional actions to force the new page.

clone["RelatedPage"] = new PageReference(pageB_id);
var saveAction = SaveAction.CheckIn | SaveAction.Publish | SaveAction.ForceNewVersion;
_repository.Save((IContent)clone, saveAction, AccessLevel.NoAccess);
#196896
Sep 14, 2018 15:21
Vote:
 

I'm afraid that didn't work Darren.

References are still there. When I try to delete the old page it still shows references in other pages that I know don't have reference to old page. When I manually publish these in Edit mode the reference is removed.

#196988
Sep 19, 2018 9:36
Vote:
 

I'm afraid that didn't work Darren.

References are still there. When I try to delete the old page it still shows references in other pages that I know don't have reference to old page. When I manually publish these in Edit mode the reference is removed.

#196989
Sep 19, 2018 9:36
Vote:
 

I would think it's correct that old versions linking to a page would still be kept as references to content. But if it works when you publish manually I guess it's not.

Could also have something to do with other language versions of the content.

Maybe you need to touch the mark as changed property?

clone["PageChangedOnPublish"] = true;
#196996
Sep 19, 2018 11:25
Vote:
 

I'm afraid that didn't work either. References are still there. I'm actually already editing pages in the language that contains the reference so I believe I got that part correct at least. 

#197000
Sep 19, 2018 12:12
Vote:
 

Yes, that did it for me Johan. I created a method that updates all the softlinks and it worked. However, ContentSoftLinkIndexer is unsupported internal API. So use it carefully and at your own risk. It might change without notice. So do test it after Episerver upgrade.

public void UpdateSoftLinks(List<ReferenceInformation> referenceList)
{
	foreach (var reference in referenceList)
	{
		var content = _repository.Get<IContent>(reference.OwnerID, reference.OwnerLanguage);
		var links = _contentSoftLinkIndexer.GetLinks(content);
		_contentSoftLinkRepository.Save(content.ContentLink.ToReferenceWithoutVersion(), content is ILocalizable ? reference.OwnerLanguage : null, links, false);
	}
}
#197020
Edited, Sep 19, 2018 14:47
Vote:
 

For me this was not working but i managed to manually change the reference to the new page by adding the following code: 

foreach (SoftLink link in links)
            {
                if (link.ReferencedContentLink == oldPage.ContentLink)
                    link.ReferencedContentLink = newPage.ContentLink;
            }

Thank you guys. 

#197022
Sep 19, 2018 15:05
Vote:
 

What you want is to trigger softlink save, not replace it manually. If pages are still showing among references you might also have missed replacing some of the old page references.

#197025
Sep 19, 2018 15:20
Vote:
 

In the meantime I made it work as it should. Now the softlink save is triggered. The manual update was a backup solution.

#197056
Sep 20, 2018 11:37
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.