Thought I would post the solution I have so far in case it would help anyone else trying to insert/save new products from an outside source or application. This solution still doesn't save text input (still working on that), but this code accepts post parameters (information about the custom expression created in an online designer) and saves this information into a new table we created for the custom designs, plus adds it to the product table, associates the product with the appropriate category and saves a PDF of the design (created by the online designer) to the Product File Downloads.
<%@ Page Language="VB" validateRequest="false"%>
<%@ Import Namespace="BVSoftware.BVC5.Core" %>
<%@ Import Namespace="System.Collections.ObjectModel" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Create a string to contain the paramaters'
' information.
Dim result As New BVSoftware.BVC5.Core.Services.WS4.WS4Response
Dim dID As String = Request.Form("id")
Dim dTitle As String = Request.Form("title")
Dim dXML As String = Request.Form("value")
Dim dType As String = Request.Form("type")
Dim dSKU As String = Request.Form("id")
Dim sConn As String = ConfigurationManager.ConnectionStrings("Bvc5Database").ConnectionString
Dim conn As SqlConnection
Dim comm As SqlCommand
Dim currCustomerID As String
currCustomerID = BVSoftware.BVC5.Core.SessionManager.GetCurrentUserId()
Dim myStrReader As StringReader = New StringReader(Request.Form("value"))
Dim xmlReader As XmlTextReader = New XmlTextReader(myStrReader)
Dim dPrice As String = ""
Dim dPDFURL As String = ""
Dim dProductType As String = ""
Dim dQtyOrdered As String
Try
' parse the xml sent in one of the post parameters from the online designer - use this information for the new product
xmlReader.MoveToContent()
If dID Is Nothing Then
dID = xmlReader.GetAttribute("id")
End If
If dSKU Is Nothing Then
dSKU = xmlReader.GetAttribute("id")
End If
If dTitle Is Nothing Then
dTitle = xmlReader.GetAttribute("type")
End If
dPrice = xmlReader.GetAttribute("price")
dPDFURL = xmlReader.GetAttribute("pdf_url")
xmlReader.ReadToFollowing("product") 'get product element
dProductType = xmlReader.GetAttribute("type")
xmlReader.ReadToFollowing("size") 'get size element
dQtyOrdered = xmlReader.GetAttribute("quantity")
'save the design xml into our database - to be passed back to online designer when customer loads a saved design
conn = New SqlConnection
conn.ConnectionString = sConn
conn.Open()
comm = New SqlCommand
comm.Connection = conn
comm.CommandText = "Insert into lv_customDesigns(designID, designSKU, designName, designXML, designType, designPDFURL, designPrice, designIsOrdered, designCustID, designProductType)" _
& "Values ('" + dID + "', '" + dID + "', '" + dTitle + "', '" + dXML + "', '" + dType + "', '" + dPDFURL + "', " + dPrice + ", 1, '" + currCustomerID + "', '" + dProductType + "')"
comm.CommandType = CommandType.Text
comm.ExecuteNonQuery()
'insert custom product into product table
Dim newProd As New Catalog.Product
newProd.Bvin = dID 'change to design id from online designer
newProd.ProductName = dTitle
if dPrice is Nothing then
newProd.SitePrice = CDec(0.00)
else
newProd.SitePrice = CDec(dPrice)
end if
newProd.CreationDate = Today
newProd.LastUpdated = Today
newProd.Sku = dSKU
newProd.ProductTypeId = "095bce9e-447c-469f-8658-28c4a460e681"
newProd.TemplateName = "CustomLayout"
newProd.TaxClass = "446337e0-8700-456a-8e48-c8a64365f2e9"
newProd.LongDescription = "Custom Wall Expression created in the Online Designer"
'store link to pdf of custom expression
newProd.ShortDescription = dPDFURL
newProd.PreContentColumnId = " - None -"
newProd.PostContentColumnId = " - None -"
newProd.VendorId = "- No Vendor -"
newProd.ManufacturerId = "- No Manufacturer -"
newProd.MinimumQty = 0
newProd.ShippingMode = CType(1, Shipping.ShippingMode)
Catalog.InternalProduct.Insert(newProd)
'add product to Custom Products Category
Dim prodCtgy As String = "c88e22fb-5eb5-46f4-a8b8-cf1e1db345b1"
Catalog.Category.AddProduct(prodCtgy, newProd.Bvin)
'add PDF of design to the file download for the product
'first insert the pdf file into the product file table
Dim f As New Catalog.ProductFile
f.FileName = Path.GetFileName(dPDFURL)
f.ShortDescription = Path.GetFileName(dPDFURL)
f.LastUpdated = Today
Catalog.ProductFile.Insert(f)
'next get the bvin of the product file record just inserted and associate the product with the pdf file in the productxfile table
Dim fbvin As String = Catalog.ProductFile.FindByFileNameAndDescription(f.FileName, f.FileName).ToString
Catalog.ProductFile.AddAssociatedProduct(f.Bvin, newProd.Bvin, 0, 0)
'finally insert the new product into shopping cart/basket
Dim prod As Catalog.Product = Catalog.InternalProduct.FindByBvin(newProd.Bvin)
Dim Basket As Orders.Order = SessionManager.CurrentShoppingCart
If Basket.AddItem(prod.Bvin, CInt(dQtyOrdered)) Then
Orders.Order.Update(Basket)
End If
'save file to the Files directory so that file downloads will work in the storefront
Dim inpath As String = Server.MapPath(CStr("liveart\images\uploaded\" & newProd.Bvin & "\" & f.FileName))
Dim outpath As String = Server.MapPath(CStr("Files\" & f.Bvin & "_" & f.FileName & ".config"))
File.Copy(inpath,outpath,True)
'return design id in response - sent back to online designer application
Dim sw As StringWriter = New StringWriter(System.Globalization.CultureInfo.InvariantCulture)
Dim xw As XmlTextWriter = New XmlTextWriter(sw)
xw.Formatting = Formatting.Indented
xw.Indentation = 3
xw.WriteStartDocument()
' Write the root element
xw.WriteStartElement("response")
'send the design ID back to the LiveArt application
xw.WriteValue(dID)
' end the root element
xw.WriteEndElement()
' Write the XML to file and close the writer
xw.WriteEndDocument()
xw.Flush()
xw.Close()
result.ResponseData = sw.GetStringBuilder.ToString()
sw.Close()
Catch ex As Exception
result.Errors.Add(New BVSoftware.BVC5.Core.Services.WS4.WS4Error("Exception", ex.Message))
End Try
If result.Success = False Then
DumpErrors(result.Errors)
Else
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "text/xml"
Dim output As String = result.ResponseData.Replace("encoding=""utf-16""", "encoding=""utf-8""")
Response.Write(output)
Response.End()
End If
End Sub
Private Sub DumpErrors(ByVal errors As Collection(Of BVSoftware.BVC5.Core.Services.WS4.WS4Error))
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "text/xml"
Dim output As String = String.Empty
Try
Dim sw As StringWriter = New StringWriter(System.Globalization.CultureInfo.InvariantCulture)
Dim xw As XmlTextWriter = New XmlTextWriter(sw)
xw.Formatting = Formatting.Indented
xw.Indentation = 3
xw.WriteStartDocument()
xw.WriteStartElement("Errors")
For Each e As BVSoftware.BVC5.Core.Services.WS4.WS4Error In errors
xw.WriteStartElement("Error")
xw.WriteElementString("Code", e.Code)
xw.WriteElementString("Message", e.Message)
xw.WriteEndElement()
Next
xw.WriteEndElement()
xw.WriteEndDocument()
xw.Flush()
xw.Close()
output = sw.GetStringBuilder.ToString
sw.Close()
Catch ex As Exception
output = "System Error: " & ex.Message
End Try
output = output.Replace("utf-16", "utf-8")
Response.Write(output)
Response.End()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>LiveArt Designer Upload Image Interface</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>