Modifying content through code...why so different?
Other content requires a whole checkout, checkin process
www.sitefinity.com/.../modifying-content-items
...but ecomm just says get the item then call a workflow method...? Is that legit and tested?
www.sitefinity.com/.../modifying-products
...also after trying this it seems it doesn't even work
Even modifying it to be the lifecycle syntax doesn't work
Hello Steve,
I have verified that this code is tested. The eCommerce code however, expects that the product is correctly in Draft before you call the method that modifies it.
This code isn't changing my data at all...
public
void
UpdateProduct(AbleProduct product)
CatalogManager manager = CatalogManager.GetManager();
ProductType productType = manager.GetProductTypes().Where(t => t.Title ==
"General product"
).SingleOrDefault();
if
(productType ==
null
)
return
;
// Product Type does not exist
Product master =
this
.SitefinityProductList.Where(x => x.GetValue<
decimal
>(
"AbleProductID"
) == product.Id && x.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master).SingleOrDefault();
if
(master !=
null
)
if
(product.InStock != master.Inventory)
Product temp = manager.Lifecycle.CheckOut(master)
as
Product;
master.Inventory = product.InStock;
//masterProduct.Sku = product.Sku;
//masterProduct.Weight = product.Weight;
master.Price = product.Price;
master.Featured = product.IsFeatured;
//manager.Provider.RecompileItemUrls(masterProduct);
master = manager.Lifecycle.CheckIn(temp)
as
Product;
manager.Lifecycle.Publish(master);
manager.SaveChanges();
Hi Steve,
I did a few tests with the following and it works as expected:
protected
void
Page_Load(
object
sender, EventArgs e)
var catMan = CatalogManager.GetManager();
var masterP = catMan.GetProducts().Where(p => p.Title ==
"Nasko"
&& p.Status==ContentLifecycleStatus.Master).FirstOrDefault();
EditProduct(masterP.Id,
"NaskoModified"
,
"DescriptionModified"
);
public
static
void
EditProduct(Guid productId,
string
newTitle,
string
newDescription)
CatalogManager catalogManager = CatalogManager.GetManager();
Product product = catalogManager.GetProduct(productId);
if
(product !=
null
)
product.Description = newDescription;
product.Title = newTitle;
product.UrlName = Regex.Replace(newTitle.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
catalogManager.RecompileItemUrls<Product>(product);
catalogManager.SaveChanges();
var contextBag =
new
Dictionary<
string
,
string
>();
contextBag.Add(
"ContentType"
, product.GetType().FullName);
string
workflowOperation =
"Publish"
;
WorkflowManager.MessageWorkflow(product.Id,
product.GetType(),
"OpenAccessDataProvider"
,
workflowOperation,
false
,
contextBag);