Navigatie

Contact

Send mail to the author(s) E-mail

View Richard Soeteman's profile on LinkedIn

RSS 2.0 | Atom 1.0 | CDF

Archief

Categorieën

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In

Zoeken

# Friday, 29 April 2011
Friday, 29 April 2011 13:06:57 (GMT Daylight Time, UTC+01:00) ( CMSImport | Umbraco )

This Blog is not active anymore, further posts will be available on my company website. Read new posts here

A question I get asked a lot is if it is possible to assign a template based on a datasource value. The answer is yes, normally the default assigned template will be assigned when importing your data and this is what 99% of the users want and expect. But using the RecordImported Event we can get the datasource value from the database and use that alias to map the template against the imported document.

Example

In this example we have some data coming from a database that we want to a standard Runway text page. Some items such as Integer and Donec are really important and we want to display these items in Pink. In Umbraco we added a new template called PinkTemplate and as you can see in the image below we added that Template alias in our Datasource that we are going to import.

Example data

As I mentioned earlier we can use the event system to manipulate the document during import . You can use the event system for CMSImport in the same way you use the normal Umbraco event system. In the example below you see that we derive from ApplicationBase and wire up the RecordImported event in the constructor. Since we only want to set the template during the initial import, we first check if the Importaction is  ImportAsNew. Then we need to check if the template column from the datasource is not null. You can access all  items from the datasource  using the e.Items collection. When the template column is not null, we get the template id from the database and assign that value to the document.

   1:  using CMSImport.Extensions.Import;
   2:  using umbraco.BusinessLogic;
   3:  using umbraco.cms.businesslogic.template;
   4:  using umbraco.cms.businesslogic.web;
   5:   
   6:  namespace AssignTemplate
   7:  {
   8:      /// <summary>
   9:      /// This class wil automatically assign a template to the document when being imported for the first time using the RecordImporting Event
  10:      /// If you want to use this class in your own project simply rename e.Items["Template"] to the template column you use
  11:      /// in your datasource
  12:      /// 
  13:      /// This code will work in both the Free and PRO edition of CMSImport and is written for version 1.2. 
  14:      /// If you have any questions please use the forum or email support@soetemansoftware.nl 
  15:      /// </summary>
  16:      public class AssignTemplateUsingDatasource : ApplicationBase
  17:      {
  18:          /// <summary>
  19:          /// Initializes a new instance of the <see cref="AssignTemplateUsingDatasource"/> class.
  20:          /// </summary>
  21:          public AssignTemplateUsingDatasource()
  22:          {
  23:              //Whenever a records gets imported 
  24:              ContentImport.RecordImported += new ContentImport.RecordImportedEventHandler(ContentImport_RecordImported);
  25:          }
  26:   
  27:          /// <summary>
  28:          /// When a record got imported this event is raised.
  29:          /// </summary>
  30:          /// <param name="sender">The imported document.</param>
  31:          /// <param name="e">The <see cref="CMSImport.Extensions.Import.RecordImportedEventArgs"/> instance containing the event data.</param>
  32:          private void ContentImport_RecordImported(object sender, RecordImportedEventArgs e)
  33:          {
  34:              //Only assign the value when its imported for the first time
  35:              if (e.ImportAction == ImportActions.ImportAsNew)
  36:              {
  37:                  Document doc = sender as Document;
  38:                  
  39:                  //Get the template value from the datasource. Datasource values are stored in the Items event arguments
  40:                  //Replace templateName with your column name in the datasource!!!
  41:                  string templateAlias = e.Items["templateName"].ToString();
  42:   
  43:                  if (doc != null && !string.IsNullOrEmpty(templateAlias))
  44:                  {
  45:                      //An alias is specified, try to get the template based on the specified alias  
  46:                      Template template = Template.GetByAlias(templateAlias);
  47:                      if (template.Id != null)
  48:                      {
  49:                          //Template is found assign it to the document
  50:                          doc.Template = template.Id;
  51:                      }
  52:                  }
  53:              }
  54:          }
  55:      }
  56:  }

