Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    Mar 28, 2015 @ 01:34
    Nathan Woulfe
    0

    SaveAndPublishWithStatus -> cancel and delete node rather than renaming

    Title is fairly self-explanatory - I'm calling the SaveAndPublishWithStatus() method, and rather than renaming a node to nodename (1) if the name is a duplicate, I want to instead cancel the save and publish, and then delete the node. Is that possible?

    Figure I'll need to do something in a BeforeSave event handler, but can't find the appropriate API methods to make it work.

    Any help would be awesome.

  • James Jackson-South 489 posts 1747 karma points c-trib
    Mar 28, 2015 @ 02:05
    James Jackson-South
    0

    Why not check to see if a node of the same name exists before saving?

    Using Examine.

    ISearchCriteria criteria = ExamineManager.Instance.DefaultSearchProvider
                                                      .CreateSearchCriteria("content");
    
    IBooleanOperation filter = criteria.NodeName("Foo");
    IPublishedContent result = Umbraco.TypedSearch(filter.Compile()).FirstOrDefault();
    
    if(result != null)
    

    etc....

  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    Mar 28, 2015 @ 02:15
    Nathan Woulfe
    0

    Thanks James, probably should have thought of that! Only issue might be that I'm programmatically creating sets of nodes, so if there are dupes in the set the indexes may not be up to date when I check for duplicates. One way to find out tho...

    Just noticed too my beforesave handler isn't even firing, guess I'll be fixing that first.

  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    Mar 28, 2015 @ 06:22
    Nathan Woulfe
    0

    For any one who might be interested, code below does the trick - cancelling the save also stops the creation of the new node, which is nice. Checking .HasIdentity() ensures I'm not cancelling save on an existing node - I only want to stop the creation of duplicate names.

    protectedoverridevoidApplicationStarted(UmbracoApplicationBaseumbracoApplication,ApplicationContextapplicationContext){ContentService.Saving+=ContentServiceSaving;}privatevoidContentServiceSaving(IContentServicesender,SaveEventArgs<IContent>args){varcs=newContentService();varuh=newUmbracoHelper(UmbracoContext.Current);varcriteria=ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");foreach(varnodeinargs.SavedEntities){varfilter=criteria.NodeName(node.Name);IPublishedContentresult=uh.TypedSearch(filter.Compile()).FirstOrDefault();if (result!=null&&node.HasIdentity==false){args.Cancel=true;}}}
  • James Jackson-South 489 posts 1747 karma points c-trib
    Mar 28, 2015 @ 11:16
    James Jackson-South
    100

    Ah right. Glad you got it sorted.

    I've reposted your code sample again below for others so it is more readable.

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
    
        ContentService.Saving += ContentServiceSaving;
    
    }
    
    private void ContentServiceSaving(IContentService sender, SaveEventArgs<IContent> args){
    
        var cs = new ContentService();
        var uh = new UmbracoHelper(UmbracoContext.Current);
        var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");
    
        foreach(var node in args.SavedEntities) {
    
            var filter = criteria.NodeName(node.Name);
    
            IPublishedContentresult = uh.TypedSearch(filter.Compile()).FirstOrDefault();
    
            if (result != null && node.HasIdentity == false){
                args.Cancel=true;
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft