How to Build an Orchard module with VS2010

Topics: Writing modules
Nov 25, 2011 at 8:08 PM

I was trying to figure out how to build a module from VS2010. I went to the link at http://docs.orchardproject.net/Documentation/Walkthrough-Writing-An-Orchard-Module and tried to perform the Installing Software Prerequisites. Item 4. says to Dobule-click the csproj file in the release package to open in VS.

I have two problems with the above. First, I don't see any package specified as "release" so I just downloaded the "recomended" package.

The second problem was even more frustrating; the instructions say to "Double-click on the csproj file in the release package to open it in VS." I don't know which csproj file they are talking about. If I unzip the package then search for .csproj I get 28 items, but which one to use? All of these csproj files appear to be for creating the various Orchard modules. I'll just have to pick one and see where things go; if anyone knows the answer to that I'd be happy to know.

I found another document on creating a module using the command line code generator. I didn't want to do that since I wanted to use most of a module I've already written in VS2010.

Are there any other tutorials around that explain how to create a module with VS2010? I'll keep watching here and if I find any I'll stick them in here. But I'm hoping that by now other people are doing this and I've just not been able to find the right article yet.

 

Nov 25, 2011 at 9:42 PM

Those 28 .csproj files are all the different modules that ship with Orchard, you notice most of them are in the subdirectory Orchard.Web/Modules.

Orchard.Web is the actual website project, so that's the one you're supposed to open.

It seems that tutorial you linked is unfinished and out of date. There's a better one here: http://docs.orchardproject.net/Documentation/Building-a-hello-world-module

(Maybe that other one should just be deleted if nobody is going to finish or update it, and I'm not even sure where it's linked to from?)

Also it's pretty helpful to have a read through of most topics on the main docs home page: http://docs.orchardproject.net/

If you're doing module development it's generally best to get a full Mercurial clone of the source repository from codeplex. It's not essential but you might find some things tricky otherwise, it helps a lot with debugging, and I find myself digging into Orchard source a lot to see examples of how particular interfaces are used.

Finally - you actually should use the command line to generate your module because it sets it up with the correct folders and Web.configs to enable the correct securities, and of course the correct Orchad dependencies. Then copy in all the code you've written already in another project (or reference that as a library).

Nov 26, 2011 at 1:45 AM

Randompete,

Thanks. I have read those docs - but my retention isn't all that great, I need to read them again.

One question about Mercurial; I already have subversion and GIT installed and I really hate to try to learn yet another source control system. Is it possible to run Mercurial along side GIT and SVN? Is there a "simple" installation for Mercurial? I'll look around but sometimes those who use it know best.

Somewhere a few weeks ago I saw an article that explained how to create an add-in module with VS2010 and showed how to manually create the files that the command-line code generation tool creates. I was  just trying to by-pass learning yet another tool, but if that is what it takes, that's what I'll do.

Again, thanks for the info.

Bob

Nov 26, 2011 at 2:33 AM
Edited Nov 26, 2011 at 2:34 AM

You should get TortoiseHg, it's just like TortoiseSvn but for Mercurial obviously, it's highly recommended. I have all three running alongside each other, to be honest I find it's hard to get away with not having all of them, so many projects use one of those three. Prior to working with Orchard I hadn't ever used Mercurial, but I have to say after getting used to it, it's by far my favourite. Git is really fiddly whereas Hg makes branching, merging and forking so easy. I still have some personal repositories in SVN, and just today I realised I wanted to branch something, then suddenly remembered it was in SVN and decided not to even bother ;)

It sounds a lot more work learning to create the module files by hand than just using the command line which Orchard gives you for free, and is one simple command. You don't need the full source to do it. It's just that module development overall is better if you can properly build and debug the entire solution.

Nov 26, 2011 at 1:41 PM

I've bit the bullet and installed Mecurial. Was pretty easy and looks like it could become a favorite. Time will tell.

In the meantime I had found what appeared to be a really well written article on how to create the module with vs2010 at these two links:
http://rajandmello.blogspot.com/2010/08/adding-new-module-using-vs-2010.html

and

http://patrickyong.net/2010/07/25/hello-orchard-creating-a-orchard-module-using-latest-source-code/

These two look similar although the second talks about creating an Area and the first shows some info on creating content parts, handlers and drivers.

Both of them indicate that you need to look for Site Configuration menu on the dashboard. So, since I've got 1.3.9 orchard apparently that is no longer there?

I remember reading or watching a video tutorial somewhere that told how to do this and mentioned the command line but showed which folders and files were necessary - I cannot find that now.

But, I will go ahead and use Mercurial to pull the source code down and try the command line to get started. I just wish I could remember where that other article or video was. Guess my age is catching up to me...

Thanks,

Nov 26, 2011 at 3:25 PM

I would still recommend the Orchard Command Line to create a module. It involves 1 step instead of several ... There's a tutorial right there in the Orchard documentation: http://docs.orchardproject.net/Documentation/Building-a-hello-world-module

The admin areas you need are Settings (for configuring the site) and Modules (for enabling modules and features).

Dec 4, 2011 at 4:06 AM
Edited Dec 4, 2011 at 4:16 AM

I decided to try this. So I started up my Orchard project which in my system is located at H:\BT.CMS\BT.OrchardModuleTraining\Orchard-Module-Development\src\Orchard.Web\ in VS2010 by running in the debug mode. (I just pressed the F5 key.)

THis opened up my test website as http://localhost:30320/OrchardLocal so I signed in as the admin and opened the dashboard.

I went to the Modules link on the dashboard and under features I enabled Code Generation in the Developer section.

Then I opened up a command line and went to H:\BT.CMS\BT.OrchardModuleTraining\Orchard-Module-Development\src\Orchard.Web\bin but could not find Orchard.exe.

Any Idea as to why this didn't work? I followed the instructions in the URL from above ( http://docs.orchardproject.net/Documentation/Building-a-hello-world-module) and the other URL's that it referenced.

--- I decided to try a WebMatrix project I had and after doing the above steps I did find the Orchard.exe so I tried a different VS project and I found it on that one too. I don't know why the first project wouldn't generate the Orchard.exe where it was supposed to.  ---

Thanks,

Bob

Coordinator
Dec 4, 2011 at 4:29 AM
No. Should be there. No error whenbuilding from vs?

Sent from my TI-99/4A

From: n8nt
Sent: 12/3/2011 9:06 PM
To: Bertrand Le Roy
Subject: Re: How to Build an Orchard module with VS2010 [orchard:280860]

From: n8nt

I decided to try this. So I started up my Orchard project which in my system is located at H:\BT.CMS\BT.OrchardModuleTraining\Orchard-Module-Development\src\Orchard.Web\ in VS2010 by running in the debug mode. (I just pressed the F5 key.)

THis opened up my test website as http://localhost:30320/OrchardLocal so I signed in as the admin and opened the dashboard.

I went to the Modules link on the dashboard and under features I enabled Code Generation in the Developer section.

Then I opened up a command line and went to H:\BT.CMS\BT.OrchardModuleTraining\Orchard-Module-Development\src\Orchard.Web\bin but could not find Orchard.exe.

Any Idea as to why this didn't work? I followed the instructions in the URL from above ( http://docs.orchardproject.net/Documentation/Building-a-hello-world-module) and the other URL's that it referenced.

Thanks,

Bob

Dec 4, 2011 at 8:01 PM
Edited Dec 4, 2011 at 9:02 PM

I did not have any errors when I rebuilt it just now. However, early on when I first was attempting to add a module  per the instructions at the beginning of this thread there were some errors and now I'm not sure exactly how I fixed them. I have gone back to see if I could figure out what I did; turns out when I tried to build my HelloWorld with an Area I had a problem resolving a reference to Orchard and Orchard.Framework and somehow I added references to these and was able to rebuild without errors. I just looked at my references folder and see that these two are marked as if they are not correct, so I deleted them and now I am not able to find them anywehre in the solution.

So, I tried to start over.

I copied the latest Orchard source into a new folder and then I opened the solution ( H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.sln) in my VS2010

I built the solution - it had no errors.

THen I started it up and created a new site - just used the defaults and created me an admin account with the default admin.

When the new site came up I went to the dashboard, then to Modules and then enabled Code Generation.

