Copied to clipboard

Flag this post as spam?

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


  • Daniel 2 posts 72 karma points
    Jan 27, 2016 @ 14:10
    Daniel
    0

    Allow members to create new content

    Hi All,

    I was hoping someone could guide me in the right direction, and by guide - I mean push me exactly where I need to go :)

    I've searched all over and not finding anything like this..

    I have a new project and I've implemented a social login, using the UmbracoIdentity (https://github.com/Shazwazza/UmbracoIdentity) package, this works well and I can successfully log in as a member using Google+ or Facebook.

    Now, I'm battling to find how to allow my members to submit content to the site - I want this to be public, whether it needs to be approved by me or not.

    I've seen similar functionality on the Dialogue Forum package, but I'm not sure how to only extract the "create new content" section from it. I also don't want to put down a complete forum or blogging package if it's not needed.

    I guess I'll need to use the Umbraco Content Service, but I also understand that there is a complication in that members are not allowed to publish content?

    Any advise would be appreciated!

  • Daniel 2 posts 72 karma points
    Jan 29, 2016 @ 08:17
    Daniel
    0

    I was really hoping someone has done something similar.

    In simple terms, is it possible to allow members to create new content from outside the admin area?

  • Niels Lynggaard 190 posts 548 karma points
    Jan 29, 2016 @ 09:13
    Niels Lynggaard
    1

    It should be fairly easy to do, just create a controller for it and use the content service to add new content..

    I'm doing exactly that on a project like this;

    I've created an API controller, that I'm consuming in the front-end by use of javascript to submit the values from the form.

    I've implemented a Model class so that everything is wrapped nicely, and then I'm just populating the new item from that model;

    Just disregard all the config-stuff, thats just some config-node that I have in the backend to allow editors to control emails, where the content is stored etc.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using Umbraco.Web.WebApi;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    using MyNameSpace.Umbraco.Classes;
    using Umbraco.Core.Logging;
    
    namespace MyNameSpace.Umbraco.Controllers.api
    {
        public class ContactFormController : UmbracoApiController
        {
           public ContactModel PostContact(ContactModel model) {
    
                ContactFormConfiguration conf = new ContactFormConfiguration();
                var homePage = Umbraco.TypedContent(model.PageId).AncestorsOrSelf(1).First(); 
                 var confignode = homePage.Children.Where(x => x.ContentType.Alias == "ConfigurationFolder").First();
                 if (confignode != null)
                 {
    
                     var contactconfig = confignode.Children.Where(x => x.ContentType.Alias == "ContactFormSettings").First();
                     if (contactconfig != null)
                     {
                         conf.ContactsSectionNode = contactconfig.GetPropertyValue<int>("contactsSectionNode");
                         conf.EmailMessage = contactconfig.GetPropertyValue<string>("emailMessage");
                         conf.EmailSubject = contactconfig.GetPropertyValue<string>("emailSubject");
                         conf.SendToEmail = contactconfig.GetPropertyValue<string>("sendToEmail");
                         //TODO: Add some default values
                     }
                 }
    
    
                var cs = Services.ContentService;
                int parentId = conf.ContactsSectionNode;//HAS to be here!  
                var content = cs.CreateContent(model.Name, parentId, "contact");
    
                content.Name = model.Name;
                content.SetValue("name", model.Name);
                content.SetValue("address", model.Address);
                content.SetValue("city", model.City);
                content.SetValue("organisation", model.Organisation);
                content.SetValue("numberOfUsers", model.NumberOfUsers);
                content.SetValue("email", model.Email);
                content.SetValue("offersent", false);
                content.SetValue("language", model.Language);
    
                cs.SaveAndPublishWithStatus(content);
    
                model.Id = content.Id;
    
                if (model.Id != null && model.Id != 0)
                {
                    try { 
                    //Send mail. 
                        string msg = conf.EmailMessage;
                        msg = msg.Replace("##NAVN##", model.Name);
                        msg = msg.Replace("##ADDRESS##", model.Address);
                        msg = msg.Replace("##CITY##", model.City);
                        msg = msg.Replace("##ORGANISATION##", model.Organisation);
                        msg = msg.Replace("##EMAIL##", model.Email);
                        msg = msg.Replace("##NUMBEROFUSERS##", model.NumberOfUsers);
                        msg = msg.Replace("##LANGUAGE##", model.Language);
                    umbraco.library.SendMail("[email protected]", conf.SendToEmail, "Ny entry fra contacts-form", msg, true);
                    }
                    catch (Exception e)
                    {
                        LogHelper.Debug<string>(" Send email for new entry in contacts failed: " + e.ToString());
                    }
                }
                return model;
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft