This project is read-only.

Problem with IContentManager

Topics: Customizing Orchard, Writing modules
Jul 6, 2011 at 2:09 PM


I'm writing a shopping cart and have the following structure: ProductPart/Record that has a price, and a ShoppingCartPart/Record that has a number of ProductParts. I've got a link table and this is pretty much as per the 1-many/many-many tutorial.

My issue is that I'd like to create the ShoppingCartPart in the code dynamically for each user. I can create the record using the repository, but it doesn't seem to create the part correctly. I haven't found a way to create a part though.I think it's just a syntax issue :) Any help or pointers is very much appreciated!


I have also tried to create a ShoppingCart type and then use IContentManager.Create("ShoppingCart").As<ShoppingCartPart>(), but this always returns null. If I leave off the .As<> and inspect the result in the debugger the ContentItem returned is a ShoppingCart and has a ShoppingCartPart. Why is the .As<> not working? Again, any help/pointers are appreciated!




Jul 6, 2011 at 2:21 PM

I believe you should look through the 'Commands' code of any standard module.

E.g. Orchard.Blogs.Commands contains the method 'Create' with the following content:

 [CommandName("blog create")]
        [CommandHelp("blog create /Slug:<slug> /Title:<title> [/Owner:<username>] [/Description:<description>] [/MenuText:<menu text>] [/Homepage:true|false]\r\n\t" + "Creates a new Blog")]
        public string Create() {
            if (String.IsNullOrEmpty(Owner)) {
                Owner = _siteService.GetSiteSettings().SuperUser;
            var owner = _membershipService.GetUser(Owner);

            if ( owner == null ) {
                throw new OrchardException(T("Invalid username: {0}", Owner));

            if(!IsSlugValid(Slug)) {
                throw new OrchardException(T("Invalid Slug provided. Blog creation failed."));

            var blog = _contentManager.New("Blog");
            blog.As<ICommonPart>().Owner = owner;
            blog.As<RoutePart>().Slug = Slug;
            blog.As<RoutePart>().Path = Slug;
            blog.As<RoutePart>().Title = Title;
            blog.As<RoutePart>().PromoteToHomePage = Homepage;
            if (!String.IsNullOrEmpty(Description)) {
                blog.As<BlogPart>().Description = Description;
            if ( !String.IsNullOrWhiteSpace(MenuText) ) {
                blog.As<MenuPart>().OnMainMenu = true;
                blog.As<MenuPart>().MenuPosition = _menuService.Get().Select(menuPart => menuPart.MenuPosition).Max() + 1 + ".0";
                blog.As<MenuPart>().MenuText = MenuText;

            return "Blog created successfully";

Hope this helps.

Jul 6, 2011 at 5:03 PM


Yes, it helps enormously on using IContentManager. Thank you.