Copied to clipboard

Flag this post as spam?

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


  • Connie DeCinko 931 posts 1160 karma points
    Aug 04, 2016 @ 15:37
    Connie DeCinko
    0

    How to create content via C# Usercontrol

    Sorry if this has been asked a hundred times, but while I dig through several years old posts, I am hoping someone can jump in with the answer.

    We used to use the Doc2Form package in Umbraco 4.x which would allow a visitor to the website to fill out a form and have the data automatically create a content node. Doc2Form was never updated for Umbraco 6.

    I currently have a c# .Net usercontrol that is a simple suggestion box form. It gathers a few text fields, saves them to a database and sends and email. I now need to extend it to automatically create an Umbraco document. I know it can be done, but what is the current recommended way? Can I do this via a usercontrol or must I change it to something else?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 04, 2016 @ 15:59
    Alex Skrypnyk
    0

    Hi Connie,

    You can create content with ContentService since 6 version was introduced.

    Read more about ContentService - https://our.umbraco.org/documentation/reference/management/services/contentservice

    Example:

    private void createComment(string userName, string comment, int parentId)
        {
            int userId = 0;
            ContentService cs = new ContentService();
            var content = cs.CreateContent(DateTime.Today.ToString("yy/MM/dd"), parentId, "BlogComments", userId);
            content.SetValue("userName", userName);
            content.SetValue("comment", comment);
            cs.SaveAndPublishWithStatus(content);
        }
    

    Thanks,

    Alex

  • Connie DeCinko 931 posts 1160 karma points
    Aug 04, 2016 @ 16:52
    Connie DeCinko
    0

    Thank you! Kept finding docs for older or newer versions. I got it working well pretty easily. Now just have to deal with a couple issues.

    How to specify the parentId without having to hard code the value. How to get the userId for the currently logged in user.

    Time to Google!

Please Sign in or register to post replies

Write your reply to:

Draft