12

Is there a way, in one fell swoop, to remove all references to a item?

I know that it's possible to view a list of referring items from the report but is there anything out-of-box to handle the removal of its attachments in bulk or am I forced to go through the list removing references one at a time? I don't want to delete the item, just its association to other items.

4 Answers 4

15

Not quite. But here's what I would do.

  1. Serialize your item
  2. Delete the original item, instructing Sitecore to delete all references
  3. Deserialize your item

Requires access to the Developer Tab, but is completely OOTB.

Serialize Item

EDIT: updated to include a few additional ways to go about it

Packager

Doesn't require access to serialization tools.

  1. Package your item
  2. Delete the original item, instructing Sitecore to delete all references
  3. Install your item package

Package Item

Content Editor

Works, but will give your item a new ID.

  1. Duplicate your item via Content Editor
  2. Delete the original item, instructing Sitecore to delete all references
  3. Rename your duplicated item back to original name

Duplicate Item

Publish/Transfer

For super users.

  1. Publish your item
  2. Delete the original item, instructing Sitecore to delete all references
  3. Transfer your item back to master via Content Editor

Transfer Item

7

This doesn't satisfy the out-of-the-box requirement but may prove useful if that criteria doesn't apply in other situations.

Here's a method using Sitecore Powershell Extensions:

$item = Get-Item -Path "master:/sitecore/content/path/to/your/item"

$linkDb = [Sitecore.Globals]::LinkDatabase

$links = $linkDb.GetReferrers($item)

foreach($link in $links) {
    $linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID 
    $itemField = $linkedItem.Fields[$link.SourceFieldID]
    $field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)

    $linkedItem.Editing.BeginEdit()
    $field.RemoveLink($link)
    $linkedItem.Editing.EndEdit()
}

This uses the LinkDatabase to iterate over the references to your item, it then gets the field that is referencing the item and calls the RemoveLink method on it. This is the same underlying method that the Relink dialog uses.

Extra credit to Michael West for the basic method as seen in this gist - https://gist.github.com/michaellwest/f563b0b3597f6c0a75d6

2
  • 1
    Powershell, useful as it is, doesn't satisfy the "out of the box" criteria on the question however.
    – Mark Cassidy
    Commented Oct 13, 2016 at 16:35
  • This is true, as that's in the detail and not in the question title, this still might be found for someone looking to do the same without that requirement. Will edit my answer to make this clearer.
    – Kasaku
    Commented Oct 13, 2016 at 16:47
4

Tricky answer :

Please perform the following steps: 1. Open the Content Editor application.

  1. Select the item, links on which you want to delete.

  2. Press the delete button in the home tab on the ribbon. The Breaking Links dialog box appears.

  3. Press the Edit Links button. The Edit Links dialog box appears.

  4. In the Edit Links dialog box you will see the list of all items that reference to this item.

  5. Press the Remove Link button for each item.

  6. Close the Edit Links dialog box by using Ok button.

  7. Close the Breaking Links dialog box by using Cancel button.

In this case you can delete all links to this without deleting the item.

2
  • I think he specifically wanted it to be done in "bulk." Going through each reference and manually removing it probably doesn't sufficiently answer the poster's question. Commented Oct 13, 2016 at 15:10
  • 1
    An alternative to removing one by one is to "Remove All", Yes, this will delete the item but then simply restore it from the Recycle Bin, sans the links. The links are broken and then it is deleted. Restoring it reverts it back to that point just before deletion.
    – jammykam
    Commented Oct 14, 2016 at 15:27
1

You can do it programmatically using this code:

Item parentItem = item.Parent;
using (SecurityHelpers.BuildSecurityDisabler())
{
    var itemReferrers = item.GetReferrers();
    if (itemReferrers != null && itemReferrers.Any())
    {
        foreach (ItemLink link in itemReferrers)
        {
            Item linkItem = dbMaster.GetItem(link.SourceItemID.Guid);
            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();
            try
            {
                customField.RemoveLink(link);
            }
            finally
            {
                linkItem.Editing.EndEdit();
            }

            //TODO: PUBLISH linkItem
        }
    }

   Sitecore.Globals.LinkDatabase.RemoveReferences(item);
   item.Delete();
}

//TODO: Publish parentItem

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.