I have an Umbraco V4.7 website, the website is going to be used to add some listings and I want the nodeName of these listings to be a unique reference number. I realise that the best number to use is likely to be the nodeId because this is going to alway be unique. Which brings me to the question, is it possible to auto populate the "Name" field that appears when you create a new node?
Ideally I would like to populate this field with the node Id but I realise that this may not be possible because it does not have a node Id yet. Therefore if this is not possible, would it be possible to populate it with something generic such as "Listing" and then as soon as I click create the nodeName is changed to the Node ID?
So the ideal solution would be for the user to click create, and if the document type is "Listing" then the "Name" field populates with "Listing" so all they need to do is click "Create" again and the node will be created with the nodeName being the nodeId.
Hopefully this makes sense and I appreciate any feedback.
I haven't investigated very well so I don't know for sure, but I don't think it's possible to prepopulate the name field.
However, in /umbraco/config/create/UI.xml there's a reference to (/umbraco)/create/content.ascx - You could try and change the codebehind for this usercontrol to use your own logic. However, be aware that if you ever upgrade your site, you will have to re-implement these changes as this file will get overwritten!
What I do currently is tell the editor to just enter something (anything, it really doesn't matter) and then in a Document.AfterSave event I detect which documenttype it is and only for the "Listing" types I would just always change the name to something (like the NodeId).
Thank you for the response, I thought that I might need to use an event handler. Is the AfterSave on going to be the best one to use or is there one that would change it as soon as you click the "Create" button? Are there any blogs etc that you know of that might give me some help in setting up an event handler to change the nodename? I am only just learning .net and I have used event handlers before but never to change a property of a node.
Not sure if changing it earlier would do you any good, you're not really going to see the name anyway unless you really go look for it.
This should work for you:
using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web;
namespace SomeCompany.EventHandlers { public class ListingTitleHandler : ApplicationBase { public ListingTitleHandler() { CMSNode.AfterSave += SetListingTitle; } private static void SetListingTitle(object sender, SaveEventArgs e) { if (sender is Document == false) return;
var doc = sender as Document; if (doc == null || doc.ContentType == null || doc.ContentType.Alias != "Listing") return; doc.Text = doc.Id.ToString(); } } }
You could try handling the event CMSNode.AfterNew (change SaveEventArgs to NewEventArgs), but that would only change it once (after it is first created) and doesn't prevent people from changing it later on. So I think AfterSave might be better for your purposes.
Thank you that is working perfectly, the only minor issue is that there is a slight delay when creating a node before the details appear. Is this normal because it is running the event handler? I am using the following code:
Nah, the delay must've been there already. Since you're using the AfterSave event handler, it shouldn't be causing delays during the creation of the new page because the eventhandler only kicks in after the page gets saved. ;)
It is kicking in when I create a new page though because the node appears with the NodeID as the name straight away before doing any saving. Which to be honest is exactly what I was looking for, I just wondered if it could be causing the delay.
Ok cheers, think part of the delay may also be caused by the Google maps datatype package that I am using on the nodes in question. Thank you for your help
Auto-populating the create "Name" field
Hi All,
I have an Umbraco V4.7 website, the website is going to be used to add some listings and I want the nodeName of these listings to be a unique reference number. I realise that the best number to use is likely to be the nodeId because this is going to alway be unique. Which brings me to the question, is it possible to auto populate the "Name" field that appears when you create a new node?
Ideally I would like to populate this field with the node Id but I realise that this may not be possible because it does not have a node Id yet. Therefore if this is not possible, would it be possible to populate it with something generic such as "Listing" and then as soon as I click create the nodeName is changed to the Node ID?
So the ideal solution would be for the user to click create, and if the document type is "Listing" then the "Name" field populates with "Listing" so all they need to do is click "Create" again and the node will be created with the nodeName being the nodeId.
Hopefully this makes sense and I appreciate any feedback.
I haven't investigated very well so I don't know for sure, but I don't think it's possible to prepopulate the name field.
However, in /umbraco/config/create/UI.xml there's a reference to (/umbraco)/create/content.ascx - You could try and change the codebehind for this usercontrol to use your own logic. However, be aware that if you ever upgrade your site, you will have to re-implement these changes as this file will get overwritten!
What I do currently is tell the editor to just enter something (anything, it really doesn't matter) and then in a Document.AfterSave event I detect which documenttype it is and only for the "Listing" types I would just always change the name to something (like the NodeId).
Hi Sebastiaan,
Thank you for the response, I thought that I might need to use an event handler. Is the AfterSave on going to be the best one to use or is there one that would change it as soon as you click the "Create" button? Are there any blogs etc that you know of that might give me some help in setting up an event handler to change the nodename? I am only just learning .net and I have used event handlers before but never to change a property of a node.
Regards
Tony
Not sure if changing it earlier would do you any good, you're not really going to see the name anyway unless you really go look for it.
This should work for you:
You could try handling the event CMSNode.AfterNew (change SaveEventArgs to NewEventArgs), but that would only change it once (after it is first created) and doesn't prevent people from changing it later on. So I think AfterSave might be better for your purposes.
Hi Sebastian,
Thank you that is working perfectly, the only minor issue is that there is a slight delay when creating a node before the details appear. Is this normal because it is running the event handler? I am using the following code:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
umbraco.BusinessLogic;
using
umbraco.cms.businesslogic;
using
umbraco.cms.businesslogic.web;
namespace
Umbraco_Lettings
{
publicclassEventHandlers :
ApplicationBase
{
public
EventHandlers()
{
CMSNode
.AfterSave += Document_AfterSave;
}
#region
AfterSave events
privatestaticvoid Document_AfterSave(object sender, SaveEventArgs
e)
{
if (sender isDocument == false) return
;
var doc = sender asDocument
;
if (doc == null || doc.ContentType == null || doc.ContentType.Alias != "Letting") return
;
doc.Text = doc.Id.ToString();
}
#endregion
}
}
Nah, the delay must've been there already.
Since you're using the AfterSave event handler, it shouldn't be causing delays during the creation of the new page because the eventhandler only kicks in after the page gets saved. ;)
It is kicking in when I create a new page though because the node appears with the NodeID as the name straight away before doing any saving. Which to be honest is exactly what I was looking for, I just wondered if it could be causing the delay.
D'oh! Of course, because Umbraco itself needs to save the new node as well, yes, that would cause a slight extra delay then. :)
Ps. Don't forget to mark a post as the solution, thanks!
Ok cheers, think part of the delay may also be caused by the Google maps datatype package that I am using on the nodes in question. Thank you for your help
is working on a reply...