Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Roy Berris 89 posts 577 karma points c-trib
    Sep 30, 2021 @ 07:25
    Roy Berris
    0

    Query through (unpublished) content on property value

    I have a set of events that I created from a external data set. When syncing these events I need to see if this content is already exists based on the value of a property.

    I have a document type with a properly EventId. Now I want to query through all content in a specific collection. And see if there already is content where the EventId matches my external event id.

    I saw some posts using the

    _contentService.GetRootContent().FirstOrDefault().Descendants()
    

    But the Descendents method doesn't exist in Umbraco 8?

  • Roy Berris 89 posts 577 karma points c-trib
    Oct 07, 2021 @ 06:55
    Roy Berris
    100

    For anyone in the future, my working code:

        private bool ContentExists(string searchString)
        {
            const int pageSize = 100;
    
            var propertyAlias = MyDetailPage.GetModelPropertyType(i => i.Propety).Alias;
            var collectionNode = _contentService.GetById(GetCollectionGuid()); // get Guid of the parent node, create method yourself
            var pages = Convert.ToInt32(Math.Ceiling(_contentService.CountChildren(collectionNode.Id, MyDetailPage.ModelTypeAlias) / (float)pageSize)); // count the number of children for the parent node
    
            if (pages <= 0)
            {
                return false; // if 0 zero it means there are no children
            }
    
            // umbraco only allows us to navigate through children paginated
            for (var pageIndex = 0; pageIndex < pages; pageIndex++)
            {
                var children = _contentService.GetPagedChildren(eventCollectionNode.Id, pageIndex, pageSize, out _);
    
                if (children.Any(i => i.GetValue<object>(propertyAlias).Equals(searchString, StringComparison.OrdinalIgnoreCase)))
                {
                    return true;
                }
            }
    
            return false;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft