protected void ProductEditorDialog_AddClosed(object sender, EventArgs args) { ProductEditor dialog = (ProductEditor)sender; // stop if they clicked cancel if (dialog.DialogResult == false) { return; } StatusBarPanel.BeginUpdateMessage("Creating new item..."); List products = Global.GetClientContext().Web.Lists.GetByTitle("Products"); ListItemCreationInformation newProductInfo = new ListItemCreationInformation(); ListItem newProduct = products.AddItem(newProductInfo); newProduct["Title"] = dialog.ProductNameTextBox.Text; newProduct["Product_x0020_Number"] = dialog.ProductNumberTextBox.Text; newProduct["Price"] = dialog.ProductPriceTextBox.Text; FieldLookupValue fieldValue = new FieldLookupValue(); foreach (ListItem item in Global.ProductCategories) { if (item["Title"].ToString() == dialog.ProductCategortyComboBox.SelectedItem.ToString()) { fieldValue.LookupId = item.Id; } } newProduct["Category"] = fieldValue; newProduct.Update(); Global.GetClientContext().ExecuteQueryAsync(OnSucceededListenerCreateProduct, OnFailListener); }
protected void ProductEditorDialog_UpdateClosed(object sender, EventArgs args) { ProductEditor dialog = (ProductEditor)sender; // stop if they clicked cancel if (dialog.DialogResult == false) { return; } StatusBarPanel.BeginUpdateMessage("Updating product details..."); // get the item being updated _product = Global.GetClientContext().Web.Lists.GetByTitle("Products").GetItemById(_productID); _product["Title"] = dialog.ProductNameTextBox.Text; _product["Product_x0020_Number"] = dialog.ProductNumberTextBox.Text; _product["Price"] = dialog.ProductPriceTextBox.Text; FieldLookupValue fieldValue = new FieldLookupValue(); foreach (ListItem item in Global.ProductCategories) { if (item["Title"].ToString() == dialog.ProductCategortyComboBox.SelectedItem.ToString()) { fieldValue.LookupId = item.Id; } } _product["Category"] = fieldValue; _product.Update(); Global.GetClientContext().ExecuteQueryAsync(OnSucceededListenerUpdateProducts, OnFailListener); }
// handlers for when the add button is clicked private void AddProductButton_Click(object sender, System.Windows.RoutedEventArgs e) { ProductEditor dialog = new ProductEditor(); dialog.SetForAdd(); dialog.Closed += new EventHandler(ProductEditorDialog_AddClosed); dialog.Show(); }
// handers for when the update button is clicked private void UpdateProductButton_Click(object sender, System.Windows.RoutedEventArgs e) { ProductEditor dialog = new ProductEditor(); dialog.SetForUpdate(); dialog.Closed += new EventHandler(ProductEditorDialog_UpdateClosed); // init fields dialog.InitFields(ProductNameTextBox.Text, ProductNumberTextBox.Text, ProductPriceTextBox.Text, ProductCategortyTextBox.Text); dialog.Show(); }