2

Is there a way to check if an item is linked to other items using C#?

Also, how to remove these links? Somehow item.Versions.RemoveAll(false) is not working if the item is linked to other items.

3 Answers 3

0

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!

0

Here is the function to get all the items that are linked and delete them.

public string RemoveReferenceLinks(string nodePath)
{
    var db = Sitecore.Configuration.Factory.GetDatabase("master");
    var item = db.GetItem(nodePath);

    if (item == null) return new ItemLink[0];

    var links =  Globals.LinkDatabase.GetItemReferrers(item, true);

    if (links.Length == 0)
    {
      return "No referrence found";
    }

    foreach (var link in links)
    {
      var sourceItem = link.GetSourceItem();

      foreach (var item1 in sourceItem.Versions.GetVersions(true))
      {

        var field = item1.Fields[link.SourceFieldID];
        var field2 = FieldTypeManager.GetField(field);

        if (field2 == null) return;

        using (new SecurityDisabler())
        {
          item1.Editing.BeginEdit();
          field2.RemoveLink(link);
          item1.Editing.EndEdit();
        }
      }
    }

    return "All reference removed";
}
0

We can get reference items from Sitecore Link Database using this extension method

 public static Item[] GetReferrersAsItems(this Item item, bool includeStandardValues = false)
    {
        var links = Globals.LinkDatabase.GetReferrers(item);
        if (links == null)
            return new Item[0];
        var linkedItems = links.Select(i => i.GetSourceItem()).Where(i => i != null);
        if (!includeStandardValues)
            linkedItems = linkedItems.Where(i => !i.Name.Equals("__standard values", StringComparison.InvariantCultureIgnoreCase));
        return linkedItems.ToArray();
    }

To retrieve the referrers simply call the Globals.LinkDatabase.GetReferrers with the item passed as a parameter.

if we only want the items from within the current context site. This can be done simply by determining if an item resides beneath the current sitecontext start path.

public static Item[] GetReferencesInContextSite(this Item item)
    {
        var siteContext = Context.Site;
        if (siteContext == null)
            return new Item[0];
        var referrers = item.GetReferencesAsItems());
        return referrers.Where(i => i.Paths.FullPath.StartsWith(siteContext.StartPath, StringComparison.InvariantCultureIgnoreCase)).ToArray();
    }

[Credit Source][1]: https://laubplusco.net/sitecore-item-extensions-get-referrers-as-items/

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.