public static bool AddProduct(SharePointContext spContext, Product product) { using (var clientContext = spContext.CreateUserClientContextForSPAppWeb()) { if (clientContext != null) { try { List lstProducts = clientContext.Web.Lists.GetByTitle("Products"); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem newProduct = lstProducts.AddItem(itemCreateInfo); newProduct["Title"] = product.Title; newProduct["ProductDescription"] = product.Description; newProduct["Price"] = product.Price; newProduct.Update(); clientContext.ExecuteQuery(); return true; } catch (ServerException ex) { return false; } } } return false; }
public ActionResult Delete(Product product) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); SharePointService.DeleteProduct(spContext, product); return RedirectToAction("Index", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }); }
public static void DeleteProduct(SharePointContext spContext, Product product) { using (var clientContext = spContext.CreateUserClientContextForSPAppWeb()) { try { List productsList = clientContext.Web.Lists.GetByTitle("Products"); ListItem itemToDelete = productsList.GetItemById(product.Id); itemToDelete.DeleteObject(); clientContext.ExecuteQuery(); } catch (ServerException ex) { // TODO: Exception Handling } } }
public ActionResult AddProduct(string title, string description, string price) { HttpStatusCodeResult httpCode = new HttpStatusCodeResult(HttpStatusCode.MethodNotAllowed); var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); Product newProduct = new Product(); newProduct.Title = title; newProduct.Description = description; newProduct.Price = price; if (SharePointService.AddProduct(spContext, newProduct)) { httpCode = new HttpStatusCodeResult(HttpStatusCode.Created); } return httpCode; }
public static bool UpdateProduct(SharePointContext spContext, Product product) { using (var clientContext = spContext.CreateUserClientContextForSPAppWeb()) { if (clientContext != null) { try { List lstProducts = clientContext.Web.Lists.GetByTitle("Products"); ListItem selectedItem = lstProducts.GetItemById(product.Id); selectedItem["Title"] = product.Title; selectedItem["ProductDescription"] = product.Description; selectedItem["Price"] = product.Price; selectedItem.Update(); clientContext.ExecuteQuery(); return true; } catch (ServerException ex) { return false; } } } return false; }