SaaS CMS has officially launched! Learn more now.

Update LinkItemCollection



I have created a scheduled job that is about to update pages in EPiServer that contains properties of type LinkItemCollections and has LinkItems containing a certain ahref.

Is for now my code looks similair to this:

var writableClone = page.CreateWritableClone();

for (int i = 0; i < writableClone.Property.Count(); i++)
   if (writableClone.Property[i] is EPiServer.SpecializedProperties.PropertyLinkCollection)

     var propertyLinkCollection = writableClone.Property[i] as EPiServer.SpecializedProperties.PropertyLinkCollection;

     if (propertyLinkCollection != null && propertyLinkCollection.Links != null)


         foreach (LinkItem linkitem in propertyLinkCollection)

               if (linkitem.Href == match)
                  linkitem.Href = newLinkahref; // Here I like to set my new value to the found match

           // I have tried republishing the page here, not very optimized, rather add all pages to a collection and after all updates is made republish the complete collection of pages




My question is, is this even possible, I can't get it to work. The ahrefs in the LinkItemCollections aren't updated. As long as I have a writeable clone of the page, all properties should be editable on that page a guess.

Do I have to a new LinkCollectionProperty to the corresponding page and republish the page with updated values?

Feb 24, 2016 21:27

The last question is, do I have to create a new LinkItemCollection with updated values and add this to the current page?

Feb 24, 2016 21:32

Does it work if you run it manually (as admin). Scheduled job normally runs as anonymous user if you haven't done anything when running automatically so that is a possible issue if you get any errors?

If that's the case use 

PrincipalInfo.CurrentPrincipal = PrincipalInfo.CreatePrincipal("your.username");

To let the job run as your.username...

Also check that you are actually publishing page and not just saving it. Use 

ContentRepository.Save(clone, SaveAction.Publish);

Or similar...

Do you get any errors in log?

Feb 24, 2016 23:36

Hi yes the job is elevated as Administrator.

if (HttpContext.Current == null)
PrincipalInfo.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity("Scheduled Job - UpdateLinkItemCollection"),
new[] { "Administrators" });


contentRepository.Save(writableClone, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

No errors in EPiServer

Feb 25, 2016 11:53

Tried remove the link and then save it? If that works the simply add the new link after removing the bad one. 

If not, try clone the link collection first and do the same procedure as work around...Might be that episerver fails to detect the change deep within an existing property since the object reference to the link collection will stay the same. DotPeek will know  :)

Edited, Feb 25, 2016 12:01

Have you debugged the code and actually seen that it goes in and finds the match? Don't know if this will be any better, but you can try to cast the value of the property to LinkItemCollection (writableClone.Property[i].Value as LinkItemCollection) and foreach throught that.. Or maybe try to foreach propertyLinkCollection.Links instead? Just a shot in the dark here without actually trying it out :)

Feb 25, 2016 12:18

Tim yes I log the exact match when iterating the Linkcollection.

Daniel I think the best approach is to clone the collection and when all LinkItems is handled I republish page with the new updated clone.

Feb 25, 2016 13:48

I think that will work yes Christer. Let me know if it works out :)

Feb 25, 2016 17:05

Thanks Tim, you are right

It only works if you access it like (writableClone.Property[i].Value as LinkItemCollection) and changing each LinkItem has effect and it updates database.
All other options do not updated values, and they remain the same.

Edited, Aug 26, 2016 15:58

Without actually testing myself I would guess that the issue is that the property itself is not marked as modified when an item in the collection is changed (it is probably just tracking add/delete not modify of an individual item). If so setting property.IsModified = true should make it persisted.

Aug 26, 2016 16:25
* 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.