Copied to clipboard

Flag this post as spam?

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


  • pbl_dk 132 posts 501 karma points
    Sep 10, 2017 @ 09:49
    pbl_dk
    0

    UmbracoHelper - find all pages with attribute

    Hi There.

    I am trying to find all nodes which contains the property "documentChooser", with the umbracohelper. But I cannot find any equivalent to "currentpage.site". How is that done.?

    'UmbracoHelper' does not contain a definition for 'Site' and no extension method 'Site'

      public ContentResult PostMeNow()
      {
       var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    var _site = umbracoHelper.Site()...   <-- Error message
    'UmbracoHelper' does not contain a definition for 'Site' and no extension method 'Site'
    
    foreach(var child in _site {
        if (child.documentChooser.Url)
        {
        ///
        }
      }
    return ....
    }
    
  • Ayo Adesina 419 posts 1001 karma points
    Sep 10, 2017 @ 10:52
    Ayo Adesina
    1

    Do you have one site in you Umbraco installation or do you have more than one?

    If you only have one I'm guessing......you what you want to do is get a list of all nodes that have a particular property?

    This will give you the root node.

    var umbracoContext = Umbraco.Web.UmbracoContext.Current;
    var umbracoHelper = new Umbraco.Web.UmbracoHelper(umbracoContext);
    rootnode = umbracoHelper.TypedContentAtRoot();
    

    Then you can look for all nodes that are of a given document type (the document type that has the property your looking for)

    like this:

    var nodesyouwant = rootnode.Where(x => x.DocumentTypeAlias == "yourDocTypeAliasHere");
    

    hope that helps. Ayo

  • Bo Jacobsen 438 posts 1818 karma points
    Sep 10, 2017 @ 11:33
    Bo Jacobsen
    100

    Hi.

    I think i know what you trying to do.

    UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
    List<IPublishedContent> listOfNodeWithProperty = new List<IPublishedContent>();
    
    IEnumerable<IPublishedContent> rootNodes = umbracoHelper.TypedContentAtRoot();
    foreach(IPublishedContent rootNode in rootNodes)
    {
        listOfNodeWithProperty.AddRange(rootNode.Descendants().Where(x => x.HasProperty("propertyAlias")));
    }
    
    return listOfNodeWithProperty;
    
  • pbl_dk 132 posts 501 karma points
    Sep 10, 2017 @ 12:20
    pbl_dk
    0

    Thanks guys! I got what I needed, a list of media-urls from the media picker on the node. I trying to test the url up against the IHttpHandler Context, so I can prevent someone to download a PDF which is extranet protected, under a specific extranet page. Not all pdfs on the solution, should be protected, so I needed some way to find the urls for the protected PDFs only. This was the last code I needed :-)

    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
       string _childrenName = "";
       string _childrenID = "";
    
       List<IPublishedContent> listOfNodeWithProperty = new List<IPublishedContent>();
    
       IEnumerable<IPublishedContent> rootNodes = umbracoHelper.TypedContentAtRoot();
       foreach (IPublishedContent rootNode in rootNodes)
       {
        listOfNodeWithProperty.AddRange(rootNode.Descendants().Where(x => x.HasProperty("documentChooser")));
       }
    
       foreach (var child in listOfNodeWithProperty)
       {
        _childrenID = child.GetPropertyValue("documentChooser").ToString();
        var media = umbracoHelper.TypedMedia(_childrenID);
        _childrenName += "/  " + media.Url();
       }
       return Content(_childrenName);
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies