No output in dropdown c#

I hope someone can help me get my dropdown to work. My dropdown loads and it has items, but it has no output.

My Code:

using System.Collections.Generic;

using DSCoreNodesUI;

using Dynamo.Nodes;

using Dynamo.Utilities;

using ProtoCore.AST.AssociativeAST;

using Dynamo.Models;

using System;

 

namespace BIMSharkDynamoTools.DropDowm

{

[NodeName(“Drop Down Example”)]

[NodeDescription(“An example drop down node.”)]

[IsDesignScriptCompatible]

publicclassDropDownExample : DSDropDownBase

{

public DropDownExample() : base(“item”) { }

private RestService.Rest rest;

 

publicoverridevoid PopulateItems()

{

// The Items collection contains the elements

// that appear in the list. For this example, we

// clear the list before adding new items, but you

// can also use the PopulateItems method to add items

// to the list.

string apiKey = ””; //Insert API Key here

rest = new RestService.Rest(apiKey);

 

Items.Clear();

 

// Create a number of DynamoDropDownItem objects

// to store the items that we want to appear in our list.

 

//var newItems = new List<DynamoDropDownItem>()

//{

// new DynamoDropDownItem(“Tywin”, 0),

// new DynamoDropDownItem(“Cersei”, 1),

// new DynamoDropDownItem(“Hodor”,2)

//};

 

//set up the collection

//var newItems = new List<DynamoDropDownItem>();

 

 

 

rest.GetDocuments(“2”);

 

var newItems = newList<DynamoDropDownItem>();

 

foreach (RestService.Document d in rest.documents)

{

//CB_Projects.Items.Add("(" + p.id + ") " + p.project_name);

//new DynamoDropDownItem("(" + d.id + ") " + d.title, d.id)),

Items.Add(new DynamoDropDownItem("(" + d.id + ") " + d.title, d.id));

}

 

 

//set up the collection

Items.AddRange(newItems);

 

 

 

// Set the selected index to something other

// than -1, the default, so that your list

// has a pre-selection.

SelectedIndex = 0;

 

 

}

 

publicoverrideIEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)

{

// Build an AST node for the type of object contained in your Items collection.

 

var intNode = AstFactory.BuildIntNode((int)Items[SelectedIndex].Item); //<- Here I get an error

var assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), intNode);

 

returnnewList<AssociativeNode> { assign };

}

}

}

 

Dynamo_dropdown_error

 

Any luck mate?

 

Let me know if you figure it out.

 

Jeremy.

My current code looks like this:

Now I do get an output but there must but there must be an easier way to the inputs.

<span class="pl-k">using</span> System.Collections.Generic<span class="pl-k">;</span>
<span class="pl-k">using</span> System.Linq<span class="pl-k">;</span>
<span class="pl-k">using</span> DSCoreNodesUI<span class="pl-k">;</span>
<span class="pl-k">using</span> Dynamo.Nodes<span class="pl-k">;</span>
<span class="pl-k">using</span> Dynamo.Utilities<span class="pl-k">;</span>
<span class="pl-k">using</span> ProtoCore.AST.AssociativeAST<span class="pl-k">;</span>
<span class="pl-k">using</span> Dynamo.Models<span class="pl-k">;</span>
<span class="pl-k">using</span> System<span class="pl-k">;</span>
<span class="pl-k">using</span> Dynamo.Engine<span class="pl-k">;</span>

