Copied to clipboard

Flag this post as spam?

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


  • Gert Peter Brigsted 30 posts 61 karma points
    Apr 03, 2011 @ 21:05
    Gert Peter Brigsted
    0

    Document.MakeNew exception

    Hi! :)

    I'm trying to develop a usercontrol that creates a new document. But as soon as I call Document.MakeNew(), an exception is thrown:

    [KeyNotFoundException: The given key was not present in the dictionary.]
       umbraco.cms.businesslogic.datatype.controls.Factory.GetNewObject(Guid DataEditorId) +325
       umbraco.cms.businesslogic.property.Property.MakeNew(PropertyType pt, Content c, Guid versionId) +707
       umbraco.cms.businesslogic.Content.createNewVersion() +518
       umbraco.cms.businesslogic.Content.CreateContent(ContentType ct) +260
       umbraco.cms.businesslogic.web.Document.MakeNew(String Name, DocumentType dct, User u, Int32 ParentId) +570
       ScrapUpload.ScrapSave_Click(Object sender, EventArgs e) in usercontrols\ScrapUpload.ascx.cs:51
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691

    I have made sure that all of the parameters to the method are correct. What am I doing wrong?

    Umbraco Version: 4.6.1

  • Yannick Smits 321 posts 718 karma points
    Apr 03, 2011 @ 22:08
    Yannick Smits
    0

    what's your code look like?

  • Gert Peter Brigsted 30 posts 61 karma points
    Apr 04, 2011 @ 22:08
    Gert Peter Brigsted
    0

    Sorry, the code is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using umbraco.cms.businesslogic.web;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.property;
    public partial class ScrapUpload : System.Web.UI.UserControl
    {
        public string ProduktTypeAlias { get; set; }
        public string ProduktAlias { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DocumentType dt = DocumentType.GetByAlias(ProduktTypeAlias);
                IEnumerable<Document> docs = Document.GetDocumentsOfDocumentType(dt.Id)
                    .OrderBy(doc => doc.sortOrder)
                    .Where(doc => doc.Published);
                var lowestLevel = docs.Min(doc => doc.Level);
                foreach (Document parentDoc in docs.Where(doc => doc.Level == lowestLevel))
                {
                    ScrapProductType.Items.Add(new ListItem(parentDoc.Text, parentDoc.Id.ToString()));
                    foreach (Document childDoc in docs.Where(doc => doc.ParentId == parentDoc.Id))
                    {
                        ScrapProductType.Items.Add(new ListItem("-- " + childDoc.Text, childDoc.Id.ToString()));
                    }
                }
            }
        }
        protected void ScrapSave_Click(Object sender, EventArgs e)
        {
            if (Member.IsLoggedOn())
            {
                int category = int.Parse(ScrapProductType.SelectedValue);
                DocumentType dt = DocumentType.GetByAlias(ProduktAlias);
                User user = User.GetUser(0);
                Member member = Member.GetCurrentMember();
                Log.Text = member.ToString();
                Document document = Document.MakeNew(ScrapName.Text, dt, user, category);
                Property navn = document.getProperty("navn");
                navn.Value = ScrapName.Text;
                Property beskrivelse = document.getProperty("beskrivelse");
                beskrivelse.Value = ScrapDescription.Text;
                Property indsendtAf = document.getProperty("indsendtAf");
                indsendtAf.Value = member;
                document.Save();
            }
        }
    }

    And the ascx is as follows:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ScrapUpload.ascx.cs" Inherits="ScrapUpload" %>
    <ul>
        <li>
            <asp:Label AssociatedControlID="ScrapName" runat="server" Text="Navn:" />
            <asp:TextBox ID="ScrapName" runat="server" />
        </li>
        <li>
            <asp:Label AssociatedControlID="ScrapDescription" runat="server" Text="Beskrivelse:" />
            <asp:TextBox ID="ScrapDescription" runat="server" />
        </li>
        <li>
            <asp:Label AssociatedControlID="ScrapProductType" runat="server" Text="Kategori:" />
            <asp:DropDownList ID="ScrapProductType" runat="server" />
        </li>
        <li>
            <asp:Label AssociatedControlID="ScrapImage" runat="server" Text="Billed:" />
            <asp:FileUpload ID="ScrapImage" runat="server" />
        </li>
    </ul>
    <asp:Button ID="ScrapSave" runat="server" OnClick="ScrapSave_Click" Text="Gem" />
    <asp:TextBox ID="Log" runat="server" TextMode="MultiLine"></asp:TextBox>

  • Jesper Hauge 298 posts 487 karma points c-trib
    Apr 05, 2011 @ 00:25
    Jesper Hauge
    0

    Your code looks legit (the new document will not be published using this code, but I gather you're aware of this)

    The error makes me think about the properties on the documenttype and the datatypes connected to these properties. Did you check that all the properties on the documenttype are correct? Are you able to create a new document of this type using the Umbraco backend?

    Regards
    Jesper Hauge

  • Gert Peter Brigsted 30 posts 61 karma points
    Apr 05, 2011 @ 21:09
    Gert Peter Brigsted
    0

    Well, there are more properties on the document type, but as long as I only save the document and not publish I should be home free, right? Also, it is possible for me to create a document of this type in the backend.

  • Jesper Hauge 298 posts 487 karma points c-trib
    Apr 06, 2011 @ 00:03
    Jesper Hauge
    0

    I don't think there's any difference regarding properties whether you publish the document or not.

    Are one of the properties on your document type some kind of dropdown, so when you're saving the document in the backend, theres a default value provided for the property. Maybe that could explain why you can't save from code where you're not setting a value for the property.

    Regards
    Jeper Hauge

  • Gert Peter Brigsted 30 posts 61 karma points
    Apr 06, 2011 @ 18:51
    Gert Peter Brigsted
    0

    I solved my problem, and I'll post my solution, although I don't like how it worked out.

    First of all, I switched the site from Integrated Mode to Classic Mode, because the admin was acting up (when i logged in to the frontend, none of the admin sections showed any child nodes).

    Then i deleted all document properties on that document type, and recreated the exact same properties. The property types are as follows:

    • Textstring
    • Textbox Multiple
    • Member Picker
    • Upload
    All properties are mandatory. Saving works now. I haven't tried changing the site back to Integrated Mode, but that's the only thing that has actually changed.

Please Sign in or register to post replies

Write your reply to:

Draft