I'm trying to programatically add stylesheet properties (to be used in a richtext editor) but am struggling to find the right classes in the API that will let me work with Stylesheets (in particular StyleSheetProperties).
Can somebody point me in the right direction (I'm keen to avoid direct SQL Inserts to the umbracoNode, cmsStylesheet and cmsStylesheetProperty tables!?)
That is how Umbraco does it now (including v6.1.3) - but this is legacy code and will probably be phased out as it is replaced by the Umbraco.Core versions.
Extra Credit - v6.x API ?
I've had a look in v6.x - it doesn't look like it's used yet but
v6.x api usually follows a Service -> Model pattern
so you connect to a service, get the model, do the work and save it back.
for stylesheets it looks like you use the FileService so
var _fileService = ApplicationContext.Current.Services.FileService;
var stylesheet = _fileService.GetStylesheetByName("name");
then you have a stylesheet, you can do stuff on - I can't quite see how you alter the properties from here, as they are read only.
stylesheet.Properties
You can poke around the code see if it sheds any light:
It appears the new API is all about the CSS file itself, not the database record maintained by Umbraco. The properties collection is writeable by casting it to List<StylesheetProperty>. This will add a property, at least to an empty CSS file.
var fileService = ApplicationContext.Current.Services.FileService;
var stylesheet = fileService.GetStylesheetByName("test.css");
if (stylesheet != null)
{
var property = new StylesheetProperty(".test", "color: blue;");
var properties = stylesheet.Properties as List<StylesheetProperty>;
if (properties != null)
{
properties.Add(property);
fileService.SaveStylesheet(stylesheet);
}
}
I can only get the database record to update using the old API. This API will happily create two stylesheets or properties with the same details, so it's important to check for existence first.
var user = umbraco.BusinessLogic.User.GetCurrent();
var stylesheet = StyleSheet.GetByName("test") ?? StyleSheet.MakeNew(user, "test", "test.css", String.Empty);
if (!stylesheet.Properties.Any(prop => prop.Text == "test style"))
{
var property = StylesheetProperty.MakeNew("test style", stylesheet, user);
property.Alias = ".test";
property.value = "color: red;";
}
String.Empty here is the content of the stylesheet in the Umbraco database. You could pass the real content here but I'm not sure what purpose it serves apart from allowing you to save it back to a file. I want to start with a CSS file, so it should be possible to read and parse the CSS using the new API and write properties to the database using the old API.
Quick Stylesheet API question
I'm trying to programatically add stylesheet properties (to be used in a richtext editor) but am struggling to find the right classes in the API that will let me work with Stylesheets (in particular StyleSheetProperties).
Can somebody point me in the right direction (I'm keen to avoid direct SQL Inserts to the umbracoNode, cmsStylesheet and cmsStylesheetProperty tables!?)
(Using V 6.1.2)
Hi,
If i was looking i would start with the source -
Current (Legacy) Method used in Packager
Looking at how the Packager puts stylesheets into umbraco - it calls Stylesheet.Import
This is on Line 431 of the stylesheet code. https://github.com/umbraco/Umbraco-CMS/blob/6.1.4/src/umbraco.cms/businesslogic/web/StyleSheet.cs#L431
That is how Umbraco does it now (including v6.1.3) - but this is legacy code and will probably be phased out as it is replaced by the Umbraco.Core versions.
Extra Credit - v6.x API ?
I've had a look in v6.x - it doesn't look like it's used yet but
v6.x api usually follows a Service -> Model pattern
so you connect to a service, get the model, do the work and save it back.
for stylesheets it looks like you use the FileService so
then you have a stylesheet, you can do stuff on - I can't quite see how you alter the properties from here, as they are read only.
You can poke around the code see if it sheds any light:
FileService : https://github.com/umbraco/Umbraco-CMS/blob/6.1.4/src/Umbraco.Core/Services/FileService.cs
Stylesheet Model : https://github.com/umbraco/Umbraco-CMS/blob/e41d3c7e844b63780a4842ccd105e8bb593e4d54/src/Umbraco.Core/Models/Stylesheet.cs
It appears the new API is all about the CSS file itself, not the database record maintained by Umbraco. The properties collection is writeable by casting it to
List<StylesheetProperty>
. This will add a property, at least to an empty CSS file.I can only get the database record to update using the old API. This API will happily create two stylesheets or properties with the same details, so it's important to check for existence first.
String.Empty here is the content of the stylesheet in the Umbraco database. You could pass the real content here but I'm not sure what purpose it serves apart from allowing you to save it back to a file. I want to start with a CSS file, so it should be possible to read and parse the CSS using the new API and write properties to the database using the old API.
is working on a reply...