Modify custom properties toolpart order

05/10/2012 23:23

 

//Override GetToolParts method in web part class   
 
public override ToolPart[] GetToolParts()
        {
            //Get all toolparts
            ToolPart[] toolparts = base.GetToolParts();
 
            // resize the tool part array 
            //ToolPart[] toolparts = new ToolPart[4];
            // instantiate the standard SharePopint tool part 
           // WebPartToolPart wptp = new WebPartToolPart();
            // instantiate the custom property toolpart if needed. 
            // this object is what renders our regular properties. 
            //CustomPropertyToolPart custom = new CustomPropertyToolPart();
            // instantiate and add our tool part to the array. 
            // tool parts will render in the order they are added to this array. 
            //toolparts[1] = new LukasToolPart();
            //toolparts[2] = new LukasToolPartCheckBox();
            //toolparts[3] = custom;
            //toolparts[0] = wptp;
 
            //Set our 'properties' toolpart as first
            return SetToolpartPosition(toolparts, 1);
 
        }
        
        /// <summary>
        /// Sets custom properties toolpart position toolparts array
        /// </summary>
        /// <param name="toolparts">array of toolparts(from wp)</param>
        /// <param name="position">desired position</param>
        /// <returns>array of ToolParts</returns>
        public ToolPart[] SetToolpartPosition(
            ToolPart[] toolparts,
            int position)
        {
            int index = position - 1;
            if ((index >= 0) && (index <= toolparts.Length))
            {
                ToolPart oldPart = toolparts[index];
                ToolPart newPart = Array.Find<ToolPart>(toolparts, isToolpartName);
                //if any toolpart 'Properties' found, no order change
                if (newPart != null)
                {
                    int newPartIndex = Array.IndexOf(toolparts, newPart);
                    toolparts[index] = newPart;
                    toolparts[newPartIndex] = oldPart;
                }
            }
          //if index is not valid, no toolpart order change
            return toolparts;
        }
        private bool isToolpartName(ToolPart toolpart)
        {
            if (toolpart.Title == "Properties")
                return true;
            else
                return false;
        }