finding unreferenced items
# help-with-umbraco
p
Hello everyone, I never got any good response in umbraco forum, could some one help on this: https://our.umbraco.com/forum/using-umbraco-and-getting-started//114458-find-un-referenced-items
m
I have some code at home i use for finiding what documwnts referance what that might help you. I will post it up im abiut 2 hours when i get home
So looking over my code I think it is actually basicly wahat your looking for. I go though all the published and unpublished pages meeting a certain crieria and add the node IDs to a list, then I work though the list and get a total count of the amount of times the page is referenced (not just pages but total times, so if a page references it 3 times that is +3 to the count). It will list 0 for pages taht are not referenced so this should work for you, you just need to update the node logic to meet your needs:
Copy code
public class LinkCounterHelper
    {
        private readonly IContentService _contentService;
        private readonly IRelationService _relationService;

        public LinkCounterHelper(IContentService contentService, IRelationService relationService)
        {
            _contentService = contentService;
            _relationService = relationService;
        }

        public List<KeyValuePair<string, int>> GetSectionPageData()
        {
            var pageStats = new List<KeyValuePair<string, int>>();
            var rootNode = _contentService.GetRootContent().FirstOrDefault();
            var sectionNodes = _contentService.GetPagedChildren(rootNode.Id, 0, int.MaxValue, out _)
                                              .Where(c => c.ContentType.Alias == "sectionHomePage" && !_contentService.GetPagedChildren(c.Id, 0, int.MaxValue, out _)
                                              .Any(child => child.ContentType.Alias == "longStorySummaryPage"));

            foreach (var node in sectionNodes)
            {
                var relationshipCount = _relationService.GetByChildId(node.Id).Count();
                pageStats.Add(new KeyValuePair<string, int>(node.Name, relationshipCount));
            }

            return pageStats.OrderByDescending(x => x.Value).ToList();
        }
    }
p
thanks, I'll try it.
6 Views