Copied to clipboard

Flag this post as spam?

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


  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 06, 2009 @ 15:14
    Jan Skovgaard
    0

    Action handlers on the member section

    Does anyone know if it is possible to create an action handler in the member section?

    I think I read somewhere that it was not possible in v3.x.x - But is it possible in v4?

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Mar 06, 2009 @ 15:43
    Dirk De Grave
    0

    Hi Jan,

    It's possible for sure. Find a small tutorial on creating events at http://umbraco.org/documentation/books/api-cheatsheet or, more detailed, a nice post from Richard at http://www.richardsoeteman.net/PermaLink,guid,f470b6cf-40da-4aa9-a0d9-7b984fe9bf59.aspx

    Latter also has an overview of all available events to subscribe to.

    Regards,
    /Dirk

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 06, 2009 @ 19:13
    Jan Skovgaard
    0

    Hi Dirk,

    Fantastic! Once again you make my day. It's exactly what I am looking for. I think my current project in Umbraco is going to be the best ever! :)

    Thank you very much for the links.

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 12, 2009 @ 10:33
    Jan Skovgaard
    0

    Hmm, I have now been working on a action handler, which should automatically create a new document in the content tree, when a member is created in the member section.

    However it does not quite seem to be working and I am a bit unsure why.

    I have this code

    [code]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using umbraco.BusinessLogic.Actions;
    using umbraco.BusinessLogic.console;
    using umbraco.cms.businesslogic.web;

    namespace HeydayCreateUserSubNodes
    {
    public class CreateSubNodesHandler : umbraco.BusinessLogic.Actions.IActionHandler
    {
    #region IActionHandler Members

    public bool Execute(Document documentObject, umbraco.interfaces.IAction action)
    {
    // Only work with create event
    if (action.Alias != "create") return true;

    // Only work with Deltager document types
    if (documentObject.ContentType.Alias != "Deltager") return true;

    Document brugerDocument = null;

    brugerDocument = Document.MakeNew("Bruger", DocumentType.GetByAlias("Bruger"), documentObject.User, 1162);
    brugerDocument.getProperty("umbracoNaviHide").Value = "1";


    brugerDocument.Save();


    return true;

    }

    public string HandlerName()
    {
    return "CreateSubNodesHandler";
    }

    public umbraco.interfaces.IAction[] ReturnActions()
    {
    return new umbraco.interfaces.IAction[] { new umbraco.BusinessLogic.Actions.ActionNew() };
    //return new umbraco.interfaces.IAction[] { new umbraco.BusinessLogic.Actions.ActionPublish() };
    }

    #endregion

    }
    }

    [/code]

    This should create a document in the content tree named "bruger" under the exiting document with the id of 1162.

    But Nothing is beeing created - But it should be possible to create something in the content tree using an action handler when a member is created, right?

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Mar 12, 2009 @ 11:05
    Ismail Mayat
    0

    Jan,

    Your code is an action on document not member. Please refer to links in Dirks post namely the cheat sheet one which will show you how to wire up event for member. Up until v4 you could only wire up events to documents and your code was the way to do it.

    In v4 you can wire up to other elements in umbraco however the code to do it is slightly different.


    Regards

    Ismail

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 12, 2009 @ 11:17
    Jan Skovgaard
    0

    Hi Ismail

    Thanks for your reply. Could you provide me with a small example on how to do it? I am a bit of a n00b, so at the moment I am rather confused about it all.

    I just thought that the code would react on any kind of "create" event no matter if it was fired in the content section or the member section.

    I have looked at Richard's examples and I am wondering if the class in v4 should inherit the "ApplicationBase" in all cases of an action handler?

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Mar 12, 2009 @ 11:41
    Ismail Mayat
    0

    Jan,

    Inheriting from application base is the way of wiring up member/media and any other event for that matter so you are on the right track. Use the example on Richards blog and instead of

    [code]
    Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
    [/code]

    you would have

    [code]
    Member.New+= // not sure what sytanx is but visual studio will auto complete for you
    [/code]

    The new event model is one of my favourite features in v4 it gives ability to do so much more.

    Regards

    Ismail

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 12, 2009 @ 11:50
    Jan Skovgaard
    0

    Do I still have to make the "Member.New" thing when I am going to automatically create members or do member-stuff with my action-handler?

    The idea in this project is to create members manually, since there are only going to be a certain amount of members each year and there are gonna be a change in members every year, who are going to have access to the site. So therefore this process i manual.

    But everytime a member is manually created I want to create a document in the content section under certain document.

    I am just asking to make sure that we are following the same path :)

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Mar 12, 2009 @ 12:58
    Ismail Mayat
    0

    Jasko,

    The member.new is the wiring up of the event which will be fired when members are created regardless of whether they are created manually or via code. The code snippet is not creating new member.

    Regards

    Ismail

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 16, 2009 @ 13:00
    Jan Skovgaard
    0

    Hi Ismail,

    Thanks for making things more clear to me.

    Now I am trying to use the following:

    [code]
    Member.New += new Member.NewEventHandler(DocumentBeforePublish);
    [/code]

    But then visual studio gives me this error:
    No overload for 'Document
    BeforePublish' matches delegate 'umbraco.cms.businesslogic.member.Member.NewEventHandler'

    I have tried looking in the API documentation under "Member.NewEventHandler Delegate".

    And it seems like it takes two parameters from what I can get from the following code sample, found in the API documentation

    [code]
    public delegate void NewEventHandler(
    Member sender,
    NewEventArgs e
    )
    [/code]

    But I am not quite sure, what it means and how I should use it?

    I am modifying the sample "Auto Expire news" sample, from http://www.richardsoeteman.net/PermaLink,guid,f470b6cf-40da-4aa9-a0d9-7b984fe9bf59.aspx

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Mar 16, 2009 @ 13:08
    Ismail Mayat
    0

    Jan,

    Take a look here for some code.

    Regards

    Ismial

  • Richard Soeteman 3875 posts 12037 karma points MVP
    Mar 16, 2009 @ 16:24
    Richard Soeteman
    0

    Hi Janm

    In the constructor type "Member.New += " and then press the tab key twice all code is generated in Visual Studio for you then.

    Hope thsi helps,

    Richard

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 16, 2009 @ 16:25
    Jan Skovgaard
    0

    Hi Richard,

    Thanks for the tip. It's going to be useful later on.

    I have got my action handler up and running and it is working perfectly :)

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 18, 2009 @ 11:39
    Jan Skovgaard
    0

    So now I have succesfully created an action handler, that acts exactly as I want it to.

    Now I want to make another one, which ads the newly created member to a specific member group on the fly. But it is not really working...

    I have the following code

    [code]
    using System;
    using System.Collections.Generic;
    using System.Text;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic;

    namespace AddMemberToGroup
    {

    public class addMembertoDeltagerGroup : ApplicationBase
    {
    public addMembertoDeltagerGroup()
    {
    Member.AfterSave += new Member.SaveEventHandler(MemberAfterSave);
    }

    void Member
    AfterSave(Member sender, SaveEventArgs e)
    {
    //fetch a member from an email address
    Member m = Member.GetMemberFromEmail(sender.Email);

    //add roles / groups to the member (it assumes the group admin has been created)
    MemberGroup mg = MemberGroup.GetByName("Deltager");

    m.AddGroup(mg.Id);
    }

    }

    }
    [/code]

    I suspect that maybe "Member.GetMemberFromEmail(sender.Email); does not return any value...But I am not sure. Maybe I am checking on the wrong event or?

  • Richard Soeteman 3875 posts 12037 karma points MVP
    Mar 18, 2009 @ 17:14
    Richard Soeteman
    0

    Hi Jan,

    I suggest you use the BeforeSave Event instead of the after save. With your current code you first save a member and then assign the member to the group which will not be saved.

    Hope this helps,

    Richard

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 19, 2009 @ 09:58
    Jan Skovgaard
    0

    Hi Richard

    I have changed the code to use the BeforeSave Event but it still does not happen.

    Therefore I tried changing this line Member m = Member.GetMemberFromEmail(sender.Email); to Member m = Member.GetCurrentMember();

    But the magic still does not happen...I am a bit lost.

  • Tim Geyssens 1060 posts 27 karma points
    Mar 19, 2009 @ 10:00
    Tim Geyssens
    0

    No need to do that, you can just do

    MemberGroup mg = MemberGroup.GetByName("Deltager");

    sender.AddGroup(mg.Id);

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 19, 2009 @ 12:02
    Jan Skovgaard
    0

    Thanks Tim :)

    I also found out that I needed to do it on the "New" event and now it works perfectly.

    Since I am really loving this, to me, new world of action handlers I am also working on another on to set a default value.

    And it works when I hardcode a value.

    All I want to do, is to fill in a property when a document is created in the tree.

    Currently I have this code to set the property to contain "Sidenavn".

    sender.getProperty("propertyPageName").Value = "Sidenavn";

    But the value I am interested in, is the name value of the document.

    I have tried looking for something like "getPageName"...can anyone give me a hint on this one?

    Is there somewhere in the API I can find this kind of info?

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Mar 19, 2009 @ 12:18
    Ismail Mayat
    0

    jan,

    there is latest api docs here you need sender.Text. The documentation states:

    [quote]
    The name of the document, amongst other used in the nice url.
    [/quote]

    Regards

    Ismail

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Mar 19, 2009 @ 12:18
    Dirk De Grave
    0

    Hi Jan,

    sender.Text should give the name of the page (node name as in the tree)
    getProperty("") syntax is used for custom properties, all other 'general' properties are defined as public properties on the Document object.

    Regards,
    /Dirk

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Mar 19, 2009 @ 12:20
    Jan Skovgaard
    0

    Hi guys

    Thank you very much! :) Just what I was looking for.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies