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 Edit(Product product) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); SharePointService.UpdateProduct(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; }
internal static Product GetProductDetails(SharePointContext spContext, int id) { Product product = new Product(); using (var clientContext = spContext.CreateUserClientContextForSPAppWeb()) { if (clientContext != null) { List lstProducts = clientContext.Web.Lists.GetByTitle("Products"); ListItem selectedItem = lstProducts.GetItemById(id); clientContext.Load(selectedItem); clientContext.ExecuteQuery(); if (selectedItem != null) { product = new Product { Title = selectedItem["Title"] as string, Description = selectedItem["ProductDescription"] as string, Price = selectedItem["Price"] as string, }; } } } return product; }
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; }