Run scripts in backend content views, fix for duplicate urlname

I had a requirement recently to make a module in which the "Title" and "UrlName" were set based on a date field.  This is done in the DynamicContent Updating event automatically.  So if the user typed "Bob" in as the title of a new content item it would really be changed to "2018-03-01-05-10AM" on save... physically impossible for the user to make it anything but the date.

So from there the users were to Actions->Duplicate each item and change the dates for each new years content item.

So I encountered a problem in that on duplicate the title and urlname come down EXACTLY as they are on the old item, no appending a 1 or 2 or anything.  So when the user changes their date and hits save, they get a nice popup saying "This content item already exists" (because the UrlName is already taken by the item it's copied from).

If telerik is reading this... maybe please fix this logic :)

So the UrlName field is mirrored automatically to the Title, and you can change this to really be any text field, but the DATE isn't a test field and will crash if you set the mirror to it.

The fix I came up with is this:
- Add script to the Duplicate view for the content item
- Script polls the urlname field until it sees a value (callback worked)
- Script then uses the MSAjax (🤮) to find the control and invisibly change it's value

Here is the script


You link it to the specific view by going to /Sitefinity/Administration/Settings/Advanced/ContentView then drilling down to
- TheContentType
- Views
- TheContentTypeBackendDuplicateView
- Scripts

Then simply add a new script and tell it where it lives and the method name to call after its available (I love that feature, thx guys)

AddScriptToRun

So obviously this is just one use-case but fundamentally you can run anything you want in any view, Insert, Edit, etc.

Please note this will (afaik) only work in the CURRENT < v10.2 Sitefinity, obviously the MSAjax (🤮) $find will not work in the new angular 2 system; docs aren't out on that yet.
Did you like this post, did I help you out, are you feeling generous

  Buy me a beer

Comments