Ejemplo n.º 1
0
        public virtual T Save()
        {
            ListItem newItem;

            try
            {
                SPContext = new ClientContext(SharePointHelper.Url);

                var web  = SharePointHelper.GetWeb(SPContext);
                var list = SharePointHelper.GetList(SPContext, web, ListName);

                if (Id == 0)
                {
                    // Create
                    var itemCreateInfo = new ListItemCreationInformation();
                    newItem = list.AddItem(itemCreateInfo);
                }
                else
                {
                    // Update
                    newItem = list.GetItemById(Id);
                }

                MapToList(newItem);
                newItem.Update();
                SPContext.Load(newItem);
                SPContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to save " + ListName + " with id " + Id + ". " + ex.Message);
            }

            return(Get(newItem.Id));
        }
Ejemplo n.º 2
0
        public static List <T> GetAllBy(string key, string value, bool notEqual = false)
        {
            var ctx     = new ClientContext(SharePointHelper.Url);
            var type    = new T();
            var results = new List <T>();

            try
            {
                var web  = SharePointHelper.GetWeb(ctx);
                var list = SharePointHelper.GetList(ctx, web, type.ListName);

                var caml  = SharePointHelper.GetByCaml(key, value, notEqual);
                var items = list.GetItems(caml);
                ctx.Load(items);
                ctx.ExecuteQuery();

                foreach (ListItem item in items)
                {
                    var t = new T();

                    t.MapFromList(item);
                    results.Add(t);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to get " + type.ListName + ". " + ex.Message);
            }

            return(results);
        }
Ejemplo n.º 3
0
        public static T GetBy(string key, int value)
        {
            var ctx = new ClientContext(SharePointHelper.Url);
            var t   = new T();

            try
            {
                var web  = SharePointHelper.GetWeb(ctx);
                var list = SharePointHelper.GetList(ctx, web, t.ListName);

                var caml  = SharePointHelper.GetByCaml(key, value);
                var items = list.GetItems(caml);
                ctx.Load(items);
                ctx.ExecuteQuery();

                var item = items.FirstOrDefault();

                t.MapFromList(item);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to get " + t.ListName + ". " + ex.Message);
            }

            return(t);
        }
Ejemplo n.º 4
0
        public static List <T> GetAllJoined(string joinList, string joinField, string[] projectedFields)
        {
            var ctx     = new ClientContext(SharePointHelper.Url);
            var type    = new T();
            var results = new List <T>();

            try
            {
                var web  = SharePointHelper.GetWeb(ctx);
                var list = SharePointHelper.GetList(ctx, web, type.ListName);

                var caml  = SharePointHelper.GetAllJoinCaml(joinList, joinField, projectedFields);
                var items = list.GetItems(caml);
                ctx.Load(items);
                ctx.ExecuteQuery();

                foreach (ListItem item in items)
                {
                    var t = new T();

                    t.MapFromList(item);

                    results.Add(t);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to get " + type.ListName + ". " + ex.Message, ex);
            }

            return(results);
        }
Ejemplo n.º 5
0
        public void Delete()
        {
            var ctx = new ClientContext(SharePointHelper.Url);

            T t = new T();

            try
            {
                var web  = SharePointHelper.GetWeb(ctx);
                var list = SharePointHelper.GetList(ctx, web, t.ListName);

                var item = list.GetItemById(Id);
                item.DeleteObject();
                ctx.ExecuteQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to delete " + t.ListName + " with id " + Id + ". " + ex.Message);
            }
        }
Ejemplo n.º 6
0
        public static List <T> GetAllByView(string viewName)
        {
            var ctx     = new ClientContext(SharePointHelper.Url);
            var type    = new T();
            var results = new List <T>();

            try
            {
                var web  = SharePointHelper.GetWeb(ctx);
                var list = SharePointHelper.GetList(ctx, web, type.ListName);

                var view = list.Views.GetByTitle(viewName);
                ctx.Load(view);
                ctx.ExecuteQuery();

                var caml = new CamlQuery();
                caml.ViewXml = string.Format("<View><Query>{0}</Query></View>", view.ViewQuery);

                var items = list.GetItems(caml);
                ctx.Load(items);
                ctx.ExecuteQuery();

                foreach (ListItem item in items)
                {
                    var t = new T();

                    t.MapFromList(item);

                    results.Add(t);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to get " + type.ListName + ". " + ex.Message);
            }

            return(results);
        }
Ejemplo n.º 7
0
        public static T Get(int id)
        {
            var ctx = new ClientContext(SharePointHelper.Url);

            T t = new T();

            try
            {
                var web  = SharePointHelper.GetWeb(ctx);
                var list = SharePointHelper.GetList(ctx, web, t.ListName);

                var item = list.GetItemById(id);
                ctx.Load(item);
                ctx.ExecuteQuery();

                t.MapFromList(item, true);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to get " + t.ListName + " with id " + id + ". " + ex.Message);
            }

            return(t);
        }