예제 #1
0
        public SPView Get(
            [Documentation(Name = "List", Type = typeof(SPList)),
             Documentation(Name = "ById", Type = typeof(string)),
             Documentation(Name = "ByTitle", Type = typeof(string))]
            IDictionary options)
        {
            SPList list = null;

            if (options != null && options["List"] != null)
            {
                list = (SPList)options["List"];
            }
            else
            {
                return(null);
            }

            var byId    = (options["ById"] != null) ? options["ById"].ToString() : string.Empty;
            var byTitle = (options["ByTitle"] != null) ? options["ByTitle"].ToString() : string.Empty;

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                SP.List splist      = clientContext.ToList(list.Id);
                var     fieldsQuery = clientContext.LoadQuery(splist.Fields.Include(
                                                                  field => field.Title,
                                                                  field => field.InternalName));

                SP.View spview = null;

                if (!string.IsNullOrEmpty(byId))
                {
                    string viewId = options["ById"].ToString();
                    spview = splist.GetView(new Guid(viewId));
                    clientContext.Load(spview);
                    clientContext.Load(spview, SPView.InstanceQuery);
                    clientContext.ExecuteQuery();
                }

                if (spview == null && !string.IsNullOrEmpty(byTitle))
                {
                    string viewTitle = options["ByTitle"].ToString();
                    var    viewQuery = clientContext.LoadQuery(splist.Views
                                                               .Where(view => view.Title == viewTitle)
                                                               .IncludeWithDefaultProperties(SPView.InstanceQuery));
                    clientContext.ExecuteQuery();
                    spview = viewQuery.FirstOrDefault();
                }

                if (spview != null)
                {
                    return(new SPView(spview, fieldsQuery.ToDictionary(item => item.InternalName, item => item.Title)));
                }
            }

            return(null);
        }