Programmatically changing url of created nodes via API
Hi,
So I am new to working with the API and I am working in a plain text editor (as opposed to Visual Studio). The following code runs whenever a node is saved and it will automatically empty the recycle bin. While useful for testing, I need to do something more relevant: I need to programmatically change the url of the node when it gets saved.
Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Logging;
public class LogWhenSaved : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
Umbraco.Core.Services.ContentService.Saved += ContentService_Saved;
}
void ContentService_Saved(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
{
sender.EmptyRecycleBin();
}
}
I wouldn't know why you'd want to change the url of a node on a save event?
If you wish to change the url of a node, you'd be better off creating your own implementation of a UrlProvider or UrlSegmentProvider, depending on what you want to change to the url
To better explain our goal, I’m looking to set up some functionality that creates a URL based on a predefined format. This URL would get its path based on property values.
For example, if I had a post for February 16th, 2017, I would need some way to have the URL be assigned a format like follows: www.mywebsite.com/blog/2017/02/16
I figure there could be a date picker editor or something along those lines that allowed the user to enter the date and then the code would assign the url based off of the value entered.
Could you provide an example of something to this end that includes the surrounding code necessary to run this as a standalone .cs file in the App_Code directory?
First, you'll be implementing a UrlProvider to "build up" the url in the format you mention, based on value of property/properties of a node (outbound)
Once that's done, you'll need a IContentFinder implementation (inbound), so you "map" the url back to a node, and get the info displayed through the template.
Good luck!
Let us know if you have more questions along the journey
Programmatically changing url of created nodes via API
Hi,
So I am new to working with the API and I am working in a plain text editor (as opposed to Visual Studio). The following code runs whenever a node is saved and it will automatically empty the recycle bin. While useful for testing, I need to do something more relevant: I need to programmatically change the url of the node when it gets saved.
Thanks!
Hi Thomas,
I wouldn't know why you'd want to change the url of a node on a save event? If you wish to change the url of a node, you'd be better off creating your own implementation of a UrlProvider or UrlSegmentProvider, depending on what you want to change to the url
See https://our.umbraco.org/Documentation/Reference/Routing/Request-Pipeline/outbound-pipeline for more info.
If you're still stuck with the examples, let us know and give us some examples of desired output and we'll get you in the right direction...
--Dirk
Hi Dirk,
Thanks for the response.
To better explain our goal, I’m looking to set up some functionality that creates a URL based on a predefined format. This URL would get its path based on property values.
For example, if I had a post for February 16th, 2017, I would need some way to have the URL be assigned a format like follows: www.mywebsite.com/blog/2017/02/16
I figure there could be a date picker editor or something along those lines that allowed the user to enter the date and then the code would assign the url based off of the value entered.
Could you provide an example of something to this end that includes the surrounding code necessary to run this as a standalone .cs file in the App_Code directory?
Thanks,
-Tom
Hi Thomas,
Here's a reference you might find useful: http://24days.in/umbraco-cms/2014/urlprovider-and-contentfinder/
First, you'll be implementing a UrlProvider to "build up" the url in the format you mention, based on value of property/properties of a node (outbound)
Once that's done, you'll need a IContentFinder implementation (inbound), so you "map" the url back to a node, and get the info displayed through the template.
Good luck!
Let us know if you have more questions along the journey
--Dirk
is working on a reply...