1

We are programmatically removing items using item.Delete() in C#. Deleted items that were linked to other items are appearing as broken link enter image description here

When manually deleting in Sitecore we get options like below img enter image description here

But when removed from code it not removing links.How to remove links programmatically before deleting item ?

4 Answers 4

1

When you delete a Sitecore item programmatically using the item.Delete() method in C#, any references to the deleted item will remain in the system, resulting in broken links. To remove these broken links, you can use the Sitecore LinkDatabase.

Here is an example of how you can remove broken links to a deleted item:

var itemToDelete = Sitecore.Context.Database.GetItem("/path/to/item");

// Delete the item
itemToDelete.Delete();

// Get the item's ID
var itemId = itemToDelete.ID;

// Remove all references to the deleted item from the link database
var links = Sitecore.Globals.LinkDatabase.GetItemReferrers(itemToDelete, false);
foreach (var link in links)
{
    var item = link.GetSourceItem();
    if (item != null)
    {
        using (new Sitecore.SecurityModel.SecurityDisabler())
        {
            var field = item.Fields[link.SourceFieldID];
            if (field != null)
            {
                var value = field.Value;
                if (Sitecore.StringUtil.Contains(value, itemId.ToString()))
                {
                    value = value.Replace(itemId.ToString(), string.Empty);
                    item.Editing.BeginEdit();
                    field.Value = value;
                    item.Editing.EndEdit();
                }
            }
        }
    }
}

In this example, We used Sitecore.Globals.LinkDatabase.GetItemReferrers(itemToDelete, false) to get all the items that refer to the deleted item. This should remove all broken links to the deleted item from the link database.

2

Sitecore runs a code similar to this snippet:

public void RemoveItemLinks(Item targetItem, bool removeCloneLinks)
{
    foreach (Item child in targetItem.Children)
        RemoveItemLinks(child, removeCloneLinks);

    foreach (ItemLink referrer in Globals.LinkDatabase.GetReferrers(targetItem))
    {
        if (removeCloneLinks || !(referrer.SourceFieldID == FieldIDs.Source) && !(referrer.SourceFieldID == FieldIDs.SourceItem))
        {
            Item sourceItem = referrer.GetSourceItem();
            if (sourceItem != null && !ID.IsNullOrEmpty(referrer.SourceFieldID))
                RemoveLink(sourceItem, referrer);
        }
    }
}

private static void RemoveLink(Item version, ItemLink itemLink)
{
    CustomField field = FieldTypeManager.GetField(version.Fields[itemLink.SourceFieldID]);

    if (field == null)
        return;

    using (new SecurityDisabler())
    {
        version.Editing.BeginEdit();
        field.RemoveLink(itemLink);
        version.Editing.EndEdit();
    }
}
1

I did a similar kind of thing in my project and here is the approach I used.

Created a method to pass the target item like below.

public void RemoveLinksofTargetItem(Item targetItem)
{
    var links = Globals.LinkDatabase.GetReferrers(targetItem);

    foreach (var link in links)
    {
            Item sourceItem = link.GetSourceItem();
            if (sourceItem != null && !ID.IsNullOrEmpty(link.SourceFieldID))
                RemoveLink(sourceItem, link);
    }
}

Created a method to remove links.

private void RemoveLink(Item item, ItemLink itemLink)
{
    CustomField field = FieldTypeManager.GetField(item.Fields[itemLink.SourceFieldID]);
    if (field != null)
    {
        using (new SecurityDisabler())
        {
            item.Editing.BeginEdit();
            field.RemoveLink(itemLink);
            item.Editing.EndEdit();
        }
    }
}

I referred to this code by extracting the Sitecore.Client DLL that has this logic on choosing the Remove Links as shown in the image in your question.

Hope this works for you.

0

You need to find the link field and then remove it. Try out the below code.

  1. Get the item links
ItemLink[] itemLinks = Globals.LinkDatabase.GetReferrers(item);
  1. Programmatically find and remove item links. You need to loop them to remove links.
ItemLink[] itemLinks = Globals.LinkDatabase.GetReferrers(Context.Database.GetItem(Sitecore.Context.Site.StartPath));

foreach (ItemLink link in itemLinks)
{
     Sitecore.Data.Items.Item linkItem = Context.Database.GetItem(link.SourceItemID);
     if (linkItem == null) continue;

     Field field = linkItem.Fields[link.SourceFieldID];
     if (field == null) continue;

     CustomField customField = FieldTypeManager.GetField(field);
     if (customField == null) continue;

     linkItem.Editing.BeginEdit();
     customField.RemoveLink(link);
     linkItem.Editing.EndEdit();
}

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.