How to debug Windows Live Writer plugins
This is final post related to live writer plugins and its about debugging the Live writer plugins. While developing I don’t think there is a way we can debug the plugin code. This is after deployment. Initially I faced one issue with options dialog, it was related to object reference issue, I got a message like this.
It only shows the exception message, not the stack trace or any other information which helps to find the exception. For debugging, you need to attach the Windows Live writer to Visual Studio(we may need to run visual studio as Administrator) using Attach Process feature of Visual Studio from Debug menu.
Happy Programming.
How to create a Windows Live writer plugin using C# Part 2
This is the second part of live writer plugin post, in this post I am implementing settings for the live writer plugin. First post I was about creating basic Live writer which help to short link using bit.ly shortening service. Now I am adding a few more link shortening services like tiny.cc and tinyurl. Also I am providing an option which helps to decide whether the link should open in a new Window or not, or more precisely adding a target=”blank” attribute to the inserted anchor tag.
First we need to inform Live Writer that the plugin has settings, which can be done using HasEditableOptions=true in plugin attributes. Now we will create the settings form, which will be like the following.
Live writer will invoke the EditOptions method in the Plugin class for displaying the settings, so we need to override the method for displaying the form.
public override void EditOptions(IWin32Window dialogOwner)
{
Settings settings = new Settings(_pluginSettings);
settings.ShowDialog();
}
For implementing plugin settings we can use IProperties, which helps plugin to read/write the settings. The Live writer understands the basic types, like string, int, float, bool and decimal. IProperties exposes methods to get and set the values. Like this
private IProperties _properties;
public bool UseBlank
{
get
{
return _properties.GetBoolean("UseBlank", true);
}
set
{
_properties.SetBoolean("UseBlank", value);
}
}
To save and retrieve the setting the following methods of IProperties class can be used.
private PluginSettings _settings;
private void cmdSave_Click(object sender, EventArgs e)
{
_settings.UseBlank = chkBlankTarget.Checked;
_settings.Provider = ddlProvider.SelectedItem.ToString();
Close();
}
//Constructor of the Settings Form.
public Settings(PluginSettings settings)
{
InitializeComponent();
_settings = settings;
chkBlankTarget.Checked = _settings.UseBlank;
ddlProvider.SelectedItem = _settings.Provider;
}
For accessing settings in the Plugin, we need to override the Initialize() method of the Plugin class, which takes the IProperties as parameter, LiveWriter will invoke the plugin with the settings.
private PluginSettings _pluginSettings;
public override void Initialize(IProperties pluginOptions)
{
base.Initialize(pluginOptions);
_pluginSettings = new PluginSettings(pluginOptions);
}
Build the project, launch the Windows Live Writer, click on the Plugin options, which will show the Plugin options dialog.
Next select Link shortener plugin and click on Options button, which will display the plugin settings dialog we designed.
Change the settings and Save the changes, try run the plugin and verify the results.
Happy Programming. Next post will be about debugging Live writer plugin.
How to create a Windows Live writer plugin using C# Part 1
Recently I got chance to explore Windows Live Writer. So I thought I will create few posts around Live Writer plugins. In this post I am covering the basics of creating Live Writer plugin. I found various plugins which helps to insert syntax highlighting code in wordpress. So I thought I will create one different, this plugin helps to insert a shorten link using bit.ly site.
So first create a class library, make sure you are selecting the target framework as .Net 2.0, otherwise it may not work. The add refernce of Live Writer API(WindowsLive.Writer.Api.dll), which will be available in C:\Program Files (x86)\Windows Live\Writer. Inherit from ContentSource class or SmartContentSource class. In this post I am using ContentSource class, both of them can be used to create the Plugin. Override the CreateContent method, this method will be invoked when users click on the Plugin in LiveWriter. Create a Windows Form, with one textbox and two buttons.
And add following attributes to the plugin class which helps live writer to identify the Plugin properties.
[InsertableContentSource("Link shortner")]
[WriterPlugin("9E560094-F1BA-433D-9FB7-4DEA9A4C3F2F",
"Link shortner",
PublisherUrl = "www.dotnetthoughts.net",
Description = "Helps to insert shorten links.",
HasEditableOptions = false,
ImagePath = "wlwicon.png")]
public class LinkShortner : ContentSource
{
}
HasEditableOptions parameter, helps to provide settings for the live writer plugin. And the ImagePath parameter helps to provide Icon for the plugin. The size should be 16×16. And it should added and Build action should be Embedded Resource.
And here is the overriden CreateContent() method.
public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
{
using (Editor editor = new Editor())
{
editor.StartPosition = FormStartPosition.CenterParent;
DialogResult result = editor.ShowDialog(dialogOwner);
if (result == DialogResult.OK)
{
//This function will convert long
//url to short url using bit.ly REST API.
//Editor.Url property returns the
//text of the textbox control.
string resultUrl = CreateShortUrl(editor.Url);
string url = string.Format
("<a href="\"http://{0}/\"">{0}</a>", resultUrl);
content = url;
}
return result;
}
}
Thats it. We are done with the creation of live writer plugin. Now the next part is deployment. Change the project output path to C:\Program Files (x86)\Windows Live\Writer\Plugins location, you may need to run Visual Studio as Administrator. Now launch the Windows Live Writer, you can see the plugin loaded
.
Clicking on it will popup the Windows form, with textbox, and clicking on Shorten button will insert a link on the post.
Next part I will post about how to create settings for plugin. Happy Blogging
How redirect output from a console application
GUI tools are offers better User experience than command line tools. But in few scenarios we don’t require GUI, like MS Build tasks, and in some cases the application may not have a UI, example: ImageMagik tools. If check codeplex.com, you can find n number of applications, which doesn’t have a UI.
The process class supports redirection of output, error and input to streams instead of normal console output stream. Process class also supports events, which will be raised on error or output data received.
And here is the implementation, which invokes the regasm.exe in .Net Framework location, and executes regasm.exe without any command line argument, which will write the help text to the Text box.
string app = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe";
Process process = Process.Start(app);
process.EnableRaisingEvents = true;
process.OutputDataReceived += (o, dre) =>
{
//To avoid cross thread exceptions.
if (txtOutput.InvokeRequired)
{
this.txtOutput.BeginInvoke((Action)
delegate { this.txtOutput.Text += dre.Data + Environment.NewLine; });
}
else
{
this.txtOutput.Text += dre.Data + Environment.NewLine;
}
};
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
The external tools option in Visual Studio works in similar fashion. You can use this technique for creating user interface for command line tools you use frequently.
Happy coding









