Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 23, 2010 @ 12:15
    Bo Damgaard Mortensen
    0

    Node is being shown twice in content tree

    Hi all,

    I've just made a usercontrol which is placed on the dashboard in umbraco to create a new member and then create a node assigned to this member. All is working as expected, but when I refresh the content tree, the created note shows up twice (with the same id) I've tried to republish entire site and reload all nodes, but nothing works.

    Has anyone ever found a solution to this problem? I have done a search both here on our.umbraco.org, google and stackoverflow.com but I haven't found a way to counter it yet :(

    This is how I do it programatically:

    DocumentType dt = DocumentType.GetByAlias("mymembertype")

    Document doc = Document.MakeNew(member.UserName, dt, User.GetUser(0), 1112);

            doc.Publish(User.GetUser(0));

            umbraco.library.UpdateDocumentCache(doc.Id);

            Access.ProtectPage(true, doc.Id, 1079, 1083);

            Access.AddMembershipUserToDocument(doc.Id, member.UserName);

    Also, as a side-question: what I don't like about this method is, that I have to hardcode the id of the parent node (1112) and the login and loginerror (1079, 1083) pages. Is there any other, more elegant way of doing this? 

    Thanks in advance!

    Bo

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Nov 23, 2010 @ 13:33
    Robert Foster
    0

    Hey Bo,

    Most of the time when I create a user control that needs parameters like the root node, or login pages, etc.  I'd create a config file for it and put the configuration details there.  Read them out into static variables when the user control is created so you don't have to unecessarily do more disk read operations.

    Regarding the duplicate node issue: what version of Umbraco are you using?  I'm doing essentially the same thing when creating the document, but not seeing the duplicate node issue.  Mind you, I'm not trying to protect the document.  My code looks like this:

     

                    var docType = DocumentType.GetByAlias(Configuration.CategoryDocumentType);

                    doc = Document.MakeNew(category, docType, CurrentMember.User, Configuration.CategoryParentDocumentId);
                    doc.Publish(CurrentMember.User);
                    umbraco.library.UpdateDocumentCache(doc.Id);
  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 23, 2010 @ 13:55
    Bo Damgaard Mortensen
    0

    Hi Robert, thanks for tuning in! :) greatly appreciated.

    Good idea using a config file to store the values. Can I trick you into pasting an example of your config? Just for inspiration purposes ;) 

    I'm running Umbraco 4.5.2 for .NET 3.5. It seems like our code is quite similar other than the values of course. I have tried without the protection part, but it's still the same. I'll paste all my code here, just in case I have missed something.

     

        protected void btnCreateMember_Click(object sender, EventArgs e)
        {      
            // Create new member
            var member = Membership.CreateUser(txtLoginName.Text, txtPassword.Text, txtEmail.Text);
            // Assign role to the new member
            Roles.AddUserToRole(member.UserName, ddGroups.SelectedItem.ToString());
            // Get the member from Umbraco
            var umbracoMember = Member.GetMemberFromLoginName(member.UserName);
            // Assign properties to membertype<
            umbracoMember.getProperty("navn").Value = txtName.Text;
            umbracoMember.getProperty("memberType").Value = "test";
            // Create new document
            DocumentType dt = DocumentType.GetByAlias("mymembertype");
            Document doc = Document.MakeNew(member.UserName, dt, User.GetUser(0), 1112);
            // Publish the document
            doc.Publish(User.GetUser(0));
            umbraco.library.UpdateDocumentCache(doc.Id);
            // Protect the document<
            Access.ProtectPage(true, doc.Id, 1079, 1083);
            Access.AddMembershipUserToDocument(doc.Id, member.UserName);
            // If success, let the user know and reset the fields
            Response.Write(@"
    ");
            ResetFields();
        }
     

    Hopefully the above code example makes sense, if not - please let me know :)

    I have also checked in the database for duplicates, but I couldn't find any. It feels like *something* needs to be refreshed, but I don't know what that could be..

    Thanks again.

    Bo

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Nov 23, 2010 @ 14:06
    Robert Foster
    0

    Seeing as I'm such a helpful, community minded person :P here's my config class (I basically stole the idea from another project):

        public class Configuration
        {
            private static bool _init = false;

            private static string _categoryRelation;

            private static string _categoryItemRelation;

            private static string _categoryDocumentType;

            public static string CategoryDocumentType
            {
                get
                {
                    if (!_init)
                        Init();
                    return _categoryDocumentType;
                }
                set
                {
                    _categoryDocumentType = value;
                }
            }

            private static int _categoryParentDocumentId;

            public static int CategoryParentDocumentId
            {
                get
                {
                    return _categoryParentDocumentId;
                }
                set
                {
                    _categoryParentDocumentId = value;
                }
            }
            public static string CategoryItemRelation
            {
                get
                {
                    if (!_init)
                        Init();
                    return _categoryItemRelation;
                }
                set
                {
                 _categoryItemRelation = value;
                }
            }

            public static string CategoryRelation
            {
                get
                {
                    if (!_init)
                        Init(); 
                    return _categoryRelation;
                }
                set
                {
                    _categoryRelation = value;
                }
            }
            private static void Init()
            {
                // Load config
                XmlDocument xd = new XmlDocument();
                xd.Load(string.Format("{0}{1}{2}{1}..{1}config{1}refactored.Favourites.config"
                    GlobalSettings.FullpathToRoot, 
                    System.IO.Path.DirectorySeparatorChar, 
                    GlobalSettings.Path));

                Log.Add(LogTypes.Debug, -1, string.Format("Favourites.Configuration: Xml: {0};", xd.OuterXml));

                _categoryDocumentType = xd.DocumentElement.SelectSingleNode("descendant::CategoryDocumentType").InnerText;
                _categoryRelation = xd.DocumentElement.SelectSingleNode("descendant::CategoryRelation").InnerText;
                _categoryItemRelation = xd.DocumentElement.SelectSingleNode("descendant::CategoryItemRelation").InnerText;
                _categoryParentDocumentId = int.Parse(xd.DocumentElement.SelectSingleNode("descendant::CategoryParentDocumentId").InnerText);

                Log.Add(LogTypes.Debug, -1, string.Format("Favourites.Configuration: CategoryDocumentType: {0}; CategoryItemRelation: {1}; CategoryRelation: {2}",
                    _categoryDocumentType, _categoryItemRelation, _categoryRelation));

                _init = true;
            }
        }
  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Nov 23, 2010 @ 14:20
    Robert Foster
    0

    When I create a user programmatically, I use the following code:

                    member = System.Web.Security.Membership.CreateUser(MemberLoginNameTxt.Text.Trim(), password.Password, MemberEmail.Text.Trim());

                    Roles.AddUserToRole(member.UserName, MemberGroupName);

                    var profile = Member.GetMemberFromLoginName(member.UserName);

                    // Add any property values to the user profile here...

                    //Save member
                    profile.Save();

    The last line essentially generates the member xml node, forcing a refresh with the new property values.

    Doubt it's going to make a difference though.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 23, 2010 @ 22:41
    Bo Damgaard Mortensen
    0

    Hi again Robert,

    Sorry for the delayed response, life goes on in the real world ;)

    Thanks for posting your config class, that's pretty neat! However, I was thinking if it was possible to, somehow make a usercontrol for the admins dashboard under a new tab called "Member settings" or something like that, where the admin can set the id's of the nodes if they should change and then grab them in the web.config file. Was just a thought - I don't know if it's too complicated or just plain nonsense ;)

    The only thing I can see that's different from how I'm creating members is that you are using the profile provider where I'm using the old umbraco membership stuff. I'm not sure if that makes a difference really.

    I've tried to fumble around with the programatically created nodes and it seems that if I move the node (manually) after it's been created, even if I move it to the same parent node, it's double disappears. I don't even have to refresh nodes or anything. Quite strange if you ask me. Of course, I have tried to both move the node, republish all and even tried to do it in an AfterPublish event, but still not working.

    /shrug

    Thanks for your help so far though! :) appreciated!

    All the best,

    Bo

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Nov 24, 2010 @ 06:01
    Robert Foster
    0

    Hi Bo,

    You could modify the configuration class to write values back to the XML config file fairly easily, then provide a User Control as an interface to the methods to do that...

    What happens if you actually create the document in a Member.AfterSave event ?  So when the member is created, use that event to create the document and publish it...

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 24, 2010 @ 12:06
    Bo Damgaard Mortensen
    0

    Hi again Robert,

    Thanks for your patience ;)

    Sounds like a good way to handle the node id configuration part! I'll have to look into that.

    I've just tried to make a new test usercontrol where the only thing I do (even from the frontend!) is creating a new node and nothing else. Still, it's showing up twice in the tree. I'm starting to pull out my hair now ;)

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Nov 24, 2010 @ 14:30
    Robert Foster
    0

    instead of using a user control to create the document, what happens if you use events instead?  use the Member.AfterSave event handler to create the document after the member is created...

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 24, 2010 @ 15:09
    Bo Damgaard Mortensen
    0

    I rethinked it a bit and decided to re-install my test-umbraco just to start over clear.

    I skipped the idea of creating a new document when a member is being created and then use the relationship api to relate members to documents. So basically, my create member usercontrol is working now as it should. I then made a new usercontrol to use in the frontend for when members needs to add new documents, here's the code:

     

    protected void btnCreateSpot_Click(object sender, EventArgs e)
        {
            Member m = Member.GetCurrentMember();
            Node parentNode = new Node(1077);
    
            if (Member.IsLoggedOn() && parentNode != null)
            {
                string partnerType = m.getProperty("medlemstype").Value.ToString();
                string partnerGruppe = m.getProperty("medlemsgruppe").Value.ToString();
    
                DocumentType dt = DocumentType.GetByAlias("PartnerSpot");
    
                Document doc = Document.MakeNew("test", dt, User.GetUser(0), parentNode.Id);
    
                doc.getProperty("partner").Value = m.LoginName;
                doc.getProperty("partnerType").Value = partnerType;
                doc.getProperty("partnerGruppe").Value = partnerGruppe;
                doc.getProperty("bodyText").Value = txtSpotText.Text;
    
                doc.Save();
    
                doc.Publish(User.GetUser(0));
                umbraco.library.UpdateDocumentCache(doc.Id);
    
                Document.AfterSave += (se, ea) => 
                {
                    RelationType rt = RelationType.GetByAlias("relateMemberToDocument");
                    Relation.MakeNew(1080, 1050, rt, "Go Further page is now related to Bo Mortensen");
                };
            }
        }
    

    Here i'm trying to create the new document and then relate it to the member that's logged in. As you can see, I even tried to hardcore the id's into the Relation.MakeNew to test it, but I get the error: 

    Server Error in '/' Application.


    Runtime Error

    When hitting the create spot button. It seems like absolutely nothing must happen in the code other than creating a new document.. I get the above error if I try to do anything else :/

    It's getting annoying when something that should be that easy is taking so long time! But I guess patience is a virtue :)

    All the best,

    Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 24, 2010 @ 15:30
    Bo Damgaard Mortensen
    0

    Ok, just when you would think that it's getting strange, the name of the newly created documents is getting copied after each other as you navigate around in umbraco. If I created a document with the name "test" from the frontend and go to the backend, I will see two of the same node with the name "test", if I then go to the developer section and back to the content section, the name of the nodes are "testtest", if I do it one more time, the names will now be "testtesttest"

    ... something is wrong here! :P

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 25, 2010 @ 15:08
    Bo Damgaard Mortensen
    0

    Weird, when I tried today on the live version of the site there was no problem, so everything is behaving just as it should now.

    Thanks for your help and patience Robert! :)

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Nov 25, 2010 @ 16:21
    Robert Foster
    0

    lol - good to hear :)  Was beginning to think there must have been something screwy with your installation...

     

Please Sign in or register to post replies

Write your reply to:

Draft