Rename and sorting nodes with an actionhandler Options
jesper
Posted: Monday, March 12, 2007 9:37:54 AM

Rank: Administration

Joined: 7/25/2006
Posts: 425
Location: vipperoed, denmark
This Actionhandler keeps nodes nicely named and sorted. The basic idea is that the webauthor creates a lot of nodes. In this case it's calendarbookings. The webautor had trouble with naming them and moving them around.

So when she creates a new node and publish it - this bit of code does the following for her:

1. Rename the node to reflect the value of one of the properties (a date value)
2. Sort the nodes (in Umbraco).
3. Republish


Now .. I'm crossing my fingers hoping that the "smilies from Hell" won't show their hate :cry:
(translate: that the code won't be messed up with smilies)

Code:

   public class RenameAndSort : IActionHandler
    {
        // struct used for sorting
        public struct MyStruture : IComparable
        {
            public Int32 iID;
            public String sName;

            public Int32 CompareTo(Object obj)
            {
                MyStruture tmpObj = (MyStruture)obj;
                return (this.sName.CompareTo(tmpObj.sName));
            }
        }
         
        /// <summary>
        /// Sort and publish all siblings and parent node
        /// </summary>
        /// <param name="d">one of the siblings</param>
        private void SortThisOut(Document d) {

            // get all siblings into a array

            ArrayList a = new ArrayList();
            foreach (umbraco.cms.businesslogic.CMSNode c in d.Parent.Children)
            {
                MyStruture m = new MyStruture();
                m.iID = c.Id;
                m.sName = c.Text;
                a.Add(m);
            }

            // sort the array
            a.Sort();

            // get all the nodes and change their sort order. publish them ..
            for (int i = 0; i < a.Count; i++) {

                // out of array
                MyStruture m = (MyStruture) a[i];

                // new sort order
                new umbraco.cms.businesslogic.CMSNode(m.iID).sortOrder = i;

                // update cache
                library.PublishSingleNode(m.iID);
           
            }

            // seems like it's needed to update the cache for the parent node .. sometimes ...
            library.PublishSingleNode(d.Parent.Id);
       
       
        }


        public bool Execute(Document documentObject, IAction action)
        {
            try
            {
               
                // Only work on one document type
                if (documentObject.ContentType.Alias == "CalenderItem")
                {

                    // the node items is named from a field/property named weekStartDate which is a datetime type.
                    DateTime dt = (DateTime)documentObject.getProperty("weekStartDate").Value;
                    string dttext = dt.ToString("yyyy-MM-dd");


                    // compare to prevent an endless republish mayhem
                    if (!documentObject.Text.Equals(dttext))
                    {
                        documentObject.Text = dttext;

                        documentObject.Publish(documentObject.User);
                        library.PublishSingleNode(documentObject.Id);

                        // sort siblings and publish all
                        SortThisOut(documentObject);

                     

                    }
                }

            }
            catch (Exception err)
            {

                // TODO
                string msg = err.Message;
            }

            return true;

        }

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

        public IAction[] ReturnActions()
        {
            return new IAction[] { new ActionPublish() };
        }



    }


Kindly,

Jesper

webbureau jesper.com doing webdesign / development / umbraco implementations / 2007&2008 MVP / umbraco certified
duckie
Posted: Tuesday, March 13, 2007 11:39:55 AM
Rank: Devotee

Joined: 8/14/2006
Posts: 212
Thats pretty nice!

I was doing something likely in a webservice (called from a win-form), but the sortorder just doesnt want to catch... Thats the reason you do a publish on the parent, right? (dont know why i didnt try that)

Signatures suck. No reason to have one.
jesper
Posted: Tuesday, March 13, 2007 1:36:55 PM

Rank: Administration

Joined: 7/25/2006
Posts: 425
Location: vipperoed, denmark
AndersM wrote:

Thats pretty nice!

I was doing something likely in a webservice (called from a win-form), but the sortorder just doesnt want to catch... Thats the reason you do a publish on the parent, right? (dont know why i didnt try that)

The parent? Yes - that's why. It's the same in Umbraco admin when you sort some nodes. To get the sorting published I sometimes have to unpublish/publish the parent node.

Kindly,

Jesper

webbureau jesper.com doing webdesign / development / umbraco implementations / 2007&2008 MVP / umbraco certified
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.