When the import is finished you see that all items are imported and when you we take a look at the Integer document you’ll see that the assigned template is PinkTemplate instead of the default Runway Textpage template.

Pink template assigned

Why isn’t this supported in the UI?

As I mentioned before most people expect to assign the default value. When I include the assign template via datasource functionality in the UI it’s an option that you need to think about before setting or ignoring the option. My idea with CMSImport is that I want it to be a no brainer to use and introducing this option will make it a little bit harder to use. So if you want this I think it’s better to write 10 lines of code instead.

Downloads

Below you can download the complete source code. Change e.Items[“templateName”] to the column name you want to use in your project, build the project and add the DLL to the bin folder of your Umbraco install. That’s all!

Download the source code here

Comments [0] | | # 
# Wednesday, 27 April 2011
Wednesday, 27 April 2011 15:54:21 (GMT Daylight Time, UTC+01:00) ( mediaprotect | Package | security | Umbraco )

This Blog is not active anymore, further posts will be available on my company website. Read new posts here

Last month  I’ve released Media protect 1.0. Mediaprotect is an Umbraco  package that helps you protect media in the same simple way as you protect documents in Umbraco. Once the package is installed and you protect certain media, or content nodes, the files will be password protected. Only authenticated members can open the files, otherwise members will be redirected to the login screen.

The problem protecting media

No solution without a problem Winking smile. When you use Umbraco, by default all media items can be accessed directly. If you have a media item that you don’t want to be accessed and someone knows the url (maybe got a link via email from a friend), or you have a link in your content  to a media item that is crawled by Google and the user uses that url the media item will be shown/downloaded, if you like it or not.

This is also the case with upload fields on a protected content node. The page is protected but the uploaded file is not protected.

How Media protect solves this problem

Once you’ve installed media protect files on protected content nodes are protected by default and you’ll get the public access menu item on the context menu of your media section.

Best of all you don’t need to modify any existing media or document type, it just works!

The public access menu item  is the same menu item you get when you want to protect a node in the content section.

Public access

When you select Public access you’ll get the same dialog as you get when protecting content. You can specify if you want to use single user or role based protection. You can also specify the login and not authorized page.

Dialog

When you are finished protecting the media item, the media item gets a No Access sign. This indicates that the media is protected and can’t be accessed directly. When you open the media item and click on the document link you will be redirected to the login page since you are not logged in. Once you are logged in as a member and the member role is assigned as allowed roles to view the media item you can download the file.

protectedmedia

Compatible with Media Pickers and default asp.net Membership provider

It’s nice that we can easily protect media items but we also want to make sure that the editor doesn’t accidently pick protected files using a Media Picker, or in the Rich Text Editor. Media Protect will also display the same No Access sign when you want to pick a media item using a media picker or via the Rich text editor. Not only the default Umbraco pickers but media protect is also compatible with some 3th party pickers, such as Multi Node Tree Picker (The awesome picker from UComponents) and the DIgibiz Advanced Media Picker. Best of all if you have a custom media picker you can add the alias of that media picker to the mediaprotect.config file and it will be picked up automatically.

DIgibiz Advanced Media Picker

Speaking about compatibility. Mediaprotect is based on the default ASP.Net Membership provider, so you can use the default asp.net controls for logging in etc. and you can also use your own  custom membership provider.  Media Protect comes with a small API you can use in a .Net usercontrol/XSLT or Razor script to determine if an item is protected, or to determine if the current logged in user has rights to access the media node.

Free alternatives

Media protect is a commercial package, it protects your media out of the box(no need to change any document or media type , it just works).It will cost you 99 euro per domain though. If you want to protect media without spending money you might want to check out these free alternatives

More info and Download

If you want more info about Media protect I suggest you check out the product site. Here you find a video demonstrating the package and documentation. You can also download a fully functional free trial from our.umbraco.org. This will work on your local machine (localhost), only thing you get for free is a nice yellow sign that says you are running in Trial mode. The trial never expires and when you want to deploy your site you can buy a license for that domain or buy an enterprise license for your whole company.

Feedback

Please tell me what you think about this package. Like it, hate it? I would love to know.
Comments [2] | | #