SharePoint Security - ARB SEcurity Solutions
Site Blog Home About ARBBlog SharePoint Dev Center Security Labs Contact
Developing A Form WebPart - Part 2 - Declare Variables, Assign Controls, And Set up Exception Management


Once the class is setup for the Form WebPart, you need to declare relevant variables and instantiate the required controls by overriding the CreateChildControls() method. For the Forms WebPart, which only accepts an ID input, the only required controls are a TextBox control and a new button that is wired to a submission event.

        // Current Site
        SPSite siteThis = null;
 
        // Current Web
        SPWeb webThis = null;
 
        // Text Box Control
        private TextBox txtTextBox;
 
        // Button Control
        private Button save;
 
        // String Build In Order To Parse Relevant Errors
        private StringBuilder msgError = new StringBuilder();
 
        // SQL connection variable
        SqlConnection sqlConn = null;
 
        // Validator Control to Verify User Input
        private RequiredFieldValidator fieldVal;

There are two best practices to consider when starting to script a piece of functionality such as the Forms WebPart. Firstly, it is advantageous to build a reference to the current site and web since this can be passed on the OnInit method in order to harvest the current site and web context by using SPControl.GetContextSite(Context). In terms of exception management, organizations typically vary in whether there are enterprise standards such as using the trapping logic out of the Microsoft Exception Handling Enterprise Library or a custom common exception library. If managing individual loose exceptions, use a class available StringBuilder() in order to append the string exception text by using (stringbuildervariable).Append within the relevant try/catch blocks.

Next, the controls have to be declared by overriding the CreateChildControls() method. The two controls that need to be instantiated are the TextBox control to accept the ID input from the user and the button control that is wired to an event handler to manage the data submission. This is accomplished by initializing each of the controls, and then call the add method out of the Controls property to add it to the WebPart output.

Wrapping this in a try/catch block, it is noticeable that the StringBuilder() being used to manage the exceptions is being called in the catch block if there is an error that occurs in the CreateChildControls().

 
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                // Call a new text box control
                txtTextBox = new TextBox();
                txtTextBox.ID = "txtTextBox";
                Controls.Add(txtTextBox);
 
                // Call a new button control and bind it to an event 
// handler to submit the data to a SQL backend when
// complete
                save = new Button();
                save.Text = "Save";
                save.Click += new EventHandler(save_Click);
                Controls.Add(save);
            }
            catch (Exception ex)
            {
                msgError.Append("Error in CreateChildControls: " + ex.Message);
            }
 
        }

If more controls were required for data entry, such as using a ListBox control with relevant bound list items, the relevant variables need only be declared and the control added in the same fashion.
        private ListBox lbListBox
 
        // Under base.CreateChildControl() in the CreateChildControls Method
        lbListBox = new ListBox();
        lbListBox.Rows = 1;
        lbListBox.Items.Add(new ListItem("my lbListBox Item"," my lbListBox Item "));
        Controls.Add(lbListBox);




[ Go Back ]
Content ©
 MVP Remote Development

 MVP -- WSS




 TechNet Article

Read my article "7 New Features That Enhance Security In SharePoint" published in the Janurary issue of TechNet magazine Read Now


 Steps To SharePoint Security

Implement Internal SharePoint Security Model

Harden Your Environment With Tools and Policies

Monitor and Supervise With Server Utilities


 SharePoint Security Articles
The Definitive Guide To MOSS Pluggable Authentication Providers
The Active Directory Membership Provider and SharePoint Introduction
Introduction to and Building an ASP.NET 2.0 Custom Session State Provider
Considerations for Security Relating To Configuration Elements
Introduction to Microsoft Office SharePoint Server and WSSv3 Trust Levels and Code Access Security
Example Attack on SharePoint With Chunked Encodes and Overflow

© 2006 ARB Security Solutions, LLC
ARB Security Solutions is not affiliated with or endorsed by Microsoft Corporation.
SharePoint is a trademark of Microsoft Corporation.     Legal Notices | Privacy
SharePointSecurityFooter