Exemple #1
0
        /// <summary>
        /// Creates content types in Contentful from any types with a <see cref="ContentTypeAttribute"/> found in an Assembly.
        /// </summary>
        /// <param name="assemblyName">The assembly to load types from.</param>
        /// <param name="configuration">The configuration for the creation process.</param>
        /// <param name="client">The optional client to use for creation.</param>
        /// <returns>A list of created or updated content types.</returns>
        public static async Task <List <ContentType> > CreateContentTypesFromAssembly(string assemblyName, ContentfulCodeFirstConfiguration configuration, IContentfulManagementClient client = null)
        {
            var types = LoadTypes(assemblyName);
            var contentTypesToCreate = InitializeContentTypes(types);
            var createdContentTypes  = await CreateContentTypes(contentTypesToCreate, configuration, client);

            return(createdContentTypes);
        }
Exemple #2
0
        /// <summary>
        /// Creates a number of content types in Contentful.
        /// </summary>
        /// <param name="contentTypes">The content types to create.</param>
        /// <param name="configuration">The configuration for the creation process.</param>
        /// <param name="client">The optional client to use for creation.</param>
        /// <returns>A list of created or updated content types.</returns>
        public static async Task <List <ContentType> > CreateContentTypes(IEnumerable <ContentTypeInformation> contentTypes, ContentfulCodeFirstConfiguration configuration, IContentfulManagementClient client = null)
        {
            var managementClient = client;

            if (managementClient == null)
            {
                var httpClient = new HttpClient();
                managementClient = new ContentfulManagementClient(httpClient, configuration.ApiKey, configuration.SpaceId);
            }

            var createdTypes = new List <ContentType>();

            var existingContentTypes = (await managementClient.GetContentTypes()).ToList();

            if (configuration.ForceUpdateContentTypes == false)
            {
                //remove any pre-existing content types from the list to be created.
                contentTypes = contentTypes.Where(c => !existingContentTypes.Any(x => x.SystemProperties.Id == c.ContentType.SystemProperties.Id));
            }

            foreach (var contentTypeInfo in contentTypes)
            {
                //make sure to add correct version for existing content types
                contentTypeInfo.ContentType.SystemProperties.Version = existingContentTypes.FirstOrDefault(c => c.SystemProperties.Id == contentTypeInfo.ContentType.SystemProperties.Id)?.SystemProperties.Version;

                var createdContentType = await managementClient.CreateOrUpdateContentType(contentTypeInfo.ContentType, version : contentTypeInfo.ContentType.SystemProperties.Version);

                if (configuration.PublishAutomatically)
                {
                    createdContentType = await managementClient.ActivateContentType(createdContentType.SystemProperties.Id, createdContentType.SystemProperties.Version ?? 1);
                }

                createdTypes.Add(createdContentType);

                if (contentTypeInfo.InterfaceControls != null && contentTypeInfo.InterfaceControls.Any())
                {
                    var currentInterface = await managementClient.GetEditorInterface(createdContentType.SystemProperties.Id);


                    foreach (var control in contentTypeInfo.InterfaceControls)
                    {
                        var index = currentInterface.Controls.FindIndex(c => c.FieldId == control.FieldId);
                        currentInterface.Controls[index] = control;
                    }
                    await managementClient.UpdateEditorInterface(currentInterface, createdContentType.SystemProperties.Id, currentInterface.SystemProperties.Version.Value);
                }
            }

            return(createdTypes);
        }