using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.SharePoint.Client; using ProviderHostedCloud1Web.Models; namespace ProviderHostedCloud1Web.Controllers { [SharePointContextFilter] public class ProductController : Controller { // GET: Product public ActionResult Index() { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (ClientContext cc = spContext.CreateUserClientContextForSPHost()) { if (cc != null) { List productList = cc.Web.Lists.GetByTitle("Products"); cc.Load(productList); cc.ExecuteQuery(); if (productList != null) { CamlQuery query = CamlQuery.CreateAllItemsQuery(); ListItemCollection products = productList.GetItems(query); cc.Load(products); cc.ExecuteQuery(); ListretVal = new List (); foreach (var product in products) { retVal.Add(new Product { ID = product.Id, Title = product["Title"].ToString(), UnitPrice = Convert.ToDecimal(product["UnitPrice"]), UnitsOnStock = Convert.ToInt32(product["UnitsOnStock"]) }); } return View(retVal); } return View(); } else { ViewBag.ErrorMessage = "Error: ClientContext is null."; return View(); } } } // GET: Product/Details/5 public ActionResult Details(int id) { try { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (ClientContext cc = spContext.CreateUserClientContextForSPHost()) { if (cc != null) { ListItem item = cc.Web.Lists.GetByTitle("Products").GetItemById(id); cc.Load(item); cc.ExecuteQuery(); if (item != null) { Product retVal = new Product { ID = item.Id, Title = item["Title"].ToString(), UnitPrice = item["UnitPrice"] == null ? 0.00m : Convert.ToDecimal(item["UnitPrice"]), UnitsOnStock = item["UnitsOnStock"] == null ? 0 : Convert.ToInt32(item["UnitsOnStock"]) }; return View(retVal); } else { return View(); } } else { ViewBag.ErrorMessage = "Error: ClientContext was null. "; return View(); } } } catch (Exception ex) { Exception ie = ex; while (ie.InnerException != null) { ie = ie.InnerException; } ViewBag.ErrorMessage = "Error: " + ie.Message; return View(); } } // GET: Product/Create public ActionResult Create() { return View(); } // POST: Product/Create [HttpPost] public ActionResult Create(System.Web.Mvc.FormCollection collection) { string title = collection["Title"].ToString(); decimal unitPrice = collection["UnitPrice"] == null ? 0.00m : Convert.ToDecimal(collection["UnitPrice"]); int unitsOnStock = collection["UnitsOnStock"] == null ? 0 : Convert.ToInt32(collection["UnitsOnStock"]); try { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (ClientContext cc = spContext.CreateUserClientContextForSPHost()) { List productList = cc.Web.Lists.GetByTitle("Products"); ListItemCreationInformation creationInfo = new ListItemCreationInformation(); ListItem item = productList.AddItem(creationInfo); item["Title"] = title; item["UnitPrice"] = unitPrice; item["UnitsOnStock"] = unitsOnStock; item.Update(); cc.ExecuteQuery(); return RedirectToAction("Index", new { SPHostUrl = Request.QueryString["SPHostUrl"] }); } } catch (Exception ex) { Exception ie = ex; while (ie.InnerException != null) { ie = ie.InnerException; } ViewBag.ErrorMessage = "Error: " + ie.Message; Product product = new Product { Title = title, UnitPrice = unitPrice, UnitsOnStock = unitsOnStock }; return View(product); } } // GET: Product/Edit/5 public ActionResult Edit(int id) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (ClientContext cc = spContext.CreateUserClientContextForSPHost()) { if (cc != null) { ListItem item = cc.Web.Lists.GetByTitle("Products").GetItemById(id); cc.Load(item); cc.ExecuteQuery(); if (item != null) { Product product = new Product { ID = item.Id, Title = item["Title"].ToString(), UnitPrice = item["UnitPrice"] == null ? 0.00m : Convert.ToDecimal(item["UnitPrice"]), UnitsOnStock = item["UnitsOnStock"] == null ? 0 : Convert.ToInt32(item["UnitsOnStock"]) }; return View(product); } else { return View(); } } else { ViewBag.ErrorMessage = "Error: ClientContext was null"; return View(); } } } // POST: Product/Edit/5 [HttpPost] public ActionResult Edit(int id, System.Web.Mvc.FormCollection collection) { try { string title = collection["Title"].ToString(); decimal unitPrice = Convert.ToDecimal(collection["UnitPrice"]); int unitsOnStock = Convert.ToInt32(collection["UnitsOnStock"]); var spContext = SharePointAcsContextProvider.Current.GetSharePointContext(HttpContext); using (ClientContext cc = spContext.CreateUserClientContextForSPHost()) { if (cc != null) { ListItem item = cc.Web.Lists.GetByTitle("Products").GetItemById(id); item["Title"] = title; item["UnitPrice"] = unitPrice; item["UnitsOnStock"] = unitsOnStock; item.Update(); cc.ExecuteQuery(); return RedirectToAction("Index", new { SPHostUrl = Request.QueryString["SPHostUrl"] }); } else { ViewBag.ErrorMessage = "ClientContext was null."; Product product = new Product { ID = id, Title = title, UnitPrice = unitPrice, UnitsOnStock = unitsOnStock }; return View(product); } } } catch (Exception ex) { Exception ie = ex; while (ie.InnerException != null) { ie = ie.InnerException; } ViewBag.Error = "Error: " + ie.Message; return View(); } } // GET: Product/Delete/5 public ActionResult Delete(int id) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (ClientContext cc = spContext.CreateUserClientContextForSPHost()) { if (cc != null) { ListItem item = cc.Web.Lists.GetByTitle("Products").GetItemById(id); cc.Load(item); cc.ExecuteQuery(); if (item != null) { Product product = new Product { ID = item.Id, Title = item["Title"].ToString(), UnitPrice = item["UnitPrice"] == null ? 0.00m : Convert.ToDecimal(item["UnitPrice"]), UnitsOnStock = item["UnitsOnStock"] == null ? 0 : Convert.ToInt32(item["UnitsOnStock"]) }; return View(product); } else { return View(); } } else { ViewBag.Error = "Error: ClientContext was null"; return View(); } } } // POST: Product/Delete/5 [HttpPost] public ActionResult Delete(int id, System.Web.Mvc.FormCollection collection) { string title = collection["Title"].ToString(); decimal unitPrice = Convert.ToDecimal(collection["UnitPrice"]); int unitsOnStock = Convert.ToInt32(collection["UnitsOnStock"]); try { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); using (ClientContext cc = spContext.CreateUserClientContextForSPHost()) { if (cc != null) { ListItem item = cc.Web.Lists.GetByTitle("Products").GetItemById(id); item.DeleteObject(); cc.ExecuteQuery(); return RedirectToAction("Index", new { SPHostUrl = Request.QueryString["SPHostUrl"] }); } else { Product product = new Product { ID = id, Title = title, UnitPrice = unitPrice, UnitsOnStock = unitsOnStock }; ViewBag.Error = "Error: ClientContext was null"; return View(); } } } catch (Exception ex) { Exception ie = ex; while (ie.InnerException != null) { ie = ie.InnerException; } ViewBag.ErrorMessage = "Error: " + ie.Message; return View(); } } } }
Tuesday, July 26, 2016
Provider-Hosted Add-in CRUD example using CSOM
Below is a complete CRUD example of provider-hosted SharePoint Add-In using CSOM.
In the controller, the CRUD is performed on a SharePoint list, "Products", which is made of [ID], [Title], [UnitPrice] and [UnitsOnStock] fields.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment