Saturday, April 21, 2012

Consuming a Service with a DotNetNuke Module

Consuming a service in a DotNetNuke Module turns out to be cleaner than I had expected.  Note that this article concerns using the “Add Service Reference” functionality of Visual Studio 2010.  It was inspired by a post that discussed consuming a Web Service which may be found at

The service I was interested in consuming was the one supplied with SmarterMail installations.  Consuming this API is described here and here.

I found it useful to test the procedure below out on a simple console application before I tried putting it in a module.

To start, you’ll need to know the URL of your SmarterMail installation.  You can get this by just logging into your SmarterMail instance.  If you don’t have a login that has administrator rights, you can stop right here because things won’t work unless you do.  You should see a URL something like (only first part shown):…

Your mileage may vary, but I found the root directory for the SmarterMail API Service by simply eliminating everything after the “:9998/” and adding the word “services” so the basic URL for the service looks like

This should show a listing of “asmx” files like:


These files when opened will show you information on the services available.  Consider the vile svcAliasAdmin.asmx.  When opened in a browser this displays (partial snipit):


To find more information out about an operation, you can click on it, but this will reveal the actual XML messages which are not needed for our purposes here.  What we really want is the URL of the service we want which will be something like:

This is what we want to feed into the VS 2010 “Add Service Reference” dialog which we get when we right click on the project name and select “Add Service Reference”.  Fill this in the textbox labeled “Address” and click “Go”


This should result in the svcAliasAdmin service being displayed in the “Services” box.  Now go down and fill in the box at the bottom with something more meaningful than “ServiceReference1”, for example “AliasAdminService”:

Now click OK.  One of the side effects of this will be to create a “system.servicemodel” section under “configuration” in your app.config or web.config.  This section should be deleted as we will be doing this all from code so we don’t need to add things to the DNN site’s web.config.
First, at the top your c# file at the end of the using statements, add:

using System.ServiceModel;

Then add the following lines in the “Main” or “page_load” section depending on if you are doing a console applications (which you should probably do first to test this) or a web project.

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint =
    new EndpointAddress(EPAddressstr);
AliasAdminService.svcAliasAdminSoapClient AliasAdmin =
    new AliasAdminService.svcAliasAdminSoapClient(binding, endpoint);
var listResult = AliasAdmin.GetAliases(SMUserIDstr, SMPasswordstr, SMDomainstr);

You will note that EPAddressstr, SMUserIDstr, SMPasswordstr, and SMDomainstr have not been defined.  Replace (or define them above their use) as follows:

string EPAddressstr =,
    SMUserIDstr =,
    SMPasswordstr = "yourpassword",
    SMDomainstr = "";

If you set a breakpoint after the call to “GetAliases”, then you can examine the contents of “listResult” to see if your call succeeded.

Obviously there are more parameters that are set in the automatically generated “system.servicemodel” section that you deleted from the config file, but if you are looking for a minimum starting solution, the above was a good starting point for me.

Adding this to a module was remarkable only in its simplicity.  I just followed the above procedure, packaged the module, and it worked the first time I installed it on my test DNN installation.  I fully expected to have to edit the manifest file and “targets” file in the BuildScripts folder to make sure the appropriate files got included in the zip and put in the right place upon installation, but, amazingly, this was not the case.
Read more!

Saturday, December 04, 2010

Word 2010 – change the templates folder, lose your quickparts

Microsofts KB 924460 gives instructions on how to change the location of your templates folder.  Unfortunately, if you do this you will lose your QuickParts for Outlook unless you also copy over NormalMail.dotm to the new location.  I did this and the QuickParts came back.  I also copied over Normal.dotm as well in case things had been stored there.

Read more!

Wednesday, June 16, 2010

C# string Extension Method that never returns null

I’m sure somebody else has done this, but I couldn’t find it on the web, though I did find a nice post on Neat Tricks with Extension Methods that I found useful.  I have continually been confronted with the problem of checking strings for nulls.  Usually I don’t care if the string is null or empty, I just want it treated as either empty or if it has a value as that value.  The extension method below seems to do the trick for me as if you call it with something like

