示例#1
0
        static string UpsertAsset(Google.Apis.Drive.v3.Data.File file, bool update, Stream stream)
        {
            string            externalId = file.Name;
            string            message    = null;
            FileContentSource fcs        = new FileContentSource(stream, externalId, file.MimeType);
            AssetModel        result;
            var emptyDescriptions = new List <AssetDescription>
            {
                new AssetDescription {
                    Description = "", Language = LanguageIdentifier.ById(new Guid("00000000-0000-0000-0000-000000000000"))
                }
            };

            try
            {
                var           fileTask = clientCM.UploadFileAsync(fcs);
                FileReference fr       = fileTask.GetAwaiter().GetResult();

                IEnumerable <AssetDescription> descriptions = new List <AssetDescription>();
                AssetUpsertModel model = new AssetUpsertModel
                {
                    FileReference = fr,
                    Descriptions  = emptyDescriptions
                };
                var createTask = clientCM.CreateAssetAsync(model);
                result = createTask.GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return(message);
        }
示例#2
0
        public void BuildContentItemVariantsUrl_ItemCodenameVariantId_ReturnsCorrectUrl()
        {
            var itemIdentifier    = ContentItemIdentifier.ByCodename(ITEM_CODENAME);
            var variantIdentifier = LanguageIdentifier.ById(VARIANT_ID);
            var identifier        = new ContentItemVariantIdentifier(itemIdentifier, variantIdentifier);
            var actualUrl         = _builder.BuildVariantsUrl(identifier);
            var expectedUrl       = $"{ENDPOINT}/projects/{PROJECT_ID}/items/codename/{ITEM_CODENAME}/variants/{VARIANT_ID}";

            Assert.Equal(expectedUrl, actualUrl);
        }
示例#3
0
        public void BuildContentItemVariantsUrl_ItemExternalIdVariantId_ReturnsCorrectUrl()
        {
            var itemIdentifier    = ContentItemIdentifier.ByExternalId(ITEM_EXTERNAL_ID);
            var variantIdentifier = LanguageIdentifier.ById(VARIANT_ID);
            var identifier        = new ContentItemVariantIdentifier(itemIdentifier, variantIdentifier);
            var actualUrl         = _builder.BuildVariantsUrl(identifier);
            var expectedUrl       = $"{ENDPOINT}/projects/{PROJECT_ID}/items/external-id/{EXPECTED_ITEM_EXTERNAL_ID}/variants/{VARIANT_ID}";

            Assert.Equal(expectedUrl, actualUrl);
        }
示例#4
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.");
                }
            }
        }