all things Sitecore related

Using XAML controls outside the Sitecore folder

By on January 23, 2012 in Daily Tip, Sitecore, Tips and Tricks, XAML with 3 Comments

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

Tags: , ,

About the Author

About the Author: Mark van Aalst is a Solution Architect for Sitecore Netherlands. Prior to working for Sitecore he has worked with the Sitecore CMS for more than 6 years and was a Sitecore MVP. Mark is also the initiator of the populair Sitecore Shared Source WeBlog module which he develops together with Nick Wesselman and Alistair Deneys. You can follow Mark on Twitter @markvanaalst or Google+ .

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

There Are 3 Brilliant Comments

Trackback URL | Comments RSS Feed

  1. Mark Ursino says:

    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.

  2. Mark Ursino says:

    That last comment was supposed to say “by registering them in the section of the config”

  3. Mark says:

    Good point, I have added your comment to the post.

    Thanks for the input

Post a Comment

Your email address will not be published. Required fields are marked *

Top