string s = null;
if (s.ValueOrEmpty().Contains(“foo”)) { … }

then you will be sure never to get one of those nasty null reference exceptions.  Here’s the code.  I put it in a project of its own as I think I’ll likely add more extensions in the future and want to use them in most of my code.  I did what the author of the post referenced above said “I've placed both of these extension methods inside the System namespace. That way, all I have to do is reference the assembly containing my extensions and they will always be available”.  Here’s the code:

namespace System
        public static string ValueOrEmpty(this string s)
            return s == null ? "" : s;

Read more!

Monday, June 07, 2010

Outlook 2007 notification problems solved by inbox repair tool

Outlook 2007 some time ago ceased notifying me of new messages. I had the options set to notify by sound and by pop-up if a new message arrived. I looked around on the web and tried a number of things that didn’t help. Yesterday I got a notification from Archive to look at a post in my deleted messages folder and saw a reference to the Outlook Inbox Repair Tool, scanpst.exe. I found the tool in C:\Program Files (x86)\Microsoft Office\Office12 on my computer.

I ran the tool and repaired the inbox and suddenly my notifications were working just fine. If you are having troubles with your new message notifications in Outlook, this is something else to try.

Read more!

Tuesday, May 25, 2010

Inserting Records into a Database via an ASP.Net GridView with EntityDataSource

I found that there are a few little “tricks of the trade” that one needs to know when inserting new records into a database using a GridView that references an EntityDataSource in ASP.Net.

I wired up the GridView to my database using an EntityDataSource as described in Diego Vega’s video All went well. I could view, sort, edit, and delete records. But insert? There didn’t seem to be a lot of info available on how to do an insert. I had done inserts in GridViews before so I started with the same basic stuff:

  • put a couple of filter fields and a filter button at the top
  • add a clear and “insert new record” button
  • click through to the event handlers and fill them in

When filling in the insert new record button event handler I suddenly discovered that I didn’t seem to have an EntityContext to hang my new record on. The answer was in Julie Lerman’s wonderful book Programming Entity Framework (O’Reilly, 2009). A 2nd edition of this book is due out soon that will cover version 4 of the Entity Framework. Chapter 11 “Using the ASP.NET EntityDataSource Control” has a section on page 279 titled “Using your own context” gave me the clues I needed.

I declared a class-level variable of the appropriate type (e.g. “YourModel.YourEntities YourContext;”) and then assigned it in the page_load event (e.g. “YourContext = new YourModel.YourEntities()”). I then used the EntityDataSource_ContextCreating event to set the EntityDataSource’s Context before it created one of its own (e.g. “e.Context = YourContext”).

Now I had access to the context! In the BtnNew_Click event I created a new entity object (e.g. “var newEntityItem = YourModel.YourItem.CreateYourItem(YourRequiredDataFields)”), populated the fields the way I wanted them, wired in the new object (e.g. “YourContext.AddToYourItemSet(newEntityItem)”), and saved the changes (e.g. “YourContext.SaveChanges()”).

There is one other little change you’ll want to make. For some strange reason, there is no confirm on the GridView generated delete link. Fortunately, this is easy to fix. Just turn off the generation (delete “ShowDeleteButton=”true”” from the CommandField of the GridView) and add the following after the CommandField entry:

<asp:LinkButton id="btngvdelete" runat="server"
OnClientClick="return confirm('Are you sure you want to delete this record?');"
CommandName="Delete" ForeColor="Black">Delete</asp:LinkButton>

For a cleaner look, instead of putting an insert button at the top you could probably put another LinkButton in the grid to handle the insert.

Read more!

Wednesday, August 05, 2009

How to remove the extra line breaks in outlook 2007 messages

This has been driving me nuts for some time. I would send a message to a yahoo group from Outlook 2007 and when it came back through all my spacing was doubled. At first I tried to find a way to create stationery that would take care of the problem. Later I discovered that the problem is that Outlook treats each "Enter" as a paragraph and maps it to a <p> html tag (with accompanying closure) and that if you want a simple <br /> tag then you have to use a Shift-Enter in composing your message. The way I solved it was to create stationery that properly reflected the eventual way the message would look. Here's how to do that:

