コード例 #1
0
        public static ListItemCollection GetDocuments(string fieldName, string fieldValue)
        {
            CAMLQueryFilter filter = new CAMLQueryFilter();

            if (!string.IsNullOrEmpty(fieldName))
            {
                SPCAMLQueryBuilder.FieldType fieldType = SharePointDocument.GetFieldTypeByFieldName(fieldName);

                switch (fieldType)
                {
                case SPCAMLQueryBuilder.FieldType.Lookup:
                    int  intValue = int.MinValue;
                    bool isInt    = int.TryParse(fieldValue, out intValue);
                    if (isInt)
                    {
                        filter = new CAMLQueryLookupFilter(fieldName, intValue, QueryType.Equal);
                    }
                    else
                    {
                        filter = new CAMLQueryLookupFilter(fieldName, fieldValue, QueryType.Equal);
                    }

                    break;

                default:
                    filter = new CAMLQueryGenericFilter(fieldName, fieldType, fieldValue, QueryType.Equal);
                    break;
                }
            }

            CAMLQueryBuilder builder = new CAMLQueryBuilder(filter);


            builder.DocumentFilter(FSObjType.Document, true);

            builder.AddViewFields(SharePointDocument.GetAllFieldNames());

            builder.BuildQuery();
            builder.OrderBy("Created", false);
            builder.BuildViewFields();

            CamlQuery camlQuery = new CamlQuery();

            camlQuery.ViewXml = builder.ToString();

            ClientContext ctx = ConnectToSharePoint();

            List spList = ctx.Web.Lists.GetByTitle("Documents");

            ctx.Load(spList);
            ctx.ExecuteQuery();

            if (spList != null && spList.ItemCount > 0)
            {
                ListItemCollection listItems = spList.GetItems(camlQuery);
                ctx.Load(listItems);
                ctx.ExecuteQuery();

                // ctx.Dispose();

                return(listItems);
            }
            else
            {
                return(null);
            }
        }