<span class="pl-k">namespace</span> <span class="pl-en">BIMSharkDynamoTools.GETCalls</span>
{
    [NodeName(<span class="pl-s"><span class="pl-pds">"</span>GET All Your BS Projects<span class="pl-pds">"</span></span>)]
    [NodeDescription(<span class="pl-s"><span class="pl-pds">"</span>GET a list of all your BS projects<span class="pl-pds">"</span></span>)]
    [IsDesignScriptCompatible]
    <span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">BIMSharkDynamoToolsGETProjects</span> : <span class="pl-k">DSDropDownBase</span>
    {
        <span class="pl-k">private</span> RestService.Rest rest;
        <span class="pl-k">private</span> <span class="pl-k">string</span> apiKey = <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span>;

        <span class="pl-c">//public static string apiKey;</span>
        <span class="pl-k">internal</span> EngineController <span class="pl-en">EngineController</span> { <span class="pl-k">get</span>; <span class="pl-k">set</span>; }

        <span class="pl-c">//test</span>
        <span class="pl-c">//public BIMSharkDynamoToolsGETProjects()</span>
        <span class="pl-c">//    : base("item")</span>
        <span class="pl-c">//{</span>
        <span class="pl-c">//    this.AddPort(PortType.Input, new PortData("BS ApiKey", "ToolTip"), 0);</span>
        <span class="pl-c">//    this.PropertyChanged += OnPropertyChanged;</span>
        <span class="pl-c">//}</span>

        <span class="pl-k">public</span> <span class="pl-en">BIMSharkDynamoToolsGETProjects</span>()
            : <span class="pl-c1">base</span>("Project(s)")
        {
            InPortData.Add(<span class="pl-k">new</span> PortData(<span class="pl-s"><span class="pl-pds">"</span>BS ApiKey<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>ToolTip<span class="pl-pds">"</span></span>));

            RegisterAllPorts();

            <span class="pl-c1">this</span>.PropertyChanged += OnPropertyChanged;
        }

        <span class="pl-k">void</span> <span class="pl-en">OnPropertyChanged</span>(<span class="pl-k">object</span> <span class="pl-smi">sender</span>, <span class="pl-k">System.ComponentModel.PropertyChangedEventArgs</span> <span class="pl-smi">e</span>)
        {
            <span class="pl-k">if</span> (e.PropertyName != <span class="pl-s"><span class="pl-pds">"</span>CachedValue<span class="pl-pds">"</span></span>)
                <span class="pl-k">return</span>;

            <span class="pl-k">if</span> (InPorts.Any(x =&gt; x.Connectors.Count == <span class="pl-c1">0</span>))
                <span class="pl-k">return</span>;

            PopulateItems();

        }

        <span class="pl-k">public</span> <span class="pl-k">override</span> <span class="pl-k">void</span> <span class="pl-en">PopulateItems</span>()
        {

            <span class="pl-k">try</span>
           {
                List&lt;<span class="pl-k">string</span>&gt; BSAPIKeys = <span class="pl-k">new</span> List&lt;<span class="pl-k">string</span>&gt;();
                <span class="pl-k">string</span> AllBSAPIKeys = <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span>;
                BSAPIKeys.Clear();

                <span class="pl-k">var</span> urls = InputNodes.Values.ToArray();
                <span class="pl-k">foreach</span> (<span class="pl-k">var</span> url <span class="pl-k">in</span> urls)
                {
                    <span class="pl-k">if</span> (InPorts.Any(x =&gt; x.PortName == <span class="pl-s"><span class="pl-pds">"</span>BS ApiKey<span class="pl-pds">"</span></span>))
                    {
                        BSAPIKeys.Add(((Dynamo.Nodes.CodeBlockNodeModel)url.Item2).Code);
                    }
                }
                AllBSAPIKeys = <span class="pl-k">string</span>.Join(<span class="pl-s"><span class="pl-pds">"</span>, <span class="pl-pds">"</span></span>, BSAPIKeys.ToArray());
                apiKey = AllBSAPIKeys.Replace(<span class="pl-s"><span class="pl-pds">"</span>;<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span>).Replace(<span class="pl-s"><span class="pl-pds">"</span>/<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span>).Replace(<span class="pl-s"><span class="pl-pds">"</span><span class="pl-cce">\"</span><span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span>);
                Items.Clear();
            }
            <span class="pl-k">catch</span>
            {
            }


            <span class="pl-k">if</span> (apiKey.Length &gt; <span class="pl-c1">20</span>)
            {
                <span class="pl-k">try</span>
                {
                    <span class="pl-c">// The Items collection contains the elements</span>
                    <span class="pl-c">// that appear in the list. For this example, we</span>
                    <span class="pl-c">// clear the list before adding new items, but you</span>
                    <span class="pl-c">// can also use the PopulateItems method to add items</span>
                    <span class="pl-c">// to the list.</span>

                    rest = <span class="pl-k">new</span> RestService.Rest(apiKey);
                    <span class="pl-c">//Items.Clear();</span>

                    <span class="pl-c">// Create a number of DynamoDropDownItem objects </span>
                    <span class="pl-c">// to store the items that we want to appear in our list.</span>
                    rest.GetProjects();

                    <span class="pl-k">var</span> newItems = <span class="pl-k">new</span> List&lt;DynamoDropDownItem&gt;();
                    <span class="pl-k">foreach</span> (RestService.Project p <span class="pl-k">in</span> rest.projects)
                    {
                        <span class="pl-k">string</span> text = <span class="pl-s"><span class="pl-pds">"</span>(<span class="pl-pds">"</span></span> + p.id.ToString() + <span class="pl-s"><span class="pl-pds">"</span>)  <span class="pl-pds">"</span></span> + p.project_name.ToString();

                        <span class="pl-k">string</span> <span class="pl-k">value</span> = p.id.ToString();
                        <span class="pl-c">//int value = int.Parse(p.id);</span>

                        <span class="pl-c">//CB_Projects.Items.Add("(" + p.id + ")  " + p.project_name);</span>
                        <span class="pl-c">//new DynamoDropDownItem("(" + d.id + ")  " + d.title, d.id)),</span>
                        Items.Add(<span class="pl-k">new</span> DynamoDropDownItem(text, <span class="pl-k">value</span>));
                    }

                    <span class="pl-c">//set up the collection</span>
                    <span class="pl-c">//var newItems = new List&lt;DynamoDropDownItem&gt;();</span>

                    <span class="pl-c">//set up the collection</span>
                    <span class="pl-c">//Items.AddRange(newItems);</span>


                    <span class="pl-c">// Set the selected index to something other</span>
                    <span class="pl-c">// than -1, the default, so that your list</span>
                    <span class="pl-c">// has a pre-selection.</span>
                    SelectedIndex = <span class="pl-c1">0</span>;
                    <span class="pl-k">return</span>;
                }
                <span class="pl-k">catch</span>
                {
                }


            }
            <span class="pl-k">else</span>
            {
                <span class="pl-k">try</span>
                {
                    <span class="pl-c">// The Items collection contains the elements</span>
                    <span class="pl-c">// that appear in the list. For this example, we</span>
                    <span class="pl-c">// clear the list before adding new items, but you</span>
                    <span class="pl-c">// can also use the PopulateItems method to add items</span>
                    <span class="pl-c">// to the list.</span>

                    Items.Clear();

                    <span class="pl-c">// Create a number of DynamoDropDownItem objects </span>
                    <span class="pl-c">// to store the items that we want to appear in our list.</span>

                    <span class="pl-k">var</span> newItems = <span class="pl-k">new</span> List&lt;DynamoDropDownItem&gt;()
                    {
                        <span class="pl-k">new</span> DynamoDropDownItem(<span class="pl-s"><span class="pl-pds">"</span>No Project found<span class="pl-pds">"</span></span>, <span class="pl-c1">0</span>),
                    };

                    <span class="pl-c">//set up the collection</span>
                    <span class="pl-c">//var newItems = new List&lt;DynamoDropDownItem&gt;();</span>

                    <span class="pl-c">//set up the collection</span>
                    <span class="pl-c">//Items.AddRange(newItems);</span>

                    <span class="pl-c">// Set the selected index to something other</span>
                    <span class="pl-c">// than -1, the default, so that your list</span>
                    <span class="pl-c">// has a pre-selection.</span>
                    SelectedIndex = <span class="pl-c1">0</span>;
                }
                <span class="pl-k">catch</span>
                {
                }
            }
        }

        <span class="pl-k">public</span> <span class="pl-k">override</span> IEnumerable&lt;AssociativeNode&gt; <span class="pl-en">BuildOutputAst</span>(List&lt;AssociativeNode&gt; inputAstNodes)
        {
            <span class="pl-c">//var intNode = AstFactory.BuildIntNode((int)Items[SelectedIndex].Item);</span>
            <span class="pl-c">//var assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), intNode);</span>
            <span class="pl-c">//return new List&lt;AssociativeNode&gt; { assign };</span>

            <span class="pl-c">// If there the dropdown is still empty try to populate it again</span>
            <span class="pl-k">if</span> (Items.Count == <span class="pl-c1">0</span> || Items.Count == -<span class="pl-c1">1</span>)
            {
                PopulateItems();
            }

            <span class="pl-c">// get the selected items name</span>
            <span class="pl-k">var</span> stringNode = AstFactory.BuildStringNode((<span class="pl-k">string</span>)Items[SelectedIndex].Item);

            <span class="pl-c">// assign the selected name to an actual enumeration value</span>
            <span class="pl-k">var</span> assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(<span class="pl-c1">0</span>), stringNode);

            <span class="pl-c">// return the enumeration value</span>
            <span class="pl-k">return</span> <span class="pl-k">new</span> List&lt;AssociativeNode&gt; { assign };
        }
    }
}

