Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Erwin 22 posts 43 karma points
    Mar 23, 2012 @ 13:18
    Erwin
    0

    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);

                    }

                }

    //setupdatabase acces to content
                var groupUnit = Hive.GetWriter<IContentStore>().Create();
                //get givenanswer
                var hiveid = new HiveId(new Guid(formCollection.Get("Id.Value")));
                var givenanswers = groupUnit.Repositories.Get<Content>(false,hiveid);
                if (givenanswers.Count() != 1)
                {
                    throw new Exception("Unexpected number of answers found found");
                }
                var givenanswerDb = givenanswers.First();
                //set the given answer
                givenanswerDb["givenAnswerAnswer"] = new HiveId(formCollection.Get("[givenAnswerAnswer]"));
                //set het template
                givenanswerDb.CurrentTemplate = template;
                
                //store the givenanswer
                groupUnit.Repositories.AddOrUpdate(givenanswerDb);
                groupUnit.Complete();
    any help is welcome

     

  • Erwin 22 posts 43 karma points
    Mar 27, 2012 @ 10:32
    Erwin
    0

    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();

  • Erwin 22 posts 43 karma points
    Mar 28, 2012 @ 10:34
    Erwin
    0

    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;

            }

Please Sign in or register to post replies

Write your reply to:

Draft