Tuesday 9 July 2013

Using the Umbraco 6 ContentService for what you actually use.

Lot of crappy examples there on the web, hope this doesnt add to them :P

If anything is unclear let me know and I will try to write it in a more clear manner.

You will need to include this in your code behind

using Umbraco.Core.Services;

 // Get the Umbraco Content Service
var contentService = new ContentService();
var user = contentService.CreateContent(
                    "Barry Chuckle", // Node Name - what I want to call the new child node
                    1084, // Parent Node we want to add to
                    "MyUsers", // The alias of the Document Type
                    0); // Umbraco User ID this will be created by, default 0

// Sample Values
user.SetValue("fullname", "Noddy Holder");
user.SetValue("email", "hello@world.com");

contentService.SaveAndPublish(user);

This should allow you create new items in the CMS from the front end of the site, its useful for contact forms, registrations etc.

Note: Also you can use uploads on the front end to hook into this as well, I would use something along the lines to get the media path.  If you use the above in conjuction with the following you can also use the upload data type.

var saveLocation = MapPath("/media/");

if (!string.IsNullOrEmpty(publicLiabilityInsuranceUpload.FileName))
{
myDocument = Path.GetFileName(myDocument Upload.PostedFile.FileName);
myDocumentUpload.PostedFile.SaveAs(saveLocation + myDocument);
}

user.SetValue("myDocument", myDocument);

Hope this clears up a few matters.

ADDITIONAL:
The multi select dropdown list should be a comma separated value, but I need to confirm 100% how this works over the next day or two when I get chance to run some tests :)

No comments: