How to set template for Content in umbraco 5 programmaticly
Hi,
I'm trying to set the template to my conent i created programmaticly, but i nothised that it doesn't have any template or alternetive templates. When i try to set and save a template nothing changes.
But when i add alttemplate in the query string it all works but this is not the solusion i whant.
i got the code for createing content programmaticly from here: https://gist.github.com/1817726 this works greate i'm just missing the templates.
Properties = new HashSet<ContentProperty>(properties)
};
var contentRepository = new List<ContentEditorModel> { contentNode };
//store node in DB
var mappedCollection = FrameworkContext.TypeMappers.Map<IEnumerable<ContentEditorModel>, IEnumerable<Revision<TypedEntity>>>(contentRepository).ToArray();
How to set template for Content in umbraco 5 programmaticly
Hi,
I'm trying to set the template to my conent i created programmaticly, but i nothised that it doesn't have any template or alternetive templates. When i try to set and save a template nothing changes.
But when i add alttemplate in the query string it all works but this is not the solusion i whant.
i got the code for createing content programmaticly from here: https://gist.github.com/1817726 this works greate i'm just missing the templates.
here is some of my code that might be relevant:
Action 1:
DocumentTypeEditorModel = GetDocumentType(DOCUMENT_TYPE_ALIAS);
var contentNode = new ContentEditorModel
{
Id = new HiveId(Guid.NewGuid()),
DocumentTypeId = DocumentTypeEditorModel.Id,
DocumentTypeAlias = DocumentTypeEditorModel.Alias,
ParentId = parentHiveId,
Properties = new HashSet<ContentProperty>(properties)
};
Action 2:
var documentType = GetDocumentType(DOCUMENT_TYPE_ALIAS);
Template template = null;
using(var reader = Hive.TryGetReader<IFileStore>(new Uri("storage://templates")).CreateReadonly())
{
var selectedTemplate = reader.Repositories.Get<File>(documentType.DefaultTemplateId.Value);
if (selectedTemplate != null)
{
template = FrameworkContext.TypeMappers.Map<Template>(selectedTemplate);
}
}
I can't update my post so here a reply:
I updated my code abit, but it still can't find the template. here is the new code, i hope some1 can help:
var writer = Hive.OpenWriter<IContentStore>();
//Gets question.
var questions = writer.Repositories.Get<Content>(false, answerQuestionViewModel.Question.Id);
//check if expected number of questions is found
if (questions.Count() != 1)
{
ViewBag.Error = string.Format("De opgevraagde vraag is niet gevonden!");
return PartialView("Index");
}
var question = questions.First();
//setup data for view
DocumentTypeEditorModel = GetDocumentType(DOCUMENT_TYPE_ALIAS);
//get the parent id
var parentHiveId = answerQuestionViewModel.UserId;
//set propertys
var nameProperty = SetProperty(NodeNameAttributeDefinition.AliasValue, new Dictionary<string, object> { { "Name", question.Name } });
var givenAnswerQuestion = SetProperty("givenAnswerQuestion", question.Id);
var givenAnswerUser = SetProperty("givenAnswerUser", answerQuestionViewModel.UserId);
var givenAnswerAnswer = SetProperty("givenAnswerAnswer", answerQuestionViewModel.Answer);
var properties = new HashSet<ContentProperty> { nameProperty, givenAnswerQuestion, givenAnswerUser, givenAnswerAnswer };
//create node
var contentNode = new ContentEditorModel
{
Id = new HiveId(Guid.NewGuid()),
DocumentTypeId = DocumentTypeEditorModel.Id,
DocumentTypeAlias = DocumentTypeEditorModel.Alias,
DocumentTypeName = DocumentTypeEditorModel.Name,
ParentId = parentHiveId,
Properties = new HashSet<ContentProperty>(properties)
};
var contentRepository = new List<ContentEditorModel> { contentNode };
//store node in DB
var mappedCollection = FrameworkContext.TypeMappers.Map<IEnumerable<ContentEditorModel>, IEnumerable<Revision<TypedEntity>>>(contentRepository).ToArray();
mappedCollection.ForEach(x => x.MetaData.StatusType = FixedStatusTypes.Published);
writer.Repositories.Revisions.AddOrUpdate(mappedCollection);
writer.Complete();
Finaly found something in the source code :
private ContentProperty SetTemplateProperty(string propertyAlias, HiveId propertyValue)
{
//NOTE: If the Umbraco API changes (an extra overload is added to new ContentProperty), this might break
var selectedTemplate = new ContentProperty((HiveId)Guid.NewGuid(),
DocumentTypeEditorModel.Properties.Where(x => x.Alias == SelectedTemplateAttributeDefinition.AliasValue).Single(),
propertyValue.IsNullValueOrEmpty() ? new Dictionary<string, object>() : new Dictionary<string, object> { { "TemplateId", propertyValue.ToString() } })
{
Name = SelectedTemplateAttributeDefinition.AliasValue,
Alias = SelectedTemplateAttributeDefinition.AliasValue,
TabId = DocumentTypeEditorModel.DefinedTabs.Where(x => x.Alias == FixedGroupDefinitions.GeneralGroup.Alias).Single().Id
};
return selectedTemplate;
}
is working on a reply...