public void DeleteItemByQuery(string ListName, ICamlQuery Query) { try { SP.Web web = context.Web; SP.List list = web.Lists.GetByTitle(ListName); SP.CamlQuery query = (SP.CamlQuery)Query.ExecuteQuery(); SP.ListItemCollection items = list.GetItems(query); context.Load(items); context.ExecuteQuery(); if (items != null) { foreach (SP.ListItem item in items) { item.DeleteObject(); context.Load(item); } context.ExecuteQuery(); } else { throw new Exception("Item does not exist"); } } catch (Exception ex) { throw new Exception("Error updating SharePoint list data: " + ex.Message); } }
public void UpdateItemByQuery(string ListName, TEntity Item, ICamlQuery Query) { try { SP.Web web = context.Web; SP.List list = web.Lists.GetByTitle(ListName); SP.CamlQuery query = (SP.CamlQuery)Query.ExecuteQuery(); SP.ListItemCollection items = list.GetItems(query); context.Load(items); context.ExecuteQuery(); if (items != null) { foreach (SP.ListItem item in items) { var properties = typeof(TEntity).GetProperties(); foreach (var property in properties) { if (property.Name != "ID" && property.Name != "Created" && property.Name != "Author") { item[property.Name] = property.GetValue(Item); } } item.Update(); context.Load(item); } context.ExecuteQuery(); } else { throw new Exception("Item does not exist"); } } catch (InvalidCastException ex) { throw new Exception(ex.Message + ". Data model types are invalid, please verify that models are strongly typed and match the SharePoint list column types.", new InvalidCastException()); } catch (Exception ex) { throw new Exception("Error updating SharePoint list data: " + ex.Message); } }
public IEnumerable <TEntity> GetItemsByQuery(string ListName, ICamlQuery Query) { try { List <TEntity> entityItems = new List <TEntity>(); SP.Web web = context.Web; SP.List list = web.Lists.GetByTitle(ListName); SP.CamlQuery query = (SP.CamlQuery)Query.ExecuteQuery(); SP.ListItemCollection items = list.GetItems(query); context.Load(items); context.ExecuteQuery(); foreach (SP.ListItem item in items) { var properties = typeof(TEntity).GetProperties(); TEntity entry = new TEntity(); foreach (var property in properties) { if (item[property.Name] != null) { var i = item[property.Name]; property.SetValue(entry, item[property.Name]); } } entityItems.Add(entry); } return(entityItems); } catch (InvalidCastException ex) { throw new Exception(ex.Message + ". Data model types are invalid, please verify that models are strongly typed and match the SharePoint list column types.", new InvalidCastException()); } catch (Exception ex) { throw new Exception("Error updating SharePoint list data: " + ex.Message); } }