Copied to clipboard

Flag this post as spam?

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


  • Yohan 9 posts 32 karma points
    Jan 31, 2013 @ 21:01
    Yohan
    0

    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:

    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?

     

  • Yohan 9 posts 32 karma points
    Feb 01, 2013 @ 19:09
    Yohan
    1

    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?

  • Yohan 9 posts 32 karma points
    Feb 05, 2013 @ 18:34
    Yohan
    0

    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.

  • Pinal Bhatt 298 posts 390 karma points
    Feb 11, 2013 @ 23:06
    Pinal Bhatt
    0

    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.

  • Pinal Bhatt 298 posts 390 karma points
    Feb 12, 2013 @ 02:29
    Pinal Bhatt
    0

    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.

  • Damian Green 452 posts 1433 karma points
    Feb 14, 2013 @ 11:25
    Damian Green
    0

    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:

     System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)+109
       
    Umbraco.Core.Persistence.Database.CommonConstruct()+60
       
    Umbraco.Core.Persistence.UmbracoDatabase..ctor(String connectionStringName)+67
       
    Umbraco.Core.Persistence.DefaultDatabaseFactory.CreateDatabase()+426
       
    Umbraco.Core.Persistence.UnitOfWork.PetaPocoUnitOfWorkProvider.GetUnitOfWork()+26

    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!

     

  • Yohan 9 posts 32 karma points
    Feb 14, 2013 @ 22:53
    Yohan
    0

    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.

  • Pinal Bhatt 298 posts 390 karma points
    Feb 14, 2013 @ 23:03
    Pinal Bhatt
    0

    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.

     


  • Damian Green 452 posts 1433 karma points
    Feb 15, 2013 @ 10:48
    Damian Green
    0

    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.

     

     

  • Ivan Doroshenko 3 posts 23 karma points
    Mar 01, 2013 @ 13:10
    Ivan Doroshenko
    0

    Hi,

    any solution? I have the same problem in creating of a console application.

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Mar 01, 2013 @ 15:11
    Morten Christensen
    3

    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

  • Pinal Bhatt 298 posts 390 karma points
    Mar 01, 2013 @ 19:49
    Pinal Bhatt
    0

    Hey... gr8 example. Thanks a lot. 

  • Damian Green 452 posts 1433 karma points
    Mar 04, 2013 @ 10:25
    Damian Green
    0

    Thanks Morten.  Will have a look at this and let you know if i find any issues.

     

  • quachnguyen 12 posts 32 karma points
    Apr 15, 2013 @ 12:14
    quachnguyen
    0

    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



    Please correct me, If I was wrong.

    P/S: here is my connection string

      <connectionStrings>
        <remove name="umbracoDbDSN" />
        <add name="umbracoDbDSN" connectionString="server=localhost\sql2008r2;database=databasename;Connect Timeout=200;user id=sa;password=secret;" />
        <!-- Important: If you're upgrading Umbraco, do not clear the connection string / provider name during your web.config merge. -->
      </connectionStrings> 
  • Yakov Lebski 539 posts 2101 karma points
    Apr 29, 2013 @ 10:32
    Yakov Lebski
    100

    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();

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Apr 29, 2013 @ 10:46
    Morten Christensen
    0

    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

  • Yakov Lebski 539 posts 2101 karma points
    Apr 29, 2013 @ 12:01
    Yakov Lebski
    0

    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" />

  • Paul Stoker 39 posts 72 karma points c-trib
    Jan 31, 2014 @ 12:07
    Paul Stoker
    0

    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,

     

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Jan 31, 2014 @ 22:05
    Morten Christensen
    0

    @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

  • Barbacan 29 posts 79 karma points
    Feb 06, 2014 @ 18:03
    Barbacan
    0

    Hi guys, once I created my content, how can I get its Id?

Please Sign in or register to post replies

Write your reply to:

Draft