Thursday, November 4, 2010

Create Widget in Orchard Module using VS 2010

Add/Create Widget in Orchard Module

For example name of module is “TestModule” and widget name is “TestWidget”

1.                  Add class in Modules>> TestModule>> ViewModels>>TestWidgetViewModel.cs
Add this code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace TestModule.ViewModels

{

    public class TestWidgetViewModel

    {

        public string var1 { getset; }

        public int var2 { getset; }
    }

}



2.                  Add class in Modules>> TestModule>> Models>>TestWidgetPart.cs

Add this code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Orchard.ContentManagement;

 

 

namespace TestModule.Models

{

    public class TestWidgetPart : ContentPart

    {

    }

}


3.                   Add class in  Modules>> TestModule>> Drivers>>TestWidgetPartDriver.cs

Add this code

using Orchard.ContentManagement.Drivers;

using TestModule.ViewModels;

using TestModule.Models;

 

namespace TestModule.Drivers

{

   public class TestWidgetPartDriver : ContentPartDriver<TestWidgetPart>

    {

        protected override DriverResult Display(TestWidgetPart part, string displayType, dynamic shapeHelper)

        {

            var model = new TestWidgetViewModel();

            return ContentShape("Parts_TestWidget",

                                () =>

                                {

                                    var shape = shapeHelper.Parts_TestWidget();

                                    shape.ContentPart = part;

                                    shape.ViewModel = model;

                                    return shape;

                                });

        }

    }

}

4.                  if Migrations.cs file is not available create in TestModule solution and add code as

using System;

using System.Collections.Generic;

using Orchard.ContentManagement.Drivers;

using Orchard.ContentManagement.MetaData;

using Orchard.ContentManagement.MetaData.Builders;

using Orchard.Core.Contents.Extensions;

using Orchard.Data.Migration;

using System.Data;

 

 

namespace TestModule.DataMigrations

{

    public class Migrations : DataMigrationImpl

    {

        public int Create()

        {

            

            ContentDefinitionManager.AlterTypeDefinition("TestWidget",

               cfg => cfg

                   .WithPart("TestWidgetPart")

                   .WithPart("CommonPart")

                   .WithPart("WidgetPart")

                   .WithSetting("Stereotype""Widget")

               );

 

            return 1;

        }

 

    }

}



5.                  If Placement.info file is not available create in TestModule solution and add code as

<Placement>

  <Place Parts_TestWidget="Content:1"/>

Placement>

 
6.                  In View folder create new folder Parts add file as TestWidget.cshtml add code as

<h1>Test Widgeth1>

7.                  Rebuild/compile solution and run
8.                  logon with admin in site
    1. go to dashboard >>Site Configuration>>Features
    2. Enable/Update your TestModule

9.               Go to Widgets>>Manage Widgets
    1. Where you TestWidget in available widget list
    2. From where you can add to particular layer and zone you want.


  1. step 8 and 9 can be done in code and its done when module is enable

    1. create setup folder in TestModule
    2. create file  Modules>> TestModule>> Setup>>TestWidgetSetupService.cs
and add code as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.Widgets.Models;

namespace TestModule.Setup
{
    public class TestModuleSetupService : ITestModuleSetupService
    {
        private readonly IContentManager _contentManager;

        public TestModuleSetupService(IContentManager contentManager)
        {
            _contentManager = contentManager;
        }

        public void Setup()
        {
           
            var homepageLayer = _contentManager.Query<LayerPart>().Where<LayerPartRecord>(lp => lp.Name == "TheHomepage").List().FirstOrDefault();

            var beforeContent = _contentManager.Create("TestWidget");
            beforeContent.As<WidgetPart>().LayerPart = homepageLayer.As<LayerPart>();
            beforeContent.As<WidgetPart>().Title = "";
            beforeContent.As<WidgetPart>().Zone = "BeforeMain";
            beforeContent.As<WidgetPart>().Position = "1";
            _contentManager.Publish(beforeContent);

           
        }
    }
}


    1. create one more file file  Modules>> TestModule>> Setup>>ITestWidgetSetupService.cs

add code as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard;

namespace TestModule.Setup
{
    public interface ITestModuleSetupService : IDependency
    {
        void Setup();
    }
}




    1. update migration.cs file as


public class Migration : DataMigrationImpl
    {
        private readonly ICorporateSiteSetupService _testModuleSetupService;
       
        public Migration(ICorporateSiteSetupService corporateSiteSetupService)
        {
            _testModuleSetupService = testModuleSetupService;
        }
        public int Create()
        {

        ContentDefinitionManager.AlterTypeDefinition("TestWidget",
               cfg => cfg
                   .WithPart("TestWidgetPart")
                   .WithPart("CommonPart")
                   .WithPart("WidgetPart")
                   .WithSetting("Stereotype""Widget")
               );



        _testModuleSetupService.Setup();

        return 1;
        }

    }