/
Program.cs
175 lines (157 loc) · 8.25 KB
/
Program.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Data.SqlServerCe;
using System.IO;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
namespace UmbConsole
{
/// <summary>
/// Before running this console app please ensure that the "umbracoDbDSN" ConnectionString is pointing to your database.
/// If you are using Sql Ce please replace "|DataDirectory|" with a real path or alternatively place
/// your database in the debug folder before running the application in debug mode.
/// </summary>
class Program
{
static void Main(string[] args)
{
Console.Title = "Umbraco Console";
//Initialize the application
var application = new ConsoleApplicationBase();
application.Start(application, new EventArgs());
Console.WriteLine("Application Started");
Console.WriteLine("--------------------");
//Write status for ApplicationContext
var context = ApplicationContext.Current;
Console.WriteLine("ApplicationContext is available: " + (context != null).ToString());
//Write status for DatabaseContext
var databaseContext = context.DatabaseContext;
Console.WriteLine("DatabaseContext is available: " + (databaseContext != null).ToString());
//Write status for Database object
var database = databaseContext.Database;
Console.WriteLine("Database is available: " + (database != null).ToString());
Console.WriteLine("--------------------");
//Get the ServiceContext and the two services we are going to use
var serviceContext = context.Services;
var contentService = serviceContext.ContentService;
var contentTypeService = serviceContext.ContentTypeService;
//Exit the application?
var waitOrBreak = true;
while (waitOrBreak)
{
//List options
Console.WriteLine("-- Options --");
Console.WriteLine("List content nodes: l");
Console.WriteLine("Create new content: c");
Console.WriteLine("Create Umbraco database schema in empty db: d");
Console.WriteLine("Quit application: q");
var input = Console.ReadLine();
if (string.IsNullOrEmpty(input) == false && input.ToLowerInvariant().Equals("q"))
waitOrBreak = false;//Quit the application
else if (string.IsNullOrEmpty(input) == false && input.ToLowerInvariant().Equals("l"))
ListContentNodes(contentService);//Call the method that lists all the content nodes
else if (string.IsNullOrEmpty(input) == false && input.ToLowerInvariant().Equals("c"))
CreateNewContent(contentService, contentTypeService);//Call the method that does the actual creation and saving of the Content object
else if (string.IsNullOrEmpty(input) == false && input.ToLowerInvariant().Equals("d"))
CreateDatabaseSchema(database, databaseContext.DatabaseProvider, application.DataDirectory);
}
}
/// <summary>
/// Private method to list all content nodes
/// </summary>
/// <param name="contentService"></param>
private static void ListContentNodes(IContentService contentService)
{
//Get the Root Content
var rootContent = contentService.GetRootContent();
foreach (var content in rootContent)
{
Console.WriteLine("Root Content: " + content.Name + ", Id: " + content.Id);
//Get Descendants of the current content and write it to the console ordered by level
var descendants = contentService.GetDescendants(content);
foreach (var descendant in descendants.OrderBy(x => x.Level))
{
Console.WriteLine("Name: " + descendant.Name + ", Id: " + descendant.Id + " - Parent Id: " + descendant.ParentId);
}
}
}
/// <summary>
/// Private method to create new content
/// </summary>
/// <param name="contentService"></param>
/// <param name="contentTypeService"></param>
private static void CreateNewContent(IContentService contentService, IContentTypeService contentTypeService)
{
//We find all ContentTypes so we can show a nice list of everything that is available
var contentTypes = contentTypeService.GetAllContentTypes();
var contentTypeAliases = string.Join(", ", contentTypes.Select(x => x.Alias));
Console.WriteLine("Please enter the Alias of the ContentType ({0}):", contentTypeAliases);
var contentTypeAlias = Console.ReadLine();
Console.WriteLine("Please enter the Id of the Parent:");
var strParentId = Console.ReadLine();
int parentId;
if (int.TryParse(strParentId, out parentId) == false)
parentId = -1;//Default to -1 which is the root
Console.WriteLine("Please enter the name of the Content to create:");
var name = Console.ReadLine();
//Create the Content
var content = contentService.CreateContent(name, parentId, contentTypeAlias);
foreach (var property in content.Properties)
{
Console.WriteLine("Please enter the value for the Property with Alias '{0}':", property.Alias);
var value = Console.ReadLine();
var isValid = property.IsValid(value);
if (isValid)
{
property.Value = value;
}
else
{
Console.WriteLine("The entered value was not valid and thus not saved");
}
}
//Save the Content
contentService.Save(content);
Console.WriteLine("Content was saved: " + content.HasIdentity);
}
/// <summary>
/// Private method to install the umbraco database schema in an empty database
/// </summary>
/// <param name="database"></param>
/// <param name="databaseProvider"></param>
/// <param name="dataDirectory"></param>
private static void CreateDatabaseSchema(Database database, DatabaseProviders databaseProvider, string dataDirectory)
{
Console.WriteLine("Please note that installing the umbraco database schema requires an empty database configured in config.");
Console.WriteLine("The 'umbracoConfigurationStatus' under appSettings should be left blank.");
Console.WriteLine("If you are using Sql Ce an empty Umbraco.sdf file should exist in the DataDictionary.");
Console.WriteLine("Press y to continue");
var input = Console.ReadLine();
if (string.IsNullOrEmpty(input) == false && input.ToLowerInvariant().Equals("y"))
{
try
{
if (databaseProvider == DatabaseProviders.SqlServerCE)
{
var dbPath = Path.Combine(dataDirectory, "Umbraco.sdf");
if (File.Exists(dbPath) == false)
{
var engine = new SqlCeEngine(@"Data Source=|DataDirectory|\Umbraco.sdf;Flush Interval=1;");
engine.CreateDatabase();
}
}
database.CreateDatabaseSchema(false);
Console.WriteLine("The database schema has been installed");
Console.WriteLine("Note: This is just an example, so no backoffice user has been created.");
}
catch (Exception e)
{
Console.WriteLine("An error occured while trying to install the database schema");
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
}
}