Copied to clipboard

Flag this post as spam?

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


  • Gustav Hultberg 7 posts 27 karma points
    Nov 01, 2012 @ 02:41
    Gustav Hultberg
    0

    Delete members by a user control

    Subject: I want to delete members manualy by a user control insted from backend.

    Question 1: Which tables should record be deleted in and in which order?

    I have done a test with the following tables and order:

    cmsPropertyData, cmsContent, umbracoNode, cmsMember2MemberGroup and cmsMember.

    that not works (gives an error with meaning that it is a conflict with table cmsContentVersion. After some research I think also that table should be involved and also cmsContentXml but I am not sure.

    Question 2: Where can I find description of the database with relations?

    I am using version 4.7.1

    Thanks

    Gustav

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 04, 2012 @ 03:17
    Bo Damgaard Mortensen
    0

    Hi Gustav,

    A general rule of thumb when working with Umbraco is to avoid altering data directly in the database since Umbraco is creating an XML representation of the data found in the database :-) Deleting data directly in the database might mess up the backend since there will be a data mismatch.

    What I think you should consider is to use the API for deleting the member. Do you know the member ID or email perhaps? I.e., if you know the email you coulduse the API like this:

    var member = Member.GetMemberFromEmail(email);
    if(member != null)
    {
    member.Delete();

    // Refresh XML cache
    umbraco.library.RefreshContent();

    The above example is written from the bottom of my memory so you might want to let the intellisense do some work for you ;-)

    Also, if you know the ID of the member you can get a reference to the member object like this:

    var member = new Member(id);

    Hope this helps. Let me know if there's any holdups.

    All the best,

    Bo

  • Gustav Hultberg 7 posts 27 karma points
    Nov 05, 2012 @ 12:39
    Gustav Hultberg
    0

    Thanks a lot Bo,

    It works perefect. I used 'GetMemberFromLoginName' to get the user to delete. Another question that I maybe have missed is updating the XML cache when get members are created by another user contol. The code (vb)  like this (short version):

    Dim Member1 As MembershipUser = Membership.CreateUser(txtUserName.Text.Trim, sPassword, txtEmail.Text.Trim)

    Roles.AddUserToRole(Member1.UserName, "BaseMember")

    Dim Profile As ProfileBase
    Profile = ProfileBase.Create(Member1.UserName)
    Profile("surename") = txtSurename.Text.Trim
    Profile("givenname") = txtGivenname.Text.Trim
    Profile("mobilenumber") = txtMobilenumber.Text.Trim
    Profile.Save()

    Do I have to update the XML cache and in that case should I use umbraco.library.RefreshContent(); or any other function for the added member?

    Best regards

    Gustav

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 05, 2012 @ 13:20
    Bo Damgaard Mortensen
    0

    Hi Gustav,

    I just had a quick chat with Sebastiaan (https://twitter.com/cultiv) about it. It turns out that the XML cache should be refreshed. The example he wrote was:

    member.XmlGenerate(new System.Xml.XmlDocument()); 
    Member.AddMemberToCache(member);

    All the best,

    Bo

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 05, 2012 @ 13:47
    Sebastiaan Janssen
    0

    As Gustav is using the membership provider API I honestly don't know how to do that. 

    But you could do:

    var member = Member.GetMemberFromEmail(txtEmail.Text.Trim);
    member.XmlGenerate(new System.Xml.XmlDocument());  
    Member.AddMemberToCache(member); 
  • Gustav Hultberg 7 posts 27 karma points
    Nov 05, 2012 @ 14:25
    Gustav Hultberg
    0

    I have been googled about how to create members manually and find that membership provider API should be used instead of the "old" way since version 4.x. Is it better to use as in http://our.umbraco.org/wiki/reference/api-cheatsheet/working-with-members as it is more native to Umbraco? In the example in the link there is a way to handle XML cache.

    Next q are. When I republish entire site in Umbraco Backend. Will that also create XML cache for members that not have been cashed before?

    //Gustav

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 05, 2012 @ 14:29
    Sebastiaan Janssen
    0

    This is actually a thing that we need to un-obsolete. Dealing with members through the membership provider api is possible but overly complex. In the courses we also use examples like the one you found using the Member API instead. So I would recommend that you use it instead of the membership provider.

    As for your second question: no, it will only generate cache for content. But I think an application pool recycle will help to put all members in the cache during the app pool startup.

  • Gustav Hultberg 7 posts 27 karma points
    Nov 06, 2012 @ 00:18
    Gustav Hultberg
    0

    I have now changed to use Member API to create and delete member. It works fine (thanks for y advice Bo and Sebastiaan). I have 3 more questions.

    1. Should I also use "m.XmlGenerate(New System.Xml.XmlDocument)" when I update existing members value (example email) to refresh the cache?

    2. Should it be before or after "m.save"?

    2. To change a members password (Hashed) I am using this code (membership provider):

         Dim members As MembershipUser
         members = Membership.GetUser()
         members.ChangePassword(members.ResetPassword(), txtNewPassword1.Text)

    Is that okey or should I use Member API and if - how?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 06, 2012 @ 14:39
    Sebastiaan Janssen
    0

    1. Yep.

    2. No always after. First save changes, then update the cache with those changes.

    3. Looks okay (haven't tested). I would probably once again go back to the Member API and try if a login with the old password works, if yes, change the password to the new password.

  • Gustav Hultberg 7 posts 27 karma points
    Nov 07, 2012 @ 10:41
    Gustav Hultberg
    0

    Sebastiaan,

    I´m getting a little bit confused. In the codeexample in our.umbraco.org/wiki/reference/api-cheatsheet/working-with-members  the cache is update before save and y wrote after (as I readed it). Code below from the wiki:

    "//Generate member Xml Cache
    m.XmlGenerate(new System.Xml.XmlDocument());

    //Save member
    m.Save(); "

    In your reply above y wrote:

    "var member = Member.GetMemberFromEmail(txtEmail.Text.Trim);
    member.XmlGenerate(new System.Xml.XmlDocument()); 
    Member.AddMemberToCache(member);"

    As I have googled the "AddMemberToCache" should be using when members are loging in.

    My code in short to create a member are (after changed dto use Member API):

    "sPassword = RandomPassword()

    Dim mt As MemberType = MemberType.GetByAlias("Medlem")

    Dim m As Member = Member.MakeNew(CheckForApostrof(txtFnamn.Text.Trim) & " " & CheckForApostrof(txtEnamn.Text.Trim), CheckForApostrof(txtEpost.Text.Trim), mt, New umbraco.BusinessLogic.User(0))

    m.LoginName = CheckForApostrof(txtAnvandarnamn.Text.Trim)
    m.Password = sPassword

    Dim mg As MemberGroup = MemberGroup.GetByName("Medlemmar")

    m.AddGroup(mg.Id)

    m.getProperty("orgpersnr").Value = CheckForApostrof(txtOrgPersnr.Text.Trim)
    m.getProperty("fornamn").Value = CheckForApostrof(txtFnamn.Text.Trim)
    m.getProperty("efternamn").Value = CheckForApostrof(txtEnamn.Text.Trim)
    m.getProperty("mobiltelefon").Value = CheckForApostrof(txtMobilnr.Text.Trim)

    m.XmlGenerate(New System.Xml.XmlDocument)
    m.Save()"

    It works, but I have to be real sure that it is the correct code.

    PS. Sebastiaan, as y work in the U HQ, do y now when it will be course/training in Umbraco next year in Europe? DS.

    Regards

    Gustav

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 07, 2012 @ 13:47
    Sebastiaan Janssen
    0

    Actually you're right it doesn't really matter which order you do it in, AddMemberToCache is indeed necessary to log them in, XmlGenerate should be before that (if you created a new member). And m.Save will take care of writing any updates to the database. Actually it shouldn't even be necessary because things like "m.getProperty("mobiltelefon").Value = CheckForApostrof(txtMobilnr.Text.Trim)" save immediately to the database anyway.

    No idea when new courses will be planned (not my department, sorry), as soon as they're available they will be posted on the site.

Please Sign in or register to post replies

Write your reply to:

Draft