First go into Microsoft Word 2007 and create a new document. Format this document the way you want it. You don't need to put any text in it. Here's what I did. First I selected all the text with ctrl-a. Then I opened the styles pane by clicking the little arrow pointing down and to the right on the Styles panel of the Home tab on the ribbon. When you hover the mouse pointer over the "Normal" style, a pull down menu will appear. Select "Modify..." Then select the font you want (I picked Arial 12 point). Pull down the format menu at the lower left and select "Paragraph". Then I made sure "Don't add space between paragraphs of the same style" was NOT checked, set spacing after to 12 points, set line spacing to single and then clicked OK.

Now you are ready to save the document. Select "Save as" and select "Other Formats". Pull down the "Save as type" drop down list and select "Web Page (*.htm,*.html)" Navigate to the folder "%appdata%\Roaming\Microsoft\Staionery" and save the file as something like "BasicEmail.html". %appdata% is different for different Microsoft operating systems. To find out where it is, open a command window (start, run, "cmd"), type cd %appdata%, press enter, and you should be able to see where it is located on your system.

Now to link this into Outlook, pull down the Tools menu in Outlook and select Options. Then select the Mail Format tab and click the "Stationery and Fonts" button. Select the "personal stationery" tab and click the "theme" button. You should see your new stationery listed somewhere in the list on the left. Select it and click OK back through the dialog boxes you have opened.

Now new email will have the format you want to send.

Read more!

Saturday, May 30, 2009

Vista's GUI Scheduled chkdsk doesn't seem to do the job

I ran into problems on my hard disk and Vista's GUI chkdsk didn't prove up to the task of getting it healthy again. Booting to repair mode from the Vista install DVD and running "chkdsk /P" from a command prompt did the trick and showed what it was doing as well.

UPDATE: It seems I had a bad disk drive. I replaced it, got Norton Disk Doctor running, installed Vista SP2 and a bunch of updates and now chkdsk seems to do the job on boot that I expected it to. I'm not sure what fixed it. I'm still getting some errors on the drive, so stay tuned.

My HP 9700 Vista machine was hanging every night. I checked the error logs and found that there were disk corruption errors logged shortly before each hang. I used the GUI to schedule a chkdsk. You do this by opening "Computer", right clicking on the drive you want to check and select properties. On the tools tab, select "Check Now ..." in the section "This option will check the volume for errors.". In the resulting dialog "Automatically fix system errors" is already checked and you can optionally check "Scan for and attempt recovery of bad sectors". Clicking start will inform you that "Windows can't check the disk while in use" and ask you if you want to check for hard disk errors the next time you "start your computer". NOTE: My experience is that it doesn't mean reboot, but start your computer from the Dead Off position. I tried just the first one but the system hung that night and then I tried the second one but it didn't seem to run on the reboot.

I didn't much like this because it seemed to hang a long time on a blank screen at startup, but not long enough to really have gone through and checked all the sectors on the disk. It was impossible to tell because you get zero feedback during this process of staring at the blank screen.

I found this not too satisfactory so I decided to run chkdsk from the command prompt. I thought I would just go the whole way and not use the Vista that was on my hard drive. I booted from a Vista install DVD, went into the repair option, and selected a command prompt. I decided that "chkdsk /B" which means "NTFS only: Re-evaluates bad clusters on the volume (implies /R)" would be the way to go. The "/R" option decodes to "Locates bad sectors and recovers readable information (implies /F)". The "/F" option means "Fixes errors on the disk". This resulted in a very long process but was really good about telling everything that is going on. Now my disks are clean and healthy (I hope)!

I decided that Vista just couldn't be trusted to keep my disks in good health, so I purchased a copy of Norton SystemWorks (direct from Symantec for $49 downloadable). I hope that does the job. If it doesn't, I'll blog about it. I'll also blog about it if I find things I particularly like.

Read more!