Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace csclt
- {
- using System;
- using Contentstack.Core;
- using Contentstack.Core.Configuration;
- using Contentstack.Core.Models;
- using System.Threading;
- using Newtonsoft.Json.Linq;
- public class Program
- {
- const int CONTENTSTACK_QUERY_LIMIT = 100;
- static void Main(string[] args)
- {
- ContentstackClient stack = new ContentstackClient(new ContentstackOptions()
- {
- ApiKey = "blt94519f01d8f92c86",
- AccessToken = "cs99af6674ef2a7a53770da6b7",
- Environment = "contentdelivery",
- });
- // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#get-all-content-types
- foreach (JObject contentTypeJson in stack.GetContentTypes().GetAwaiter().GetResult())
- {
- new Thread(() =>
- {
- ContentType contentType = stack.ContentType(contentTypeJson["uid"].ToString());
- ContentstackCollection<Entry> firstPage = GetPage<Entry>(
- stack,
- contentType,
- 0,
- true /*includeCount*/);
- //WARN: firstPage.Count should be something like ContentType.EntryCount (Entries per Content Type, not per page)
- for (int loopSkip = CONTENTSTACK_QUERY_LIMIT; loopSkip < firstPage.Count; loopSkip += CONTENTSTACK_QUERY_LIMIT)
- {
- //WARN: https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp
- int mySkip = loopSkip;
- new Thread(() =>
- {
- ListPage(contentType.ContentTypeId, GetPage<Entry>(
- stack,
- contentType,
- mySkip));
- }).Start();
- }
- // for performance, launch paging threads/HTTP calls in the background, then list first page
- ListPage(contentType.ContentTypeId, firstPage);
- }).Start();
- }
- }
- private static ContentstackCollection<TEntryModel> GetPage<TEntryModel>(
- ContentstackClient stack,
- ContentType contentType,
- int skip,
- bool includeCount = false)
- {
- // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#queries
- // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#skip
- // https://www.contentstack.com/docs/platforms/dot-net/api-reference/api/Contentstack.Core.Models.Query.html
- Query query = contentType.Query().Skip(skip);
- if (includeCount)
- {
- query.IncludeCount();
- }
- return query.Find<TEntryModel>().GetAwaiter().GetResult();
- }
- private static void ListPage(string contentTypeUid, ContentstackCollection<Entry> page)
- {
- if (page.Count < 1)
- {
- Console.WriteLine($"No Entries for [{contentTypeUid}]");
- }
- foreach (Entry entry in page.Items)
- {
- Console.WriteLine($"[{contentTypeUid}] : {entry.Uid} : {entry.Title}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement