Copied to clipboard

Flag this post as spam?

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


  • Jan Højriis Dragsbæk 12 posts 63 karma points
    Apr 05, 2013 @ 12:56
    Jan Højriis Dragsbæk
    0

    Using ContentService to create multiple nodes via Base

    I am creating a nodeimporter based on XML data and I got my data in variables, but when I try to create the nodes, I get a nullreferenceexception from umbraco.

    I am calling my method

    AddOrUpdateProduct( sku, itemName, b2bprice, b2cprice, actualStock, GetMaterialID( material ) );

    and the methods look like this:

    protected int GetMaterialID( string materialName ) {
    
          var containerID = 1591;
    
          IContent material;
          var materialSearch = uQuery.GetNode( containerID ).GetDescendantNodes().SingleOrDefault( n => n.NodeTypeAlias == "ProductMaterial" && n.Name == materialName );
          if ( materialSearch != null ) {
            material = _contentService.GetById( materialSearch.Id );
          } else {
            material = _contentService.CreateContent( materialName, containerID, "ProductMaterial" );
          }
    
          _contentService.SaveAndPublish( material );
          return material.Id;
        }
    
        protected void AddOrUpdateProduct( string sku, string productName, decimal B2BPrice, decimal B2CPrice, int stock, int materialID ) {
    
          IContent product;
          var categoryId = 1615;
    
          var currentProduct = uQuery.GetNode(categoryId).GetDescendantNodes().SingleOrDefault( n => n.NodeTypeAlias == "Product" && n.GetProperty( "sku" ).Value == sku );
          if ( currentProduct != null ) {
            product = _contentService.GetById( currentProduct.Id );
          } else {
    
            product = _contentService.CreateContent( productName, categoryId, "Product" );
          }
    
          // update properties
          product.SetValue( "sku", sku );
          product.SetValue( "priceDKK", B2CPrice.ToString(CultureInfo.InvariantCulture) );
          product.SetValue( "materials", materialID.ToString() );
    
          _contentService.SaveAndPublish( product );
        }

    The script runs and creates the first node (the one from material) just fine, but the second throws the exception

    Exception has been thrown by the target of an invocation.
    STACKTRACE:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at Umbraco.Web.BaseRest.RestExtensionMethodInfo.Invoke(String[] parameters)

    INNEREXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
       at Umbraco.Core.Models.PropertyType.IsPropertyValueValid(Object value)
       at Umbraco.Core.Models.ContentBase.b__13(Property property)
       at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
       at Umbraco.Core.Models.ContentBase.IsValid()
       at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Boolean omitCacheRefresh, Int32 userId, Boolean raiseEvents)
       at Umbraco.Core.Services.ContentService.SaveAndPublish(IContent content, Int32 userId, Boolean raiseEvents)
       at Muubs.Website.C5.ProductReader.AddOrUpdateProduct(String sku, String productName, Decimal B2BPrice, Decimal B2CPrice, Int32 stock, Int32 materialID) in d:\projects\muubs-website-clone\_Source\Muubs.Website\C5\ProductReader.cs:line 131
       at Muubs.Website.C5.ProductReader.ImportToDB() in d:\projects\muubs-website-clone\_Source\Muubs.Website\C5\ProductReader.cs:line 87
       at Muubs.Website.Base.ImportDataz() in d:\projects\muubs-website-clone\_Source\Muubs.Website\Base.cs:line 18

    How can I fix this?

Please Sign in or register to post replies

Write your reply to:

Draft