Esempio n. 1
0
        internal static async Task <ContentItemModel> PrepareTestItem(ContentManagementClient client, string typeCodename, string externalId = null)
        {
            var type = ContentTypeIdentifier.ByCodename(typeCodename);

            if (externalId != null)
            {
                // We use upsert for preparing item by external ID in order to be able to recover from situation when previous test run didn't clean up properly
                var item = new ContentItemUpsertModel()
                {
                    Name = "Hooray!",
                    Type = type,
                };

                return(await client.UpsertContentItemByExternalIdAsync(externalId, item));
            }
            else
            {
                var item = new ContentItemCreateModel()
                {
                    Name = "Hooray!",
                    Type = type,
                };

                return(await client.CreateContentItemAsync(item));
            }
        }
Esempio n. 2
0
        internal static async Task <ContentItemVariantModel> PrepareTestVariant(ContentManagementClient client, string languageCodename, object elements, ContentItemModel item)
        {
            var addedItemIdentifier                = ContentItemIdentifier.ByCodename(item.CodeName);
            var addedLanguageIdentifier            = LanguageIdentifier.ByCodename(languageCodename);
            var addedContentItemLanguageIdentifier = new ContentItemVariantIdentifier(addedItemIdentifier, addedLanguageIdentifier);
            var variantUpdateModel = new ContentItemVariantUpsertModel()
            {
                Elements = elements
            };

            return(await client.UpsertContentItemVariantAsync(addedContentItemLanguageIdentifier, variantUpdateModel));
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var options = new ContentManagementOptions
            {
                ProjectId = ConfigurationManager.AppSettings["ProjectId"],
                ApiKey    = ConfigurationManager.AppSettings["ContentManagementApiKey"]
            };
            var client = new ContentManagementClient(options);

            var folderPath = ConfigurationManager.AppSettings["DatabaseFolderPath"];

            ImportActors(folderPath, client);
            ImportMovies(folderPath, client);
        }
Esempio n. 4
0
        private static void ImportMovies(string folderPath, ContentManagementClient client)
        {
            foreach (var movie in DatabaseEntry.CreateFromFolder(Path.Combine(folderPath, "Movies")))
            {
                Console.WriteLine($"Importing {movie.GetText("Name")}");

                var imageExternalIds = ImportMoviePhotos(movie, folderPath, client);

                var externalId = $"Movie - {movie.ExternalId}";

                var item = new ContentItemUpsertModel
                {
                    Name             = movie.GetText("Name"),
                    Type             = ContentTypeIdentifier.ByCodename("movie"),
                    SitemapLocations = movie.GetListItems("Sitemap location").Select(x => SitemapNodeIdentifier.ByCodename(GetCodename(x)))
                };

                client.UpsertContentItemByExternalIdAsync(externalId, item);

                var itemIdentifier     = ContentItemIdentifier.ByExternalId(externalId);
                var languageIdentifier = LanguageIdentifier.DEFAULT_LANGUAGE;

                var variant = new ContentItemVariantUpsertModel
                {
                    Elements = new
                    {
                        name         = movie.GetText("Name"),
                        description  = movie.GetText("Description"),
                        synopsis     = movie.GetText("Synopsis"),
                        release_date = movie.GetDateTime("Release date"),
                        genre        = movie.GetListItems("Genres").Select(x => TaxonomyTermIdentifier.ByCodename(GetCodename(x))),
                        cast         = movie.GetListItems("Cast").Select(x => ContentItemIdentifier.ByExternalId($"Actor - {x}")),
                        imdb_rating  = movie.GetNumber("IMDB rating"),
                        rating       = new [] { MultipleChoiceOptionIdentifier.ByCodename(GetCodename(movie.GetText("Rating"))) },
                        slug         = movie.GetText("Slug"),
                        photos       = imageExternalIds.Select(imageExternalId => AssetIdentifier.ByExternalId(imageExternalId))
                    },
                };

                client.UpsertContentItemVariantAsync(new ContentItemVariantIdentifier(itemIdentifier, languageIdentifier), variant);
            }
        }
Esempio n. 5
0
        public async static Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext context)
        {
            try
            {
                var config = new ConfigurationBuilder()
                             .SetBasePath(context.FunctionAppDirectory)
                             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                             .AddEnvironmentVariables()
                             .Build();


                string strLanguageCode = config["KenticoCloudLanguageCode"];

                ContentManagementOptions options = new ContentManagementOptions
                {
                    ProjectId = config["KenticoCloudProjectID"],
                    ApiKey    = config["KenticoCloudContentManagementAPIKey"]
                };

                // Initializes an instance of the ContentManagementClient client
                ContentManagementClient client = new ContentManagementClient(options);

                // Defines the content elements to update
                Task <string> body = new StreamReader(req.Body).ReadToEndAsync();

                ArticleModel NewArticleModel = JsonConvert.DeserializeObject <ArticleModel>(body.Result.ToString());

                // Specifies the content item and the language variant
                ContentItemIdentifier        itemIdentifier     = ContentItemIdentifier.ByCodename(NewArticleModel.OriginalCodename);
                LanguageIdentifier           languageIdentifier = LanguageIdentifier.ByCodename(strLanguageCode);
                ContentItemVariantIdentifier identifier         = new ContentItemVariantIdentifier(itemIdentifier, languageIdentifier);

                // Upserts a language variant of your content item
                ContentItemVariantModel <Article> responseUpdate = await client.UpsertContentItemVariantAsync <Article>(identifier, NewArticleModel.NewArticle);

                return((ActionResult) new OkObjectResult($"SUCCESS: Language variant added!"));
            }
            catch (Exception ex)
            {
                return(new OkObjectResult("FAILURE: " + ex.Message));
            }
        }
