Copied to clipboard

Flag this post as spam?

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


  • mmmoustache 50 posts 79 karma points
    May 14, 2012 @ 12:14
    mmmoustache
    0

    Creating a new node using Razor

    Hi Everyone,

    I'm working on a booking system so that once a particular form has been submitted on my site, it also creates a new node in the back-end for reference. Is it possible to do this only using razor? If not, how can I create this functionality?

    I'm currently working with the following code that was suggested to me from the Umbraco documentation (by adding it within the codeblock that runs if send is successful), but my razor script errors:

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;

    DocumentType dt = DocumentType.GetByAlias("Textpage");
    User author = User.GetUser(0);
    Document doc = Document.MakeNew("My new document", dt, author, 1018);

    I'm using Umbraco v4.7.1.1 and get the following error: "The type or namespace name 'DocumentType' could not be found (are you missing a using directive or an assembly reference?)".

    If I add @'s to the namespaces I get the error: 'System.Security.Principal.IPrincipal' does not contain a definition for 'GetUser' and no extension method 'GetUser' accepting a first argument of type 'System.Security.Principal.IPrincipal' could be found (are you missing a using directive or an assembly reference?).

    Does anyone have any idea as to where I'm going wrong?

    Kind Regards,
    mmmoustache

  • mmmoustache 50 posts 79 karma points
    May 14, 2012 @ 17:14
    mmmoustache
    0

    It might also be worth mentioning that following the same api documentation, I can publish and unpublish nodes using these snippets:

    Unpublishing node
    @using umbraco.cms.businesslogic.web;

    @{
        Document doc new Document(1822);
        doc.UnPublish();
    }

    Publishing node
    @using umbraco.cms.businesslogic.web;

    @{
      Document new Document(1822);
      d.Publish(new umbraco.BusinessLogic.User(0));
      umbraco.library.UpdateDocumentCache(d.Id);
    }

    However, for whatever reason, the snippet on creating a new node does not work.

  • Amir Khan 1284 posts 2741 karma points
    Oct 03, 2014 @ 19:44
    Amir Khan
    0

    Incase anyone else has this issue, here'w how to create a new document with razor.

    Taken from: https://gist.github.com/joeriks/877600

     

    @using umbraco.cms.businesslogic.web;

    @{


    var dt = DocumentType.GetByAlias("Bar");
    @* Make sure its a valid document type alias *@
    if (dt != null)
    {
    var author = umbraco.BusinessLogic.User.GetUser(0);

    var doc = Document.MakeNew("Comment", dt, author, 1124);

    @* Tell umbraco to publish the document *@
    doc.Publish(author);
    umbraco.library.UpdateDocumentCache(doc.Id);
    }
    }

     

     

Please Sign in or register to post replies

Write your reply to:

Draft