Using v6 API ContentService in external application
I have v6 RC installed (didn't expect it to go full release so quickly after RC!) and I'm trying to add content nodes using the new v6 API. I read the info here and was able to do so on an Umbraco served page:
Now I'm trying to do it from an external application, in this case a console app. In an Umbraco page you can just do this to get the ContentService:
var contentService = ApplicationContext.Current.Services.ContentService;
But that doesn't work with an external program so I'm trying to create a ContentService object doing something like this:
var contentService = new ContentService();
That doesn't work either since I'm not telling the application where the Umbraco instance is or how to connect to it. I'm not telling it to do that because I don't know how :)
So how do I connect to the Umbraco instance from an external application so I can use the v6 API from a console app?
Making progress but getting a different error now. If nobody responds then this might just be me talking to myself while working through this problem, but hopefully it will help somebody else too.
I was able to get the ContentService() part working by copying the umbracoDbDSN connection string from the Umbraco install to the console app's app.config. Here's all the code I have so far (sorry, the "code" formatting dropdown option isn't available to me right now for some reason):
var contentService = new ContentService(); var product = contentService.CreateContent( "Product name goes here", 1056, "Product", 0); contentService.SaveAndPublish(product);
I'm getting this error on the CreateContent() call:
ArgumentNullException was unhandled You must set the singleton 'Umbraco.Core.Persistence.SqlSyntax.SyntaxConfig' to use an sql syntax provider. Parameter name: SqlSyntaxProvider
Anybody? Seems like this should be a simple thing to do if I could just get over the hump of wiring up everything properly up front. Is the v6 API just too new? It would be nice if one of the devs would chime in since there's no documentation on how to do this and the blog post I referenced above only shows how to do it from inside Umbraco instead of externally.
most likely error is due to last parameter of CreateContent method. As you are passing user id as 0(zero) it will try to find current user and you dont have current user in you console app. Try putting actual user id. Not sure this is the reason but hope it helps.
nops like some other issue. I tried to read a node using new API but getting same SqlSyntax.SyntaxConfig error on LINE # 2
var s = new ContentService();
var node = s.GetById(1354);
string name = node.Name;
either we are missing something or its a bug or APIs are not fully ready. with lack of documentation we cant say anybody. somebody from HQ only have to respond to this.
Had a go at this myself and hit the same issues. Looks like you have to spin up a unitofwork and repository to get this fired up but not done that before so not sure what the solution is there.
Also posted this on twitter last night and asked Niels as he claimed in his post about v6 that this could be done but still no resolution so none the wiser. Looks like im going to have to write my app to run from a macroscript instead for the time being as up against it time wise.
Will keep looknig for a way to do this though.
These are thinks ive found so far that may be related:
I figured I wouldn't be able to use the empty constructor in an external app, but there's no good info out there on how to instantiate ContentService otherwise. Digging in the source code there are a number of different constructors for ContentService:
So which one do we use in an external application and how? I tried to dig deeper and look at the RepositoryFactory class but found nothing promising there, and the other constructors require even more difficult parameters to decipher.
We're in total agreement--per the blog post it's got to be something simple, but unless the devs chime in we might never know. You'd think they'd step up their forum presence at least for a few weeks after such a big new feature such as a new API. As it is it's the blind leading the blind in here :) It's fine to expect the community to eventually build up a knowledge base for the API, but if we don't even know how to get started that's not exactly going to work, is it.
Its almost 2 weeks now that U6 is out but I dont see any documentation coming out and when this release is major for APIs, something is really expected. Niels boldy wrote in his blog post and i can see comments asking for more details but no response. Even i have asked HQ team via tweeter to look into this but no response.
I exchanged some tweets with Morten yesterday and he says it is possible to do this but takes a bit more work. He said a would do a bit of a write up when he gets a moment. I linked in this post so he may post it here - if not i'll post a link when I get one.
I hope this sample implementation provides some insight as to how you can use the ContentService (and other services) in something like a Console Application.
The connectionstring is missing the ProviderName, which is used to determine which SqlSyntaxProvider to use.
If you have an installation somewhere else you can see what it should be, but usually its: "System.Data.SqlClient" for Sql Server "System.Data.SqlServerCe.4.0" for Sql CE "MySql.Data.MySqlClient" for MySql.
@Paul I think you would be much better off by creating some kind of REST endpoint in umbraco that you can call from your asp.net MVC application. If you are already operating in a web context then why not continue to use the web for interacting with umbraco?
The ContentService has a bunch of dependencies, so you'd still have to start up an application with a boot manager. This is mostly because of various Resolvers which are used in various places of the codebase.
I haven't experimented with a "standalone" ContentService, but I would assume a lot of dependencies needs to be wired up - and that to an extent that would be very close to starting the entire application.
Using v6 API ContentService in external application
I have v6 RC installed (didn't expect it to go full release so quickly after RC!) and I'm trying to add content nodes using the new v6 API. I read the info here and was able to do so on an Umbraco served page:
http://umbraco.com/follow-us/blog-archive/2013/1/22/introducing-contentservice-aka-the-v6-api.aspx
Now I'm trying to do it from an external application, in this case a console app. In an Umbraco page you can just do this to get the ContentService:
But that doesn't work with an external program so I'm trying to create a ContentService object doing something like this:
That doesn't work either since I'm not telling the application where the Umbraco instance is or how to connect to it. I'm not telling it to do that because I don't know how :)
So how do I connect to the Umbraco instance from an external application so I can use the v6 API from a console app?
Making progress but getting a different error now. If nobody responds then this might just be me talking to myself while working through this problem, but hopefully it will help somebody else too.
I was able to get the ContentService() part working by copying the umbracoDbDSN connection string from the Umbraco install to the console app's app.config. Here's all the code I have so far (sorry, the "code" formatting dropdown option isn't available to me right now for some reason):
var contentService = new ContentService();
var product = contentService.CreateContent(
"Product name goes here",
1056,
"Product",
0);
contentService.SaveAndPublish(product);
I'm getting this error on the CreateContent() call:
ArgumentNullException was unhandled
You must set the singleton 'Umbraco.Core.Persistence.SqlSyntax.SyntaxConfig' to use an sql syntax provider.
Parameter name: SqlSyntaxProvider
So what now?
Anybody? Seems like this should be a simple thing to do if I could just get over the hump of wiring up everything properly up front. Is the v6 API just too new? It would be nice if one of the devs would chime in since there's no documentation on how to do this and the blog post I referenced above only shows how to do it from inside Umbraco instead of externally.
most likely error is due to last parameter of CreateContent method. As you are passing user id as 0(zero) it will try to find current user and you dont have current user in you console app. Try putting actual user id. Not sure this is the reason but hope it helps.
nops like some other issue. I tried to read a node using new API but getting same SqlSyntax.SyntaxConfig error on LINE # 2
either we are missing something or its a bug or APIs are not fully ready. with lack of documentation we cant say anybody. somebody from HQ only have to respond to this.
Had a go at this myself and hit the same issues. Looks like you have to spin up a unitofwork and repository to get this fired up but not done that before so not sure what the solution is there.
Also posted this on twitter last night and asked Niels as he claimed in his post about v6 that this could be done but still no resolution so none the wiser. Looks like im going to have to write my app to run from a macroscript instead for the time being as up against it time wise.
Will keep looknig for a way to do this though.
These are thinks ive found so far that may be related:
http://jamesheppinstall.wordpress.com/2012/06/16/petapoco-quick-and-easy-unit-of-work/
And a procoess umbraco seems to go through when setting this up:
And some stuff about the respoitiories from Morten (search for PetaPocoUnitOfWorkProvider) :
http://our.umbraco.org/mobile?mode=topic&id=34773
Got to be an easy way of doing it!
I figured I wouldn't be able to use the empty constructor in an external app, but there's no good info out there on how to instantiate ContentService otherwise. Digging in the source code there are a number of different constructors for ContentService:
ContentService()
ContentService(RepositoryFactory repositoryFactory)
ContentService(IDatabaseUnitOfWorkProvider provider)
ContentService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory)
ContentService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, IPublishingStrategy publishingStrategy)
So which one do we use in an external application and how? I tried to dig deeper and look at the RepositoryFactory class but found nothing promising there, and the other constructors require even more difficult parameters to decipher.
We're in total agreement--per the blog post it's got to be something simple, but unless the devs chime in we might never know. You'd think they'd step up their forum presence at least for a few weeks after such a big new feature such as a new API. As it is it's the blind leading the blind in here :) It's fine to expect the community to eventually build up a knowledge base for the API, but if we don't even know how to get started that's not exactly going to work, is it.
yes you are right Yohan.
Its almost 2 weeks now that U6 is out but I dont see any documentation coming out and when this release is major for APIs, something is really expected. Niels boldy wrote in his blog post and i can see comments asking for more details but no response. Even i have asked HQ team via tweeter to look into this but no response.
I exchanged some tweets with Morten yesterday and he says it is possible to do this but takes a bit more work. He said a would do a bit of a write up when he gets a moment. I linked in this post so he may post it here - if not i'll post a link when I get one.
Hi,
any solution? I have the same problem in creating of a console application.
Hi All,
Sorry it took so long to reply, but only had time to look at creating a console app using the ContentService today and here is the result:
http://github.com/sitereactor/umbraco-console-example/
I hope this sample implementation provides some insight as to how you can use the ContentService (and other services) in something like a Console Application.
As for documentation for v6 we have added some docs here: http://our.umbraco.org/documentation/Reference/Management-v6/ but more is still to come.
- Morten
Hey... gr8 example. Thanks a lot.
Thanks Morten. Will have a look at this and let you know if i find any issues.
Hi Morten Christensen
I get your code example, everything is Ok, but When I did try to list all node (press L key), I got an error message
It's cause because SqlSyntaxProvider is not initialized, I don't know why.
My workaroud is
var service = new Umbraco.Core.Services.ContentService();
SqlSyntaxContext.SqlSyntaxProvider = new MySqlSyntaxProvider();
The connectionstring is missing the ProviderName, which is used to determine which SqlSyntaxProvider to use.
If you have an installation somewhere else you can see what it should be, but usually its:
"System.Data.SqlClient" for Sql Server
"System.Data.SqlServerCe.4.0" for Sql CE
"MySql.Data.MySqlClient" for MySql.
Hope this helps.
Morten
My connection string contains provider attribute.
<add name="MySql" connectionString="Server=localhost;Port=3306;Database=OTHER_DB_NAME;Uid=root;Pwd=pwd;use procedure bodies=false;charset=utf8" providerName="MySql.Data.MySqlClient" />
<remove name="umbracoDbDSN" />
<add name="umbracoDbDSN" connectionString="Server=127.0.0.1; Database=UMBRACO_DB;Uid=root;Pwd=pwd" providerName="MySql.Data.MySqlClient" />
Hi Morten,
Thanks for posting an example of how to access the API from a console application.
I'm currently working on a POC to determine whether Umbraco content can be pulled into a separate Asp.Net MVC application.
Looking at your example I see you've initialised the umbraco application base but this seems quite heavy handed.
I'm wondering if you ever discoved a way of just initialising the component you needed for a simple ContentService call?
Thanks,
@Paul I think you would be much better off by creating some kind of REST endpoint in umbraco that you can call from your asp.net MVC application. If you are already operating in a web context then why not continue to use the web for interacting with umbraco?
The ContentService has a bunch of dependencies, so you'd still have to start up an application with a boot manager. This is mostly because of various Resolvers which are used in various places of the codebase.
I haven't experimented with a "standalone" ContentService, but I would assume a lot of dependencies needs to be wired up - and that to an extent that would be very close to starting the entire application.
- Morten
Hi guys, once I created my content, how can I get its Id?
is working on a reply...