Esempio n. 6
0
        private static void ImportActors(string folderPath, ContentManagementClient client)
        {
            foreach (var actor in DatabaseEntry.CreateFromFolder(Path.Combine(folderPath, "Actors")))
            {
                Console.WriteLine($"Importing {actor.GetText("Name")}");

                var imageExternalId = ImportActorPhoto(actor, folderPath, client);

                var externalId = $"Actor - {actor.ExternalId}";

                var item = new ContentItemUpsertModel
                {
                    Name = actor.GetText("Name"),
                    Type = ContentTypeIdentifier.ByCodename("actor"),
                };

                client.UpsertContentItemByExternalIdAsync(externalId, item);

                var itemIdentifier     = ContentItemIdentifier.ByExternalId(actor.ExternalId);
                var languageIdentifier = LanguageIdentifier.DEFAULT_LANGUAGE;

                var variant = new ContentItemVariantUpsertModel
                {
                    Elements = new
                    {
                        name  = actor.GetText("Name"),
                        born  = actor.GetDateTime("Born"),
                        bio   = actor.GetText("Bio"),
                        photo = new [] { AssetIdentifier.ByExternalId(imageExternalId) }
                    },
                };

                client.UpsertContentItemVariantAsync(
                    new ContentItemVariantIdentifier(itemIdentifier, languageIdentifier),
                    variant
                    );
            }
        }
Esempio n. 7
0
        public KontentFunctions(KontentFunctionsSettings settings)
        {
            options = new ContentManagementOptions
            {
                ProjectId = settings.ProjectId,
                ApiKey    = settings.CMApiKey
            };
            managementClient = new ContentManagementClient(options);

            cleanProject = settings.CleanProject;

            userItemVariants = new Dictionary <string, ContentItemVariantModel <UserModel> > {
            };

            conversationTypeGuid = Guid.Parse(settings.ConversationTypeGuid);
            userTypeGuid         = Guid.Parse(settings.UserTypeGuid);

            bannedConversations = settings.BannedConversations.Split(',');

            if (!EnsureProject().Result)
            {
                throw new Exception("");
            }
        }
Esempio n. 8
0
 public ImportController(string projectId, string apiKey)
 {
     _managementClient = new ContentManagementClient(new ContentManagementOptions {
         ProjectId = projectId, ApiKey = apiKey
     });
 }
Esempio n. 9
0
 public static void Init(IDeliveryClient delclient, ContentManagementClient cmclient)
 {
     clientDelivery = delclient;
     clientCM       = cmclient;
 }
Esempio n. 10
0
        private static string ImportActorPhoto(DatabaseEntry actor, string folderPath, ContentManagementClient client)
        {
            var externalId  = $"Actor image - {actor.ExternalId}";
            var filePath    = Path.Combine(folderPath, "Actors", "images", $"{actor.ExternalId}.jpg");
            var contentType = "image/jpeg";

            var descriptions = new List <AssetDescription> {
                new AssetDescription
                {
                    Language    = LanguageIdentifier.DEFAULT_LANGUAGE,
                    Description = actor.GetText("Name")
                }
            };

            client.UpsertAssetByExternalIdAsync(
                actor.ExternalId,
                new FileContentSource(filePath, contentType),
                new List <AssetDescription>()
                );

            return(externalId);
        }
Esempio n. 11
0
        private static string[] ImportMoviePhotos(DatabaseEntry movie, string folderPath, ContentManagementClient client)
        {
            folderPath = Path.Combine(folderPath, "Movies", "images", movie.ExternalId);
            var externalIds = new List <string>();
            var contentType = "image/jpeg";

            foreach (var filePath in Directory.EnumerateFiles(folderPath, "*.jpg", SearchOption.TopDirectoryOnly))
            {
                var externalId = $"Movie image - {movie.ExternalId} ({Path.GetFileNameWithoutExtension(filePath)})";

                var descriptions = new List <AssetDescription> {
                    new AssetDescription
                    {
                        Language    = LanguageIdentifier.DEFAULT_LANGUAGE,
                        Description = movie.GetText("Name")
                    }
                };

                var file = client.UpsertAssetByExternalIdAsync(movie.ExternalId, new FileContentSource(filePath, contentType), new List <AssetDescription>());

                externalIds.Add(externalId);
            }

            return(externalIds.ToArray());
        }