It's quite simplte. uCommerce is using a generic repository so you can actually use that. The most simple way to do it is resolve the repository you want (etc Category) via uCommerce's ObjectFactory and then fill in required information for category and save it through the repository. You just have to be aware that you have to set security settings for a user otherwise will it not show up at first.
Example:
var repository = ObjectFactory.Instance.Resolve>();
var category = new Category() { "...set properties..." };
Thanks for info :-) well yes, I did checked the forum post, I saw it before, and yes, I can see it now (haven't seen before) there is example with category aswell.
So way I understand it must be like so:
1) create product
2) create category
3) create relation
Creation of product - is pretty much streight forward, just one question, where do I get "ProductDefinitionId = 1, // Set to an existing defintion" my compiler complains here obviously cause there is no ProductDefinitionId setup in the first place, where is he taking it from?
Than Category, well pretty simple "var category = Category.SingleOrDefault(x => x.Name == "Software");" no questions here,
Than the relation... well first of all why would you use {} instead of () if you are trying to create a new instance of the object, and not defining the method?
var relation = new CategoryProductRelation
{
ProductId = product.ProductId,
CategoryId = category.CategoryId
};
Those {} are confusing me. how am I supposed to implement the relation?
I recommend creating category first because a product has to live at least in one category (as I recall). ProductDefinitionId = 1 can be found several places. The easist way to find out is to navigate to your definition in uCommerce tree and hold your mouse over the definition name. Then in the bottom of your browser you should be able to see what javascript method is being called. It should contain the id.
Another way is to find the id through SQL Server Management Studio and find the actually id.
Last way is to fetch the category with uCommerce API based on the definition name and then get the id from the definition object.
Nah... it's all ok, now I see, that It is actual .net functionality, not sure why i haven't used it before in such a way :-/ and thanks for one more link and info... now it makes much more sense :)))
I will try doing what I can with all this and will let you know if I have any trouble.
Hmm, but still is not beying assigned :-? for some reason, UCommerce.EntitiesV2 does not contain any ProductDefinitionId , it contains ProductDefinitionbut not the other one. Any suggestions? :-(
If I am trying to set ProductDefinition to = 1 - than compiler obviously compains cause you cannot set ProductDefinition to int...
I am using UCommerce.EntitiesV2, not sure what else should I do :( stuck again :(
ProductDefinition is the one you need to get. Then you can fetch the id from ProductDefinition. I think it has a property called ProductDefinitionId but I'm not sure on hand.
Just to share my results on this one with the community
This is the code I am using and it works for me so far :-)
1) In my example, I am creating new product in existing category
2) I create new caterogy and add it into the catalog.
//1) Create new product
// Get existing product definition - product requires one
var productDefinition = ProductDefinition.SingleOrDefault(x => x.Name == "BasicProduct");
// Get exsisting category called "Cars"
var category = Category.SingleOrDefault(x => x.Name == "Cars");
// Create new product
var product = new Product
{
Sku = "SKU-123456",
Name = "My Product",
AllowOrdering = true,
DisplayOnSite = true,
ProductDefinition = productDefinition // Set to an existing defintion
};
product.Save();
// Add product relation to category
var relation = new CategoryProductRelation
{
Product = product,
Category = category
};
relation.Save();
//2) Create totally new Category
// get existing Catalog that will contain the our new category
var catalog = ProductCatalog.SingleOrDefault(x => x.Name == "ShopCatalog");
// get existing category definition by Name
var definition = Definition.SingleOrDefault(x => x.Name == "Default Category Definition");
// create new category
var category2 = new Category
{
Name = "Bikes",
Definition = definition
};
// add Category to the catalog and save.
catalog.AddCategory(category2);
catalog.Save();
How to create category programmatically in C#? :)
Hello Guys, question is quite simple, maybe someone has an answer to this one?
Any kind of help is highly appreciated.
What I eventually need, is that I want to be able to Populate the uCommerce shop with programmatically.
I guess be able to create new Categories in my Default category definition group - fill it in.
Than create products with default product definition, and than populate them into those categories...
Isn't that a piece of cake? :-)
I think it is for someone who did it before...
Thanks alot for help!
//Dmitrij
Hi Dimitrij,
It's quite simplte. uCommerce is using a generic repository so you can actually use that. The most simple way to do it is resolve the repository you want (etc Category) via uCommerce's ObjectFactory and then fill in required information for category and save it through the repository. You just have to be aware that you have to set security settings for a user otherwise will it not show up at first.
Example:
var repository = ObjectFactory.Instance.Resolve>();
var category = new Category() { "...set properties..." };
repository.save(category);
you can also try to take a look at this: http://blog.lasseeskildsen.net/post/uCommerce-How-To-Create-A-Product-Using-The-API.aspx (a bit more simple)
Regards Martin
Hej Martin! :-)
Thanks for info :-) well yes, I did checked the forum post, I saw it before, and yes, I can see it now (haven't seen before) there is example with category aswell.
So way I understand it must be like so:
1) create product
2) create category
3) create relation
Creation of product - is pretty much streight forward, just one question, where do I get "ProductDefinitionId = 1, // Set to an existing defintion" my compiler complains here obviously cause there is no ProductDefinitionId setup in the first place, where is he taking it from?
Than Category, well pretty simple "var category = Category.SingleOrDefault(x => x.Name == "Software");" no questions here,
Than the relation... well first of all why would you use {} instead of () if you are trying to create a new instance of the object, and not defining the method?
Those {} are confusing me. how am I supposed to implement the relation?
//Dmitrij
Hi again,
I recommend creating category first because a product has to live at least in one category (as I recall). ProductDefinitionId = 1 can be found several places. The easist way to find out is to navigate to your definition in uCommerce tree and hold your mouse over the definition name. Then in the bottom of your browser you should be able to see what javascript method is being called. It should contain the id.
Another way is to find the id through SQL Server Management Studio and find the actually id.
Last way is to fetch the category with uCommerce API based on the definition name and then get the id from the definition object.
You are asking for using {} instead of (). I'm not quite sure whether you're asking for why I'm using object initializer (correct me if I'm misunderstanding). Try to look at this: http://weblogs.asp.net/dwahlin/archive/2007/09/09/c-3-0-features-object-initializers.aspx it will explain usage of object initializer.'
Best Regards Martin
Hej Martin,
Nah... it's all ok, now I see, that It is actual .net functionality, not sure why i haven't used it before in such a way :-/ and thanks for one more link and info... now it makes much more sense :)))
I will try doing what I can with all this and will let you know if I have any trouble.
Thanks so far,
Dmitrij :-)
Hmm, but still is not beying assigned :-? for some reason, UCommerce.EntitiesV2 does not contain any ProductDefinitionId , it contains ProductDefinition but not the other one. Any suggestions? :-(
If I am trying to set ProductDefinition to = 1 - than compiler obviously compains cause you cannot set ProductDefinition to int...
I am using UCommerce.EntitiesV2, not sure what else should I do :( stuck again :(
//Dmitrij
ProductDefinition is the one you need to get. Then you can fetch the id from ProductDefinition. I think it has a property called ProductDefinitionId but I'm not sure on hand.
Hello again Martin,
Sorry for my question, and thanks for your patience.
But you are saying here that:
"ProductDefinition is the one you need to get. Then you can fetch the id from ProductDefinition."
ok and this is what I see:
If that is what I need - it has no reference nor to ID nor to ProductDefinitionId ...
Instead Lassees article suggests to use
But as you can see I am getting an error :-/
//Dmitrij
Hi Dmitrij,
ProductDefinition property on Product-class is a reference to a productdefinition. One way to set the definition :
var productDefinition = ProductDefinition.SingleOrDefault(x => x.Name == "Definition name");
Then you can say
var product = new Product
{
ProductDefinition = productDefinition,
}
This way NHibernate will make sure to set the correct references in database tables.
Hej Martin,
Nice!!! I think I got it now :) It works perfectly now, thanks alot for help! :-)
Have a great day mate,
Dmitrij
Just to share my results on this one with the community
This is the code I am using and it works for me so far :-)
1) In my example, I am creating new product in existing category
2) I create new caterogy and add it into the catalog.
is working on a reply...