Exception when creating new content programmatically
Hi,
I'm trying to create a new content node from C#. I've looked around the forums and have found the following code, which apparently works for some people:
using Umbraco.Core.Services;
ContentService cs = new ContentService();
var content = cs.CreateContent("Name", 1234, "DocType");
cs.SaveAndPublishWithStatus(content);
However, this doesn't seem to work. The code crashes on the very first line (the ContentService line, not the using!) with "Object reference not set to an instance of an object."
Obviously that error isn't particularly helpful, but I'm not sure where to even begin troubleshooting this issue. It's crashing before the call to CreateContent so it's not due to an invalid parent ID or doctype, but rather is happening before anything "site-specific".
I'm using umbraco.library.GetPreValues() in a different part of the code, and that's working, so it's "talking" to the Umbraco instance successfully.
Any ideas? I'm using Umbraco 6.2.5 on Server 2008 R2.
var _contentService = ApplicationContext.Current.Services.ContentService;
var _contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
They're both giving a NullReferenceException (Object reference not set to an instance of an object). Am I possibly missing a using? I have these:
using umbraco.cms.businesslogic.web;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.WebApi;
Umbraco.Core is indeed the only one that's "lit up" in Visual Studio, so that makes sense.
It's an external DLL. I've put the "umbracoDbDSN" connection string in app.config and that's sufficient to get prevalues from a different part of the code, but do I possibly need something else to access the ContentService?
If the Dll is then copied into Umbraco's bin folder then you should be ok,
As a Genral rule, I would add UmbracoCms.Core as a nuget package to your dll project. to make sure you are getting the right references in the project
But if you are calling the code in the dll from outside of the Umbraco Application then there is quite a lot you need to do to get the umbraco invoked before you can do any content stuff:
There are a couple examples of getting umbraco up for a command line
if you want to do some quick and dirty mass content creation quickly i would put your code inside a UmbracoApiController (for security use UmbracoAuthorizedController) and then call the URL in a browser to do all the work inside umbraco
Apologies; it seems that I wasn't as forthcoming with information as I should've been - that's what happens when you're rushing to post before dashing out of the office! :)
I already have the NuGet package so I should have all the necessary DLLs. Thanks for the links to that documentation. I'll post back with success/failure once I've taken a look.
Exception when creating new content programmatically
Hi,
I'm trying to create a new content node from C#. I've looked around the forums and have found the following code, which apparently works for some people:
However, this doesn't seem to work. The code crashes on the very first line (the ContentService line, not the using!) with "Object reference not set to an instance of an object."
Obviously that error isn't particularly helpful, but I'm not sure where to even begin troubleshooting this issue. It's crashing before the call to CreateContent so it's not due to an invalid parent ID or doctype, but rather is happening before anything "site-specific".
I'm using umbraco.library.GetPreValues() in a different part of the code, and that's working, so it's "talking" to the Umbraco instance successfully.
Any ideas? I'm using Umbraco 6.2.5 on Server 2008 R2.
[Edited to make slightly clearer]
One would not typically create a new content service. IIRC, getting the content service usually works something like this:
Thanks, but no luck. Same error.
For what it's worth, the "sample" that I was using was here, which apparently worked for one person ... but not me :)
Hi,
The following will create content : Assuming 1054, is a valid node in your content tree, and you have a Alias called ContentPage
if this isn't working i would check the node number and that the doctype Alias is what you think it should be
you can check the node id by calling get content
and you can see if you are getting the right content type using the ContentType service
also to double check with these you could create content by passing the node
Kevin
Neither of these lines are working:
They're both giving a NullReferenceException (Object reference not set to an instance of an object). Am I possibly missing a using? I have these:
Any other ideas?
Hi
you should only need
using Umbraco.Core
for the code to work.where are you running the code from ? (Partial/Macro, App_Code or an external DLL?)
I just pasted the example above directly from a class in App_Code.
Umbraco.Core is indeed the only one that's "lit up" in Visual Studio, so that makes sense.
It's an external DLL. I've put the "umbracoDbDSN" connection string in app.config and that's sufficient to get prevalues from a different part of the code, but do I possibly need something else to access the ContentService?
Hi
If the Dll is then copied into Umbraco's bin folder then you should be ok,
As a Genral rule, I would add UmbracoCms.Core as a nuget package to your dll project. to make sure you are getting the right references in the project
But if you are calling the code in the dll from outside of the Umbraco Application then there is quite a lot you need to do to get the umbraco invoked before you can do any content stuff:
There are a couple examples of getting umbraco up for a command line
https://github.com/sitereactor/umbraco-console-example (not sure if this is 100% upto date)
And Chauffeur does it a quite clever way - that lets it run from the bin folder. (follow the logic from here https://github.com/aaronpowell/Chauffeur/blob/master/Chauffeur.Runner/Program.cs)
if you want to do some quick and dirty mass content creation quickly i would put your code inside a UmbracoApiController (for security use UmbracoAuthorizedController) and then call the URL in a browser to do all the work inside umbraco
Apologies; it seems that I wasn't as forthcoming with information as I should've been - that's what happens when you're rushing to post before dashing out of the office! :)
I already have the NuGet package so I should have all the necessary DLLs. Thanks for the links to that documentation. I'll post back with success/failure once I've taken a look.
OK, I've been through the console example and now have everything working :)
Thanks again for the help!
I keep getting this error when trying to save content ....
Exception Details: System.InvalidOperationException: Sequence contains no elements
Here's my code:
First of all, what is
post
? Is it supposed to becontent
?I've found my working code and - assuming that
post
iscontent
- I've set the properties a bit differently:Does that help at all?
is working on a reply...