Overview of SharePoint Controls

Controls are nothing but reusable user interface components that execute when a SharePoint page is requested and render the generated markup to the web browser. SharePoint controls can be classified broadly under the following two categories:

• User controls

• Custom controls or web controls

The major difference between a user control and a custom control is that user controls are much easier to develop due to a high level of visual design support available for these controls. The user interface elements are coded separately using the template markup code and stored in .ascx file. Visual designers such as Visual Studio facilitate the development process by allowing you to drag and drop the standard web controls (such as text boxes, labels, and so on) onto the designer surface. Thus the development process for user controls is similar to that of developing ASP.NET pages, the only difference being the template markup file has .ascx extension in place of .aspx and user controls inherit from the System.Web.UI.UserControl class instead of System.Web.UI.Page class.

Web controls on the other hand contain only the assembly code. The user interface elements are also emitted using the assembly code, contained in a .cs (for C# development) or .vb (for Visual Basic development) file. Due to the absence of separate template markup code for user interface elements, no design time support is available for custom controls.

To better appreciate the features and functionalities provided by user and custom controls this hour looks at the steps involved in creating user and custom controls.

Developing User Controls

As discussed earlier, user controls are developed similarly to ASP.NET page development. In fact they can also be thought of as ASP.NET pages having a .ascx extension. Like the ASP.NET web forms, user controls also have two separate files associated with them, one containing the markup code and the other containing the assembly code.


Did You Know?

You can also merge the markup code and the assembly code to write inline code blocks in the markup file. However, this is generally not considered a good practice, as it reduces code readability.


All the out of the box SharePoint user controls are stored in the TEMPLATECONTROLTEMPLATES folder under the SharePoint root (or the 14 hive). Any user control you develop should also go into this directory.


By the Way

Since the markup code file for a user control (that is, an ASCX file) must be placed under the TEMPLATECONTROLTEMPLATES folder, user controls can be deployed only as farm solutions.


Developing Custom Controls

As discussed earlier, custom controls contain only the assembly code, and the user interface elements are also emitted using the assembly code. Due to the absence of separate template mark-up code for user interface elements, no design time support is available for custom controls.

In the preceding example, notice that we did not write any template markup code to design the control’s user interface. The user interface elements were also created through the assembly code by overriding the CreateChildControls method. Also, there was no design time support when it came to developing the control’s user interface.


Did You Know?

As a rule of thumb, you should develop user controls when the user interface is more or less static, or you need the controls to render a large number of child controls, as in the case of data entry forms containing many form fields. Custom controls are preferable in situations where you want to create dynamic user interfaces and need a lot of control-on-control rendering.


Understanding the Concept of Safe Mode Processing

In the preceding two scenarios, you tested the user and the custom control by adding them to an application page. The process for deploying these controls on master pages and site pages is also similar. You can customize the master page or a site page, add the required registration tag, and then use the control in the page. However, one important additional step is required for using the custom controls on the customized SharePoint master and site pages.

Before discussing this additional step, let’s first have a look at the concept of safe mode processing. By default, the SharePoint security model blocks execution of inline code blocks and unapproved controls in the content served from the content databases. This important security measure prevents users from inserting faulty/malicious code blocks in customized pages and also prevents them from deploying unapproved controls and web parts.

Since the customized pages are served from the content database against the file system, safe mode processing enforces restrictions on the controls that can be added to these pages. The allowed set of controls are called safe controls Therefore to use the earlier developed custom control on the site pages, you must register it as a safe control in your site’s web.config.

If you open your web applications web.config and look for the SafeControls tag, you find entries similar to Figure 5.4.

Image

Figure 5.4. Safe control entries in web.config

To register your custom control as a safe control you can directly edit the web.config shown in Figure 5.4 and add an entry similar to the following:

<SafeControl
     Assembly="[Fully Qualified Assembly Name]"
     Namespace="ImageUploaderUserControlProject.ImageViewerCustomControl"
     TypeName="*" />

Or, as always, you can let Visual Studio do all the work for you. Right-click the ImageViewerCustomControl element in your project and select Safe Mode Entries from the properties window. Click the Add button, as illustrated in Figure 5.5. Deploy the solution and examine the web.config again. You will find that Visual Studio added the required safe control entry into the web.config and now you are free to use the control on the customized site and master pages.

Image

Figure 5.5. Registering a control as Safe using Visual Studio

In case you are wondering why we don’t need to do this for the user controls, examine the web.config again and look for the following safe control entry:

<SafeControl Src="~/_controltemplates/*"
IncludeSubFolders="True" Safe="True"
AllowRemoteDesigner="True"
SafeAgainstScript="True" />

As discussed earlier, the user controls are deployed to the CONTROLTEMPLATES folder under the SharePoint root (or 14 hive). Hence all the user controls are already registered as safe controls and there is no need to explicitly add a safe control entry for them.


By the Way

The concept of safe mode processing is not applicable to the application pages. This is because application pages are served from the file system and not from the content database. Therefore, you can use custom controls on application pages without the need of registering them as safe controls.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.239.77