From here (  H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web ) I opened up a command-line window ( by going to the folder in Explorer and holding the shift key down then right-clicking and selecting "open command window here."

In that command-line window I then executed the command:  bin\Orchard.exe

This gives me the following errors:


H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web>bin\Orchard.exe
Initializing Orchard session. (This might take a few seconds...)
log4net:ERROR [OrchardFileAppender] Unable to acquire lock on file H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-error-2011.
12.04.log. The process cannot access the file 'H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-error-2011.12.04.log' because i
t is being used by another process.
log4net:ERROR OrchardFileAppender: Failed to open [H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-error]. Attempting [H:\BT.C
MS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-error-1] instead.
log4net:ERROR [OrchardFileAppender] Unable to acquire lock on file H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-debug-2011.
12.04.log. The process cannot access the file 'H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-debug-2011.12.04.log' because i
t is being used by another process.
log4net:ERROR OrchardFileAppender: Failed to open [H:\BT.CMS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-debug]. Attempting [H:\BT.C
MS\BT.OrchardModuleTraining\Orchard-HelloWorld-Module\Orchard-Source\src\Orchard.Web\App_Data\Logs\orchard-debug-1] instead.
Type "?" for help, "exit" to exit, "cls" to clear screen

orchard>

However, I assumed this was because I still had Visual Studio (or maybe my web-browser with the new site) Open. So I first closed the web browser and tried the command again (after typing quit in the command window. That got the same set of errors. So I closed my solution in Visual Studio. Then I typed "quit" again in the command window and then "bin\Orcharde.exe" again. This time it worked and I got the 'Type "?" for help, "exit" to exit, "cls" to clear screen.' prompt.

Since I initially had the orchard> prompt even after the above error messages, would it have been ok to continue? I thought not,  and haven't tried, but just wondered.

Now I'll continue on with trying to create a module following the code-generation example above.

So next I genereated the new module structure by executing the following command in my command window:

codegen module HelloWorld

This succeeeded. I noticed that my Visual Studio indicated that there was a change and it asked me to upload changed files which I said yes to.

I noticed that now I had the new Module in my Modules folder.

I then went on to edit the manifest (module.txt) and add the Routes.cs and HomeController.cs files. I noticed that I don't have the MVC Intellisense for adding a controller (when I go to add the controller by right-clicking on the controllers folder, it does not offer me the "Add Controller" in the context menu.) Also, after adding the HomeController.cs file to the Controllers folder manually then, when I go to add the view by right-clicking on the Index Method, again I don't get offered the intellisense for adding a view. So I had to create that also by hand. So I created a folder underneath the Views folder named Home then created a view file names HelloWorld.cshtml. Is this the expected behavior, or should I be seeing the intellisense as in the past.

However, after following all of this, I was able to build the Hello World Module.

Thanks for the help.

Now I will try something a bit more complex.

Dec 5, 2011 at 2:47 PM

Those errors don't stop it working - it just sometimes happens when you have a web instance running, the log files are locked; and if so, the system will just start a new one with "-1" on the end.

Since you bring it up, I do think that error dump looks pretty bad, it could be offputting or even pretty scary for a first time Orchard developer. Since it's actually expected (and handled) behaviour, does it even need a notification? Maybe the message could be removed outright (it's a single line of code to delete in Orchard.Logging.OrchardFileAppender) or at least made friendlier. Could just say:  "Logging to '{0}'. '{1}' was locked by another process, this is normal when using console" instead.

Coordinator
Dec 5, 2011 at 8:37 PM

You're right, Pete. Patch & bug welcome for this.

Dec 6, 2011 at 8:03 AM
bertrandleroy wrote:

You're right, Pete. Patch & bug welcome for this.

Do you mean just suppress the message entirely?

Coordinator
Dec 6, 2011 at 8:04 AM

No, making it less scary.

Dec 6, 2011 at 12:10 PM

I've managed it by using System.Console.WriteLine to emit the friendly message; not ideal, but it's impossible otherwise, because the message is originated by this call in Orchard.Logging.OrchardFileAppender:

                        LogLog.Error(string.Format("OrchardFileAppender: Failed to open [{0}]. Attempting [{1}-{2}] instead.", fileName, fileName, newSuffix));

So currently, log4net takes that error and spits out to Console the final message you see. Due to where this sits in the whole DI infrastructure, it seems pretty much impossible to do anything else with the information than write it out to console.

However, then there's an additional problem that for some reason each log file is opened twice, so you get a total of four messages. Not what best to do about this!