Copied to clipboard

Flag this post as spam?

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


  • Peter Alcock 113 posts 176 karma points
    Feb 28, 2013 @ 14:32
    Peter Alcock
    0

    Document by node name

    Hi all,

    Trying to get the hang of the API stuff and new to C# also to be honest.

    I have the below script which when clicked updates a node by it's ID (contained in Textbox value) which works great but instead of updating the nodes pagetitle by the nodeId i want it to do it by finding the node by its nodename if that makes sense?

     protected void Button2_Click(object sender, EventArgs e)

        {

            int numVal = Int32.Parse(TextBox2.Text);

            Document doc = new Document(numVal);

            // Get the properties you wish to modify by it's alias and set their value

            // the value is saved in the database instantly!

            doc.getProperty("pageTitle").Value = TextBox1.Text;

            // After modifying the document, prepare it for publishing

            User author = User.GetUser(0);

            doc.Publish(author);

            // Tell umbraco to publish the document so the updated properties are visible on website

            umbraco.library.UpdateDocumentCache(doc.Id);

            //umbraco.library.PublishSingleNode(doc.Id); - outdated

    }

    So it's the Document doc = new Document(numVal); which find the node by id, how can i get it to find the node by its nodeName which in this case happens to be 'harvey'.

    Thanks

  • Peter Alcock 113 posts 176 karma points
    Feb 28, 2013 @ 14:33
    Peter Alcock
    0

    The complete code is this by the way if it helps

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using umbraco.BusinessLogic;

    using umbraco.cms.businesslogic.web;

     

     

     

     

    public partial class usercontrols_WebUserControl : System.Web.UI.UserControl

    {

     

     

        protected void Page_Load(object sender, EventArgs e)

        {

            ProfileOne.HRef = "../" + Page.User.Identity.Name;

     

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            //Get the type you would like to use by its alias 

            //and the user who should be the creator of the document 

            DocumentType dt = DocumentType.GetByAlias("Profile");

            User author = User.GetUser(0);

     

            //create a document with a name, a type, an umbraco user, and the ID of the document's parent page. 

            //To create a document at the root of umbraco, use the id -1 

     

            Document doc = Document.MakeNew(Page.User.Identity.Name, dt, author, 1232);

     

     

            doc.getProperty("pageTitle").Value = "my test title";

            doc.getProperty("metaDescription").Value = "This is my lovely meta description.";

            doc.getProperty("umbracoNaviHide").Value = "1";

     

     

            //after creating the document, prepare it for publishing 

     

            doc.Publish(author);

     

            //Tell umbraco to publish the document

            umbraco.library.UpdateDocumentCache(doc.Id);

            //umbraco.library.PublishSingleNode(doc.Id); - obsolete

     

            Literal1.Text = "Your profile has been created " + Page.User.Identity.Name + " with reference number " + doc.Id + doc.Text;

        }

        protected void Button2_Click(object sender, EventArgs e)

        {

            int numVal = Int32.Parse(TextBox2.Text);

            Document doc = new Document(numVal);

     

     

     

            // Get the properties you wish to modify by it's alias and set their value

            // the value is saved in the database instantly!

            doc.getProperty("pageTitle").Value = TextBox1.Text;

     

     

            // After modifying the document, prepare it for publishing

            User author = User.GetUser(0);

            doc.Publish(author);

     

            // Tell umbraco to publish the document so the updated properties are visible on website

            umbraco.library.UpdateDocumentCache(doc.Id);

            //umbraco.library.PublishSingleNode(doc.Id); - outdated

     

     

        }

    }

  • Peter Alcock 113 posts 176 karma points
    Feb 28, 2013 @ 15:45
    Peter Alcock
    100

    Nevermind i got it! heres my solution if anyone else needs it! it looks for any node which its name equals to the logged in users name is so you can only update that node.

    var specificNode = new Document(1232);

            var childNodes = specificNode.Children;

     

            foreach (var node in childNodes)

            {

                if (node.Text == Page.User.Identity.Name)

                {

    Actions here i.e update pagetitle

         }

            }

Please Sign in or register to post replies

Write your reply to:

Draft