Ejemplo n.º 1
0
        /// <summary>
        /// Will evaluate the Library/List and if the list has reached the threshold it will produce SAFE queries
        /// </summary>
        /// <param name="onlineLibrary">The list to query</param>
        /// <param name="camlStatement">A base CAML query upon which the threshold query will be constructed</param>
        /// <returns>A collection of CAML queries NOT including WHERE</returns>
        public static List <string> SafeCamlClauseFromThreshold(this List onlineLibrary, string camlStatement = null, int?defaultLastItemId = default(int?))
        {
            var camlQueries = new List <string>();

            onlineLibrary.EnsureProperties(olp => olp.ItemCount, olp => olp.LastItemModifiedDate, olp => olp.LastItemUserModifiedDate);

            // we have reached a threshold and need to parse based on other criteria
            var itemCount = onlineLibrary.ItemCount;

            if (itemCount > 5000)
            {
                var lastItemId  = (defaultLastItemId.HasValue) ? defaultLastItemId.Value : onlineLibrary.QueryLastItemId(onlineLibrary.LastItemModifiedDate);
                var startIdx    = 0;
                var incrementor = 1000;

                for (var idx = startIdx; idx < lastItemId + 1;)
                {
                    var startsWithId = idx + 1;
                    var endsWithId   = (idx + incrementor);
                    if (endsWithId >= lastItemId)
                    {
                        endsWithId = lastItemId + 1;
                    }

                    var thresholdEq = new SPThresholdEnumerationModel()
                    {
                        StartsWithId = startsWithId,
                        EndsWithId   = endsWithId
                    };

                    var camlThresholdClause = thresholdEq.AndClause;
                    if (!string.IsNullOrEmpty(camlStatement))
                    {
                        camlThresholdClause = CAML.And(camlThresholdClause, camlStatement);
                    }
                    camlQueries.Add(camlThresholdClause);

                    idx += incrementor;
                }
            }
            else
            {
                camlQueries.Add(camlStatement ?? string.Empty);
            }

            return(camlQueries);
        }
        /// <summary>
        /// Will evaluate the Library/List and if the list has reached the threshold it will produce SAFE queries
        /// </summary>
        /// <param name="onlineLibrary">The list to query</param>
        /// <param name="incrementor">(Default) 1000 rows in the query, can specify up to 5000</param>
        /// <param name="camlStatement">A base CAML query upon which the threshold query will be constructed</param>
        /// <param name="defaultStartItemId">(OPTIONAL) if specified the caml queries will begin at ID >= this value</param>
        /// <param name="defaultLastItemId">(OPTIONAL) if specified the caml queries will terminate at this value; if not specified a query will be executed to retreive the lastitemid</param>
        /// <returns>A collection of CAML queries NOT including WHERE</returns>
        public static List <string> SafeCamlClauseFromThreshold(this List onlineLibrary, int incrementor = 1000, string camlStatement = null, Nullable <int> defaultStartItemId = default(Nullable <int>), Nullable <int> defaultLastItemId = default(Nullable <int>))
        {
            if (incrementor > 5000)
            {
                throw new InvalidOperationException(string.Format("CAML Queries must return fewer than 5000 rows, you specified {0}", incrementor));
            }

            var camlQueries = new List <string>();
            var lastItemId  = 0;
            var startIdx    = 0;

            // Check if the List/Library ItemCount exists
            if (!onlineLibrary.IsPropertyAvailable(octx => octx.ItemCount))
            {
                onlineLibrary.Context.Load(onlineLibrary, octx => octx.ItemCount);
                onlineLibrary.Context.ExecuteQueryRetry();
            }

            // we have reached a threshold and need to parse based on other criteria
            var itemCount = onlineLibrary.ItemCount;

            if (itemCount > 5000)
            {
                if (defaultStartItemId.HasValue)
                {
                    startIdx = defaultStartItemId.Value;
                }

                if (defaultLastItemId.HasValue)
                {
                    lastItemId = defaultLastItemId.Value;
                }
                else
                {
                    lastItemId = onlineLibrary.QueryLastItemId();
                }

                for (var idx = startIdx; idx < lastItemId + 1;)
                {
                    var startsWithId = idx + 1;
                    var endsWithId   = (idx + incrementor);
                    if (endsWithId >= lastItemId)
                    {
                        endsWithId = lastItemId + 1;
                    }

                    var thresholdEq = new SPThresholdEnumerationModel()
                    {
                        StartsWithId = startsWithId,
                        EndsWithId   = endsWithId
                    };

                    var camlThresholdClause = thresholdEq.AndClause;
                    if (!string.IsNullOrEmpty(camlStatement))
                    {
                        camlThresholdClause = CAML.And(camlThresholdClause, camlStatement);
                    }
                    camlQueries.Add(camlThresholdClause);

                    idx += incrementor;
                }
            }
            else
            {
                camlQueries.Add(camlStatement ?? string.Empty);
            }

            return(camlQueries);
        }