コード例 #1
0
        public IEnumerable <T> Retrieve()
        {
            if (String.IsNullOrWhiteSpace(this.ListName))
            {
                throw new Exception("The list name was not provided.");
            }

            if (!this.FieldsMapping.HasItems())
            {
                throw new Exception("The field mapping is empty.");
            }

            var clientContext = RepositorySharepointAccess.GetContext();
            var oList         = clientContext.Web.Lists.GetByTitle(this.ListName);
            var camlQuery     = new CamlQuery {
                ViewXml = this.GetCamlQuery()
            };

            /*
             * var sw = new StringWriter();
             * var oWebsite = clientContext.Web;
             * var collList = oWebsite.Lists;
             * var listInfo = clientContext.LoadQuery(
             *  collList.Include(
             *      list => list.Title,
             *      list => list.Fields.Include(field => field.Title, field => field.InternalName)));
             *
             * clientContext.ExecuteQuery();
             *
             * foreach (var list in listInfo)
             * {
             *  var collField = list.Fields;
             *
             *  foreach (var oField in collField)
             *  {
             *      sw.WriteLine("List: {0} \n\t Field Title: {1} \n\t Field Internal Name: {2}", list.Title, oField.Title, oField.InternalName);
             *  }
             * }
             * var lalala = sw.ToString();
             */

            var collListItem = oList.GetItems(camlQuery);

            clientContext.Load(collListItem);
            clientContext.ExecuteQuery();

            var itemType = typeof(T);
            var itens    = new List <T>();

            foreach (var oListItem in collListItem)
            {
                var item = Activator.CreateInstance <T>();

                foreach (var fieldMap in this.FieldsMapping)
                {
                    var prop = itemType.GetProperty(fieldMap.Key);
                    if (prop != null && (oListItem[fieldMap.Value.Name] is FieldLookupValue) && oListItem[fieldMap.Value.Name] != null)
                    {
                        prop.SetValue(item, Convert.ChangeType(((FieldLookupValue)oListItem[fieldMap.Value.Name]).LookupValue, prop.PropertyType));
                    }
                    else if (prop != null && !(oListItem[fieldMap.Value.Name] is FieldUserValue) && oListItem[fieldMap.Value.Name] != null)
                    {
                        prop.SetValue(item, Convert.ChangeType(oListItem[fieldMap.Value.Name], prop.PropertyType));
                    }
                }

                itens.Add(item);
            }

            return(itens.HasItems() ? itens : null);
        }
コード例 #2
0
 public SharepointQueryable(string listName, object fieldsMapping)
 {
     this.ListName      = listName;
     this.FieldsMapping = RepositorySharepointAccess.GetFieldsMappings(fieldsMapping);
 }