Esempio n. 1
0
        private async Task DeleteAllItems(List <ContentItemModel> contentItems)
        {
            var allTasks  = new List <Task>();
            var throttler = new SemaphoreSlim(initialCount: 2);

            foreach (var item in contentItems)
            {
                await throttler.WaitAsync();

                allTasks.Add(
                    Task.Run(async() =>
                {
                    try
                    {
                        logger.Info("Deleting item:" + item.Id);
                        await managementClient.DeleteContentItemAsync(ContentItemIdentifier.ById(item.Id));
                    }
                    catch (KenticoCloud.ContentManagement.Exceptions.ContentManagementException e)
                    {
                        logger.Error(e, "Something went wrong deleting item.");
                    }
                    finally
                    {
                        throttler.Release();
                    }
                }));
            }

            await Task.WhenAll(allTasks);
        }
Esempio n. 2
0
        private async Task <List <ContentItemIdentifier> > GetAllUserItemIdentifiers()
        {
            List <ContentItemIdentifier>            listIds  = new List <ContentItemIdentifier>();
            ListingResponseModel <ContentItemModel> response = await managementClient.ListContentItemsAsync();

            // this is guid of Content Type "User" in the KC project
            ObjectIdentifier userContentType = new ObjectIdentifier()
            {
                Id = Guid.Parse("eaf7e4c9-0a4d-46dc-b787-ef02c9860592")
            };

            while (true)
            {
                foreach (var item in response)
                {
                    if (item.Type == userContentType)
                    {
                        listIds.Add(ContentItemIdentifier.ById(item.Id));
                    }
                }

                if (!response.HasNextPage())
                {
                    break;
                }
                response = await response.GetNextPage();
            }

            return(listIds);
        }
Esempio n. 3
0
        internal async Task <ContentItemVariantModel <UserModel> > CreateUser(IntercomUser user)
        {
            string itemName;

            if (user.Type == "admin")
            {
                itemName = user.Name;
            }
            else
            {
                itemName = user.Id;
            }

            var userItem = await CreateUserItem(itemName, user.Id);

            UserModel userModel = new UserModel
            {
                Name         = user.Name,
                Type         = user.Type,
                Email        = user.Email,
                Id           = user.Id,
                IntercomLink = "<p><a href=\"" + IntercomFunctions.GetIntercomUserLink(user.Id, user.Type) + "\">Link to intercom</a></p>"
            };

            var userVariant = await CreateUserVariant(userModel, ContentItemIdentifier.ById(userItem.Id));

            var publishUser = await PublishItemVariant(userItem.Id.ToString());

            return(userVariant);
        }
Esempio n. 4
0
        public void BuildItemUrl_ItemId_ReturnsCorrectUrl()
        {
            var identifier  = ContentItemIdentifier.ById(ITEM_ID);
            var actualUrl   = _builder.BuildItemUrl(identifier);
            var expectedUrl = $"{ENDPOINT}/projects/{PROJECT_ID}/items/{ITEM_ID}";

            Assert.Equal(expectedUrl, actualUrl);
        }
Esempio n. 5
0
        public void BuildContentItemVariantsUrl_ItemIdVariantCodename_ReturnsCorrectUrl()
        {
            var itemIdentifier    = ContentItemIdentifier.ById(ITEM_ID);
            var variantIdentifier = LanguageIdentifier.ByCodename(VARIANT_CODENAME);
            var identifier        = new ContentItemVariantIdentifier(itemIdentifier, variantIdentifier);
            var actualUrl         = _builder.BuildVariantsUrl(identifier);
            var expectedUrl       = $"{ENDPOINT}/projects/{PROJECT_ID}/items/{ITEM_ID}/variants/codename/{VARIANT_CODENAME}";

            Assert.Equal(expectedUrl, actualUrl);
        }
Esempio n. 6
0
        internal async Task <(ContentItemVariantModel <ConversationModel> variant, bool success)> SyncSingle(Conversation intercomConversation, ContentItemVariantModel <ConversationModel> conversationVariant, List <ContentItemVariantModel <UserModel> > conversationUserVariants)
        {
            currentConversationId    = intercomConversation.id;
            currentConversationUsers = new List <UserModel>()
            {
            };
            currentConversationUsers.AddRange(conversationUserVariants.Select(x => x.Elements));

            ContentItemModel conversationItem = null;
            Guid             itemIdentifier   = Guid.Empty;

            // If conversation doesn't exist, create item for it
            if (conversationVariant == null)
            {
                try
                {
                    logger.Debug("Creating item for conversation: " + currentConversationId);
                    conversationItem = await CreateConversationItem(currentConversationId);

                    itemIdentifier = conversationItem.Id;
                }
                catch (ContentManagementException e)
                {
                    logger.Error(e, "Unable to create item for conversation: " + currentConversationId);
                    return(null, false);
                }
            }
            else
            {
                itemIdentifier = conversationVariant.Item.Id;
            }

            // Fill model with data
            var model = CreateConversationModel(intercomConversation);

            logger.Debug("Creating item variant for conversation: " + currentConversationId);
            var variant = await UpsertConversationVariant(model, ContentItemIdentifier.ById(itemIdentifier));

            var publishConversation = await PublishItemVariant(itemIdentifier.ToString());

            return(variant, true);
        }
Esempio n. 7
0
        private async Task <List <ContentItemIdentifier> > GetAllItemIdentifiers()
        {
            List <ContentItemIdentifier>            listIds  = new List <ContentItemIdentifier>();
            ListingResponseModel <ContentItemModel> response = await managementClient.ListContentItemsAsync();

            while (true)
            {
                foreach (var item in response)
                {
                    listIds.Add(ContentItemIdentifier.ById(item.Id));
                }

                if (!response.HasNextPage())
                {
                    break;
                }
                response = await response.GetNextPage();
            }

            return(listIds);
        }
Esempio n. 8
0
        /// <summary>
        /// </summary>
        /// <param name="elements">The Elements property of the content item to be updated</param>
        /// <param name="codename">If set, the item with the specified code name will be upserted</param>
        static void UpsertSingleItem(Dictionary <string, object> elements, string codename, ContentType type, bool update)
        {
            ContentItemModel contentItem  = null;
            ContentItem      existingItem = null;
            Guid             itemid;

            // Try to get existing content item for updating
            if (update && existingItem == null)
            {
                existingItem = GetExistingContentItem(codename, type.System.Codename);
            }

            // If not updating, create content item first
            if (!update)
            {
                contentItem = CreateNewContentItem(codename, type.System.Codename);
                if (contentItem.Id != null)
                {
                    itemid = contentItem.Id;
                }
                else
                {
                    throw new Exception("Error creating new item.");
                }
            }
            else
            {
                // We are updating existing
                if (existingItem != null)
                {
                    itemid = new Guid(existingItem.System.Id);
                }
                else
                {
                    // Existing item wasn't found, create it
                    contentItem = CreateNewContentItem(codename, type.System.Codename);
                    if (contentItem.Id != null)
                    {
                        itemid = contentItem.Id;
                    }
                    else
                    {
                        throw new Exception("Error creating new item.");
                    }
                }
            }

            // After item is created (or skipped for updateExisting), upsert content
            try
            {
                // Get item variant to upsert
                ContentItemIdentifier        itemIdentifier     = ContentItemIdentifier.ById(itemid);
                LanguageIdentifier           languageIdentifier = LanguageIdentifier.ById(new Guid("00000000-0000-0000-0000-000000000000"));
                ContentItemVariantIdentifier identifier         = new ContentItemVariantIdentifier(itemIdentifier, languageIdentifier);

                elements = ValidateContentTypeFields(elements, type);

                // Set target element value
                ContentItemVariantUpsertModel model = new ContentItemVariantUpsertModel()
                {
                    Elements = elements
                };

                // Upsert item
                var upsertTask = clientCM.UpsertContentItemVariantAsync(identifier, model);
                var response   = upsertTask.GetAwaiter().GetResult();
            }
            catch (ContentManagementException e)
            {
                if (e.Message.ToLower().Contains("cannot update published"))
                {
                    throw new Exception("This tool cannot currently update published content. If you wish to update a published item, you will first need to unpublish it within Kentico Kontent.");
                }
            }
        }