Oh my God, this is my dream answer!!
But…I still meet some error …
About " private RestService.Rest rest; "
The type or namespace name ‘type/namespace’ could not be found …
But I already using all the reference just like yours,please help me !
Thanks a lot !

I have all my calls to the BIM Shark application in its own dll.

I’d love to hear from you if you find an easier way to grab the values form the inputs

This weekend I had one more look at my code but I had no luck in getting it working.
The only “sample” I can find is “Get Family Parameter” in DynamoRevit/src/Libraries/RevitNodesUI/RevitDropDown.cs.

Code
private Element GetInputElement()
{
var inputNode = InPorts[0].Connectors[0].Start.Owner;
var index = InPorts[0].Connectors[0].Start.Index;

        var identifier = inputNode.GetAstIdentifierForOutputIndex(index).Name;

        if (EngineController == null) return null;
        var data = this.EngineController.GetMirror(identifier).GetData();


        object family = data.IsCollection ? 
            data.GetElements().FirstOrDefault() : 
            data.Data;

        var elem = family as Revit.Elements.Element;

        return null == elem ? null : elem.InternalElement;
      }

when I try to do the same I do not get any usefull data i return.

Nicklas,

It’s the EngineController implementation that returns null. You have to define it first. I think in this case Dynamo team implemented a Customization class that looks something like this:

Also, I had a similar issue and discussed it here: https://github.com/DynamoDS/Dynamo/issues/5469

using DSRevitNodesUI;
namespace Dynamo.Wpf.Nodes.Revit
{
/// <summary>
///
/// </summary>
public class GetBuiltInParameterNameNodeViewCustomization : DropDownNodeViewCustomization, INodeViewCustomization<archilabUI.ItemSelector>
{
/// <summary>
///
/// </summary>
/// <param name=“model”></param>
/// <param name=“nodeView”></param>
public void CustomizeView(archilabUI.ItemSelector model, Dynamo.Controls.NodeView nodeView)
{
base.CustomizeView(model, nodeView);

// this is not a recommended workaround
// says someone at Dynamo team - I believe them!
model.EngineController = nodeView.ViewModel.DynamoViewModel.EngineController;
}
}
}

Sorry for the late reply. Your answer was just the solution I needed.