Advertisement
commodore73

Iterate Entries

Jul 22nd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. namespace csclt
  2. {
  3.     using System;
  4.  
  5.     using Contentstack.Core;
  6.     using Contentstack.Core.Configuration;
  7.     using Contentstack.Core.Models;
  8.     using System.Threading;
  9.     using Newtonsoft.Json.Linq;
  10.  
  11.     public class Program
  12.     {
  13.         const int CONTENTSTACK_QUERY_LIMIT = 100;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             ContentstackClient stack = new ContentstackClient(new ContentstackOptions()
  18.             {
  19.                 ApiKey = "blt94519f01d8f92c86",
  20.                 AccessToken = "cs99af6674ef2a7a53770da6b7",
  21.                 Environment = "contentdelivery",
  22.             });
  23.  
  24.             // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#get-all-content-types
  25.             foreach (JObject contentTypeJson in stack.GetContentTypes().GetAwaiter().GetResult())
  26.             {
  27.                 new Thread(() =>
  28.                 {
  29.                     ContentType contentType = stack.ContentType(contentTypeJson["uid"].ToString());
  30.                     ContentstackCollection<Entry> firstPage = GetPage<Entry>(
  31.                         stack,
  32.                         contentType,
  33.                         0,
  34.                         true /*includeCount*/);
  35.  
  36.                     //WARN: firstPage.Count should be something like ContentType.EntryCount (Entries per Content Type, not per page)
  37.                     for (int loopSkip = CONTENTSTACK_QUERY_LIMIT; loopSkip < firstPage.Count; loopSkip += CONTENTSTACK_QUERY_LIMIT)
  38.                     {
  39.                         //WARN: https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp
  40.                         int mySkip = loopSkip;
  41.                         new Thread(() =>
  42.                         {
  43.                             ListPage(contentType.ContentTypeId, GetPage<Entry>(
  44.                                     stack,
  45.                                     contentType,
  46.                                     mySkip));
  47.                         }).Start();
  48.                     }
  49.  
  50.                     // for performance, launch paging threads/HTTP calls in the background, then list first page
  51.                     ListPage(contentType.ContentTypeId, firstPage);
  52.                 }).Start();
  53.             }
  54.         }
  55.  
  56.         private static ContentstackCollection<TEntryModel> GetPage<TEntryModel>(
  57.             ContentstackClient stack,
  58.             ContentType contentType,
  59.             int skip,
  60.             bool includeCount = false)
  61.         {
  62.             // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#queries
  63.             // https://www.contentstack.com/docs/developers/apis/content-delivery-api/#skip
  64.             // https://www.contentstack.com/docs/platforms/dot-net/api-reference/api/Contentstack.Core.Models.Query.html
  65.             Query query = contentType.Query().Skip(skip);
  66.  
  67.             if (includeCount)
  68.             {
  69.                 query.IncludeCount();
  70.             }
  71.  
  72.             return query.Find<TEntryModel>().GetAwaiter().GetResult();
  73.         }
  74.  
  75.         private static void ListPage(string contentTypeUid, ContentstackCollection<Entry> page)
  76.         {
  77.             if (page.Count < 1)
  78.             {
  79.                 Console.WriteLine($"No Entries for [{contentTypeUid}]");
  80.             }
  81.  
  82.             foreach (Entry entry in page.Items)
  83.             {
  84.                 Console.WriteLine($"[{contentTypeUid}] : {entry.Uid} : {entry.Title}");
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement