Assumptions
Create a new project (optional)
Create a genre taxonomy node
Create a movie content type
Import some movies
1
git clone https://github.com/contensis/dotnet-movie-import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
namespace MovieImport
{
public class Movie
{
public string Title { get; set; }
public string Overview { get; set; }
public int Runtime { get; set; }
public string[] Genres { get; set; }
public DateTime ReleaseDate { get; set; }
public long Revenue { get; set; }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CsvHelper;
using Zengenti.Contensis.Management;
namespace MovieImport
{
class Program
{
// The mapping between the taxonomy node name in the CSV and the taxonomy key to save
private static readonly Dictionary<string, string> GenreTaxonomyKeys = new Dictionary<string, string>
{
{"Action", "0/5/1"},
{"Comedy", "0/5/2"},
{"Crime", "0/5/3"},
{"Drama", "0/5/4"},
{"Romance", "0/5/5"},
{"Western", "0/5/6"}
};
static void Main()
{
// Create the management client
var client = ManagementClient.Create("<Contensis URL>",
"<Client ID>",
"<Shared Secret>");
// Get the project
var project = client.Projects.Get("<Project Id>");
// Read the movie CSV file
using (var textReader = File.OpenText(@"Movies.csv"))
{
var csv = new CsvReader(textReader);
// Iterate over each movie, creating it
foreach (var movie in csv.GetRecords<Movie>())
{
CreateMovie(project, movie);
}
}
}
private static void CreateMovie(Project project, Movie movie)
{
// Set-up a new movie entry
var movieEntry = project.Entries.New("movie");
// Set each field value
movieEntry.Set("title", movie.Title);
movieEntry.Set("overview", movie.Overview);
movieEntry.Set("releaseDate", movie.ReleaseDate);
movieEntry.Set("runtime", movie.Runtime);
movieEntry.Set("genres", GetGenreTaxonomyKeys(movie.Genres));
movieEntry.Set("revenue", movie.Revenue);
// Save and publish the movie
movieEntry.Save();
movieEntry.Publish();
Console.WriteLine({{APP}}quot;Saved movie {movieEntry.Get("title")}");
}
private static string[] GetGenreTaxonomyKeys(string genreNames)
{
var genres = genreNames.Split(',');
return genres.Select(genre => GenreTaxonomyKeys[genre]).ToArray();
}
}
}
Summary