Developer forum

Forum » Development » Ribbon.DataContext.DataSource is not initialized

Ribbon.DataContext.DataSource is not initialized

Lars Larsen
Lars Larsen
Reply

Hi

I'm trying to make a custom ribbon button for orderlists. I am using the example (OrderListAddIn1.cs) from the Dynamicweb Visual Studio Templates. But the example doesn't work because Ribbon.DataContext.DataSource is not initialized. The values in DataSource is:

ThreadSafetyMode=ExecutionAndPublication, IsValueCreated=false, IsValueFaulted=false, Value=null

I am running v9.9.1. Is this a bug?


Replies

 
Lars Larsen
Lars Larsen
Reply

Bump!

 
Nicolai Pedersen
Reply

I have asked a developer to look at the code and see what can be wrong. You or our code :-)

 
Lars Larsen
Lars Larsen
Reply

Thanks :-D

 
Viktor Letavin Dynamicweb Employee
Viktor Letavin
Reply

Hi Lars,

I've tried code sample for button ribbon button add-in and I had no problems with that, Lazy onli initializes value at the time first call occured in the runtime, that might be your problem?

you can try my example, may be it will clarify something:

[AddInTarget(RibbonBarAddInTarget.eCom.OrderList)]
    public class MyAddin : RibbonBarAddIn
    {

        /// 
        /// Initializes a new instance of the MyAddin class.
        /// 
        public MyAddin(RibbonBar ribbon)
            : base(ribbon)
        {

        }

        public override void Load()
        {
            // To learn more about RibbonBarAddIns, check out: http://developer.dynamicweb-cms.com/documentation/for-developers/cms-extensibility/ribbon-bar.aspx

            RibbonBarGroup group = base.CreateDefaultContainer();
            RibbonBarButton button = new RibbonBarButton();
            group.Name = "My group";

            group.AddItem(button);

            // Access the DataContext which is a Order collection for this AddIn.
            var orders = base.Ribbon.DataContext.DataSource as Lazy>;
            double ordersTotal = 0.0;
            if (orders != null && orders.Value != null)
            {
                foreach (var order in orders.Value) {
                    ordersTotal += order.TotalPrice;
                }
                /// process orders.Value
            }

            button.Text = $"Orders total {Services.Currencies.Format(Context.Currency, ordersTotal)}";
            button.DoTranslate = false;
            button.Title = "some hover text";
            button.Icon = KnownIcon.Money;
            // Alternatively, specify your own Image path:
            // button.ImagePath = "/Admin/SomeImage.png";

            button.Size = Icon.Size.Large;
            button.EnableServerClick = true;
            button.Click += button_Click;

        }

        void button_Click(object sender, EventArgs e)
        {
            // Handle click here           

        }

    }
if you will still have some problems or questions, let me know I will glad to help.

 

BR, Viktor.
 
Lars Larsen
Lars Larsen
Reply

Hi Viktor

Your code wont build (see below). I think you miss thomething in the line I have marked with the red arrow. 

Capture.PNG
 
Viktor Letavin Dynamicweb Employee
Viktor Letavin
Reply

Hi,

My bad, It should be 


var orders = base.Ribbon.DataContext.DataSource as Lazy<IEnumerable<Order>>;


Sorry for the inconvenience.

 

BR, Viktor.

 
Lars Larsen
Lars Larsen
Reply

Hi Viktor

Thanks - yes now it builds. If I mark orders in the orderlist, how do I get a list of these orders in the code?

Capture.PNG
 
Viktor Letavin Dynamicweb Employee
Viktor Letavin
Reply
This post has been marked as an answer

Hi,

I don't think this is what ribbon buttons made, but you can try  replace

            button.EnableServerClick = true;
            button.Click += button_Click;

with


            button.OnClientClick = @"   var selectedOrdersHidden = document.createElement('input');
                                        selectedOrdersHidden.type = 'hidden';
                                        selectedOrdersHidden.value = List.getSelectedRows('List').map(function(row) {return row.getAttribute('itemid')}).join();;
                                        selectedOrdersHidden.name = 'SelectedOrderIds';
                                        document.forms[0].appendChild(selectedOrdersHidden);
                                        document.forms[0].submit();";

            if (Dynamicweb.Context.Current.Request["SelectedOrderIds"] != null)
            {
                var selectedOrderIds = Dynamicweb.Context.Current.Request["SelectedOrderIds"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                var selectedOrders = new List<Order>();
                foreach (var selectedOrderId in selectedOrderIds)
                {
                    var order = Services.Orders.GetById(selectedOrderId);
                    if (order != null)
                    {
                        selectedOrders.Add(order);
                    }
                }
            }

BR, Viktor.

Votes for this answer: 1
 
Lars Larsen
Lars Larsen
Reply

Hi Viktor

Thanks, it works like a charm smiley

 

You must be logged in to post in the forum