In certain cases you want to offer the end-user a userfriendly interface to manage content, users or anything else. Thanks to Sitecore’s backend flexibility this is possible in more ways than one. In this case I will use the technique which is described in this blogpost by Adam Conn.
In the example you can see that a new window is opened by the code below:
public override void Execute(CommandContext context) { Assert.ArgumentNotNull(context, "context"); UrlString url = new UrlString("/sitecore/shell/~/xaml/GridExample.GridExamplePage"); url["id"] = context.Items[0].ID.ToString(); SheerResponse.ShowModalDialog(url.ToString(), "650", "500"); }
As you can see this is done by calling a url with a /~xaml/ handler. The xaml handler calls the Sitecore.Web.UI.XamlSharp.Xaml.XamlPageHandlerFactory class which basicly searches for the control. In the example from Adam the control which is being searched for is placed in the “/sitecore modules” folder. By default Sitecore searches these controls in the “/sitecore modules” and “/sitecore/shell/Applications” folders.
But if you build custom applications within Sitecore the chance is that you do not want to place the custom code into the “/sitecore” or “/sitecore modules” folders. Of course you can place the code into your own location, but then the XAML handler cannot find the control.
The configuration which defines the folders for Sitecore to look for controls is inside the “/App_Config/XamlSharp.config” file. There are two watchers defined which tell Sitecore to look into the folder mentioned earlier. You can add a new watcher by adding it to the XamlSharp.config. Or better, use a Sitecore include config file.
That would be something like this:
/CustomShell/GridExample *.xaml.xml *.xaml.xml.cs true
and the url to call in the command would be like this
public override void Execute(CommandContext context) { Assert.ArgumentNotNull(context, "context"); UrlString url = new UrlString("/~/xaml/GridExample.GridExamplePage"); url["id"] = context.Items[0].ID.ToString(); SheerResponse.ShowModalDialog(url.ToString(), "650", "500"); }
Update:
Mike Ursino (check out his blog here) added a comment that it is also possible to register a single control by adding the namespace and assembly to the XamlSharp.config file. In that case your config file would be like this:
http://www.yourcompany.com/webcontrols Company.Controls Company.Web.UI.WebControls
3 Comments
Great post. One question though: can’t you also add your new controls to Sitecore by registering them in the
section of the config? I recently did this to create my first custom XAML control via C#. I registered my namespace and assembly in that section to use it from another XAML file.
That last comment was supposed to say “by registering them in the section of the config”
Good point, I have added your comment to the post.
Thanks for the input