Tag Archives: Doug Ware

Using Visual Studio 2010 to Create BCS Applications

There are two ways to use Visual Studio to create BCS applications. The first is to build custom BCS models with the Business Data Connectivity Model, the second is to use Visual Studio to migrate declarative models built with SharePoint Designer for deployment via solution packages.

Business Data Connectivity Model

Visual Studio 2010 includes the Business Data Connectivity Model project template that you can use to create and use a .NET Assembly shim to any data store for use by BCS. Solutions based on the project template consist of a feature to install the model in BCS, an XML configuration file that is the model, and .NET classes that do the work of reading and writing data.

The XML model contains all of the information required to work with the .NET classes including method and type descriptors. This means that the associated .NET class’s methods and parameters must match the model.

At this point in the chapter you may have the strong impression that Microsoft really wants people to buy licenses to SharePoint Server if they need BCS. If so, it will not surprise you to discover that you must do some extra work to use this project template with SharePoint Foundation to support deployment to BCS.

Migrating Declarative Models to Visual Studio

You can use the Business Data Connectivity Model project template as a basis to migrate declarative models created in SharePoint Designer. Begin by using SharePoint Designer to export the model. Then create a Business Data Connectivity Model project and remove the default template items. Finally, add the exported model and replace the missing SharePoint Server specific feature receiver to deploy the model to SharePoint Foundation.

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

SharePoint 2010: Developer: Sandbox Solution Overview

SharePoint 2010 includes a secure isolated environment for user deployable Web solution packages—the user code sandbox. To deploy a sandbox solution, a user (usually the site owner), uploads a WSP to a special library that is part of every SharePoint 2010 site named the solution gallery. Once added to the solution gallery, the site owner can activate the solution by clicking a button on the ribbon.

The sandbox makes it easy to create code that runs on a SharePoint server as Web Parts, pages, and event handlers. Code in the sandbox runs under a restricted set of privileges out of process from the web application in a monitored host process called the Windows SharePoint Services User Code Host Service.

The SharePoint object model includes facilities to allow communication between the main worker process and the user code host. The SPUserCodeWebPart Web Part is one of these facilities. SPUserCodeWebPart provides the ability to host controls running in the sandbox on a page running in the main worker process.

In addition to a reduced set of privileges, the sandbox also provides a limited and safe subset of the SharePoint object model. This prevents sandbox code from making any changes outside the current site and from executing with explicit elevation of privileges via the SPSecurity namespace.

Why Use Sandbox?

The sandbox environment gives farm operators the ability to enable customization for users without providing administrative access to the farm. This power comes with a number of safeguards to protect the overall stability and security of the farm.

The sandbox protects the farm from poorly written or malicious code. This includes protection from:

  • Unhandled exceptions
  • Processor intensive operations
  • Unauthorized manipulation of web application and farm infrastructure
  • Elevation of privilege

SharePoint Central Administration and the solution gallery both provide visibility to farm administrators of solution health and resource needs. Administrators can define quotas to block defective solutions and have the ability to manually block execution of specific solutions for any reason.

How the Sandbox Works

The Windows SharePoint Services User Code Host Service provides a partial trust AppDomain to host sandboxed processes. The service consists of three parts:

  • SPUCHostService.exe
  • SPUCWorkerProcessProxy.exe
  • SPUCWorkerProcess.exe

SPUCHostService manages one or more SPUCWorkerProcess via SPUCWorkerProcessProxy. This architecture makes it possible to scale the user code sandbox across multiple servers in the farm. Solutions in the sandbox use a special version of Microsoft.SharePoint.dll located in the UserCode\Assemblies folder in the SharePoint root.

The host service also allows configuration of server affinity—you can specify that sandbox code runs on the same machine as a request or that requests to run sandboxed code are run on available servers with the Sandboxed Code Service. Regardless of the configuration the host service runs sandbox code within SPUCWorkerProcess.exe. SPUCWorkerProcess.exe is the process to which you attach the debugger to debug sandbox code.
doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

SharePoint 2010: Master Pages in SharePoint 2010

An installation of SharePoint 2010 contains many master pages. There are master pages that preserve backward compatibility for sites migrated from WSS 3.0 or MOSS 2007 including the old master page for Team sites, default.master, and master pages for application pages including simple.master and application.master.

There are also new master pages that support the new look and feel of SharePoint 2010. These are the v4 master pages and generally speaking, there is a v4 master page that corresponds to the old master pages. For example, v4.master is the replacement for default.master and simplev4.master is the replacement for simple.master.

Master Page Locations

You can find SharePoint’s various master pages in a few different locations depending on the master page and the context of its use. Within a SharePoint site, master pages are in a document library called the Master Page Gallery. The URL for this library relative to the Web’s URL is _catalogs/masterpage.

When you create a site, SharePoint provisions the appropriate master pages from the SharePoint root to the Master Page Gallery. The stock master pages that deploy to the Master Page Gallery from the SharePoint root are in Template\Global. However, there are additional master pages that support various application and administration pages that never deploy to a site located in Template\Layouts and in Template\Admin.

Often you can find additional custom master pages in the SharePoint root as part of features that deploy master pages via a Module feature element or as part of a custom site template.

Associating Master Pages with Content Pages

You can associate a master page with a content page in several ways. Some of these are dynamic and resolve at runtime to the SPWeb’s MasterUrl or CustomMasterUrl property, while others are static.

To create a dynamic association, use one of the following tokens as the value of the MasterPageFile attribute in the Page directive.

  •  ~masterurl/default.master
  •  ~masterurl/custom.master

Figure below shows the ~masterurl/default.master token in action in SharePoint
Designer. In this case it resolves to /_catalogs/masterpage/v4.master, but you
could change the SPWeb.MasterUrl property; the master page would change
without modifying the content pages.

Masterpagesimg1

Alternatively, you can specify the exact master page you want your content page to use with one of the static tokens.

  •  ~site/ActualFileName.master
  •  ~sitecollection/ActualFileName.master

The page parsers interpret both sets of tokens at runtime, but it is also possible to control the selection of a master page dynamically with .NET code in a page’s OnPreInit method or by using an HttpModule.

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

Microsoft SharePoint: Custom Styles

Themes are sufficient for scenarios that require only the application of colors, but to exercise total control over styling you must create and use custom style sheets. There are a variety of ways to include custom style sheets in your SharePoint pages. Each has advantages and disadvantages.

The first is to include a standard HTML <link> element in a site’s master page, in a single page, or in a control. As this is basic HTML it requires no specific knowledge of SharePoint and it works in both farm and sandbox deployments. The disadvantage to this approach is that it is inflexible—the link URL will not change as you change the site’s location nor does it support any other SharePoint functionality like themes.

Another approach is to set the AlternateCssUrl property of the SPWeb class. This technique works in both farm and sandbox deployments. When the AlternateCssUrl property contains a value, a web control present in the v4.master page, CSSLink, renders an HTML <link> element containing the value of the AlternateCssUrl property after it renders the links for the core SharePoint style sheets.

Because CSSLink renders the AlternateCssUrl after the core styles you can be certain that the browser will apply your styles last. This is important because the order in which style sheets appear in a page determines which rules apply. Because the application of AlternateCssUrl appears after the core styles, you can use it to override and replace individual rules from the core styles.

You can specify any valid URL as the value of AlternateCssUrl. The only disadvantage to this approach is that it applies the target style sheet to every page in your site. Therefore, for styles that affect only a small portion of your site, like an individual control, you may choose a more targeted approach for performance reasons.

The CssRegistration web control provides the ability to add CSS to a page as needed for a given scenario. You can apply the control in both farm and sandbox solutions to any artifact that includes design time markup, i.e., pages and user controls. However, CssRegistration also includes a static method for use by Web Parts. Unfortunately, this is only available in farm deployments. CssRegistration allows the inclusion of CSS from specific locations including:

  • TEMPLATE\LAYOUTS\[LCID]\STYLES
  • Style Library
  • Absolute path

Unless you specify an absolute path, SharePoint automatically accommodates any changes to the site’s location providing flexibility that a <link> element lacks. CssRegistration also allows application of themes to style sheets whereas <link> does not.

It is important to know that the CssRegistration web control does not render anything at runtime. Instead it works in conjunction with the CSSLink control.

CssRegistration and CssLink

As the name implies, the CssRegistration control registers CSS for rendering by CssLink. If multiple items register the same CSS, for example two instances of the same Web Part, CssLink renders a single link. CssLink renders registered CSS in alphabetical order or based on the optional CssRegistration After property.

The After property is a new and welcome addition in SharePoint 2010. Also new is the ability to render links conditionally via the optional ConditionalExpression and RevealToNonIE properties. For example, to render a style sheet for only IE7 you can specify IE 7 as the value for ConditionalExpression.

The CssLink control renders more than just the values specified by CSSRegistration. CssLink renders links in the following order:

  • CssRegistrations
  • Core styles
  • AlternateCssUrl
  • DefaultUrl (Optional CssLink property value)

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

SharePoint Client Object Model – Microsoft SharePoint

A typical SharePoint site contains much information that is potentially useful to other systems. SharePoint provides a number of APIs that other systems can use to access and manipulate that information. SharePoint Foundation 2010 includes a new set of libraries, the SharePoint client object model, which client applications can use to access SharePoint’s core services.

The SharePoint client object model is a set of managed code and JavaScript libraries that support traditional .NET assemblies and executables, Silverlight, and browser-based JavaScript. In the past, you had to work with SharePoint via SOAP-based Web services. These services consume and emit XML that the client must build and parse. The client object model does not require you to build and parse XML nor is it based on SOAP. Instead you use classes that are very similar to the ones you use when writing SharePoint code on the server.
These classes act as proxies to a WCF service.

Problems Addressed

The client object model is actually a set of client and server proxies to a WCF service. However, you do not have to understand the WCF service because the client object model takes care of all of the tedious details of connecting to and communicating with the WCF service.

This is a major improvement over the *.asmx (SOAP) services found in WSS 2.0 and WSS 3.0. To use the SOAP services you must understand SOAP, build and parse XML, and most importantly, understand the specific service. Since these services consume XML, knowledge of the SharePoint object model is only slightly helpful to understanding a given service. Each of the services is a program with its own rules. Understanding how to write code to add an item to a list doesn’t mean you know the specific SOAP service endpoint or how to
format XML that the service understands.

Apart from being complex, the SOAP services are inefficient. Accomplishing most tasks requires multiple round-trips to the server and multiple service endpoints. Those who use hosted SharePoint and can’t deploy code to the farm have no choice and must use the services if they need integration. However, due to the complexity and learning curve of the traditional services, most people build and deploy custom services that support specific use cases to integrate earlier versions of SharePoint with other applications. The traditional SOAP based services are still supported, but unless you are writing code to target both WSS 3.0 and SharePoint Foundation you should use the client object model.

Client Object Model Overview

When you use the client object model you work with classes and methods. There is still XML, but you don’t see it or work with it. The client object model builds the XML based on your work. When it is time to do work on the server, the client object model passes the XML to the client.svc WCF service endpoint. SharePoint does whatever work is indicated by the XML and returns a JSON (JavaScript Object Notation) encoded response to the client object model which updates the state of any object instances as necessary. Figure 1 shows a conceptual view of the communication between the client and the server.

Sharepoint_Client_object

Figure 1.The client object model and client service handle all communication and message formatting for you.

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

Create Child Controls – Microsoft SharePoint 2010

Most web controls that have visual elements compose other controls into a greater whole. Every web control contains a Controls collection property that contains the controls of which it is composed.

The controls in the Controls collection are called child controls because a page is easily visualized as a tree of controls with the page as the root. Each node of the tree can have zero or more children.

Use the CreateChildControls to instantiate any child controls that your web control contains.

The first two things that happen when ASP.NET renders a page are the initialization and loading of each node of the tree. The next thing that happens is the loading of state into each node of the tree so that the tree correctly represents any user input.

State is stored in a hidden form field named __VIEWSTATE and its contents are used to rehydrate a page between postbacks. The loading of ViewState works only if ASP.NET is able to match each node in the current rendering with the previous version—the one with which the user interacted and submitted input.

Consider the following code snippet:

 

If you ran this code in ASP.NET 2.0, perhaps on a WSS 3.0 or MOSS 2007 site, the Web Part would render a button 50 pixels wide without error. However, the event handler will not fire when a user clicks the button because the MyWebPart class creates a new instance of the Button class each time it initializes. The myButton variable then references a new button.

When the ViewState loads to indicate that the user clicked a button, the new button has replaced the original button in the tree and the event doesn’t fire.

Here’s the correct way to write this code:

 

In this example, you are still creating a new instance each time—ASP.NET is stateless after all, but the timing of the operation allows the page rendering infrastructure to correctly associate the button with its state when it adds the button to the Controls collection.

When working with the page rendering life cycle, timing is everything. ASP.NET 3.5, the basis of SharePoint Foundation, is more forgiving in this regard. However, unless you know that your custom Web Part will never run in an older version of ASP.NET, it is a best practice to follow the simple rule—create your child controls inside CreateChildControls.
doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

Lists and Libraries in the Object Model – Microsoft SharePoint 2010

The functionality that features provide to define and create lists is useful, but the static nature of XML means that you need more tools at your disposal to fully exploit the power of lists and libraries. Fortunately, just as you’d expect, the core SharePoint assembly contains a robust set of classes to allow automation of lists and their contents.

As you explore these classes, you may find yourself in familiar territory: The names may be new, but the techniques closely resemble those that you use to manipulate any .NET collection. The techniques for adding, editing, and deleting list items resemble those for every data access stack from Microsoft since Windows development first became popular in the 1990s.

In fact, the only unusual aspect of working with SharePoint lists and libraries is the query syntax which you will see in a later section.

You can accomplish a great deal in code with a relatively small number of
classes:

  • SPListCollection
  • SPList
  • SPDocumentLibrary
  • SPListItemCollection
  • SPListItem
  • SPListView
  • SPContentType
  • SPField

Each SPWeb instance exposes an SPListCollection via the Lists property that contains all of the SPList instances in the web. Some of the SPList instances are document libraries and are actually of type SPDocumentLibrary. Each SPList and SPDocumentLibrary exposes its items via the Items property, which is of type SPListItemCollection. Each item is an SPListItem.

The other classes, SPListView, SPContentType, and SPField represent a list’s presentation and structure.

Retrieving List Instances

If you need to access a list within a page context, perhaps as part of a custom list form, you can get the current List or ListId from SPContext.Current. In other situations, feature event receivers for instance, you access a list instance via the containing web.

Lists and libraries are members of a specific web. Therefore, instances of the SPWeb class expose a property named Lists that returns an instance of SPListCollection that you can use to enumerate all of the lists in the web.

The SPListCollection Item property has three overloads that you can use to retrieve a specific list instance. One takes the list’s GUID, one takes the list’s name, and the other takes the list’s ordinal position in the collection.

Alternatively, you can use the SPWeb.GetList method to retrieve a list based on its URL. This method is especially versatile because the URL can point directly to the list or to any of its forms using an absolute or site relative format.

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

Core Foundation Assemblies

Before you can write any managed code for SharePoint you must reference the assembly that contains the classes you need. Although there are many assemblies in a SharePoint installation, there are a few that you will use often. If you are using one of the SharePoint project templates in Visual Studio 2010, the appropriate references are usually preconfigured when you create the Visual Studio solution.

The most commonly used assemblies are in the SharePoint root within the ISAPI folder although there are many located in other places, especially in the global assembly cache. Some of the most commonly used assemblies are:

  • Microsoft.SharePoint: The core classes for SharePoint Foundation.
  • Microsoft.SharePoint.Client: The core classes for client applications using the managed client object model.
  • Microsoft.SharePoint.Linq: LINQ to SharePoint.
  • Microsoft.SharePoint.WorkflowActions: SharePoint specific workflow actions.

Core Classes

The classes that represent the elements common to all SharePoint sites are in the Microsoft.SharePoint assembly. Figure 15 shows a few of these classes. If you have taken the time to become familiar with SharePoint as a user, the purpose of most of these should be obvious to you at this point—SPSite is a site collection, SPWeb is a web, and so on.

corefoundationimg1 Some of the commonly used classes in Microsoft.SharePoint.dll.

SPContext

SPContext is the only class shown in figure above that doesn’t represent an item in a SharePoint site. You use this extremely handy class when writing code that runs when SharePoint renders a page, for example in a Web Part or user control, to gain access to the current request. You can use SPContext . Current to get access to the current:

  •  Site
  •  Web
  •  List
  • ListItem
  •  More…

It is important to note that SPContext is always governed by the security context of the user that is requesting the page. You can get the current user programmatically via the CurrentUser property of the SPWeb class. For example:

SPUser currentUser = SPContext.Current.Web.CurrentUser;

Because SPContext.Current uses the security context of the current user your code will throw a security exception if it attempts to perform any operations that the user does not have permission to perform.

Common Conventions

As you saw in Figure above, it is easy to guess the names of most of the core classes—just add an SP to the front of the name. For example, it should come as no surprise that the class you use to work with an alert is SPAlert! Other common conventions include:

  • An item’s name is usually the Title property such as SPWeb.Title and SPList.Title.
  • Most collection indexers provide the following overloads for accessing collection items:
    • Ordinal index
    • Guid
    • Title (if applicable)
  • Most changes to property changes only persist when you call the object instance’s Update() method.

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

Project Templates

The first thing you do when building a new solution in Visual Studio 2010 is to select a project template. The template you select determines the tools that are available and how Visual Studio behaves when you build the project. Visual Studio comes with project templates for a variety of Windows and Web application types. Among these are a number of templates for building SharePoint applications.

Several of the project templates concern development of specific types of features. Each of these is a starting point that allows you to add any type of feature, but that starts with an element manifest that corresponds with the project template’s name. The feature oriented project templates are:

  •  List Definition
  •  Content Type
  •  Module
  •  Event Receiver

Workflows are an important component of most SharePoint environments. There are five workflow specific project templates for SharePoint, two of these support workflow development for SharePoint 2007. The SharePoint workflow templates are:

  •  Sequential Workflow
  •  State Machine Workflow
  • Import Reusable Workflow
  •  SharePoint 2007 Sequential Workflow
  •  SharePoint 2007 State Machine Workflow

The SharePoint 2007 workflow templates are the only direct support Visual Studio offers developers writing code for SharePoint 2007. All of the other project templates support only SharePoint 2010.

The remaining SharePoint project templates are:

  • Empty SharePoint Project: An empty project with SharePoint tools.
  • Visual Web Part: A user control and a Web Part wrapper with a feature to add the Web Part to the deployment target.
  • Business Data Connectivity Model: A project with tools for building Business Connectivity Services applications.
  • Site Definition: A project pre-populated with files for a custom site definition.
  • Import SharePoint Solution Package: A project with imported contents from a WSP usually generated via the Save Site as Template functionality of a SharePoint site.

Most large solutions start with the Empty SharePoint Project template.

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.

Types of Features

Most of the elements in a SharePoint installation are either defined using features or leverage features in some way. Accordingly, there are feature types that define most of the site elements with which you are already familiar.

Feature/Site Template Association

Feature/site template associations allow you to associate new features and functionality with existing site definitions or site templates so that, when you provision a site, SharePoint automatically activates the features. This technique is known as feature stapling.

Feature stapling makes versioning much easier because it allows you to associate new features with preexisting site definitions. Stapled features also provide an excellent way to handle situations that require activation code, because they are applied after their associated site is fully provisioned.

Two features are involved in feature stapling: the feature that contains the functionality you want to add to an existing site template, and the feature/site template association feature. Feature/site template associations have Farm or WebApplication scope.

You can globally associate a feature with every site definition on a farm. Associate a feature with the global site definition by specifying GLOBAL for the TemplateName attribute. Whenever you create a site, the global site definition is applied, so your feature is also applied.

Workflow

Use a workflow feature to attach a custom workflow to SharePoint. The feature includes the code for the workflow and the necessary forms for each step.

Document Converter

Document converters automatically convert a document library item from one format to another. WSS does not offer document converters, but MOSS includes several (for example, to convert documents to HTML) and third-party converters to support conversion to PDF and other formats.

Site Columns and Content Types

In features, you define site columns with a Field element. All of the core site columns are defined in a feature named fields that is activated by the global site definition. You can use the fields feature as a reference when creating your own site columns or content types.

Just as the global site definition activates the fields feature for site columns, the ctypes feature is also activated globally for content types. You can create a new content type with a feature using the ContentType feature element.

Sometimes, you will need to associate a content type with an existing list or alist that another feature created as part of a larger solution. Use the ContentTypeBinding feature type for that task.

Lists

You can define a new type of list—including content type associations, views, and custom forms—with a ListTemplate feature. Each of the standard lists and libraries is defined by a ListTemplate feature element. For example, the DocumentLibrary feature defines the Document Library template.

You can create new list instances by using ListInstance to specify the target URL and the list template that defines the list.

SharePoint exposes a rich event model for lists that allows custom code to run whenever something happens to a list or an item. Use the Receivers feature type to associate the assembly and a class that contains a handler for a specific event type to all lists defined by a particular list template.

Module

The Module feature allows you to deploy files to a site. You can use it to add files to a library or simply place the files onto the site in a specified folder. In the latter case, the module creates the folder if necessary.

Control

You might expect the Control feature to install controls, but it doesn’t. Many of the core site pages use a Web control named DelegateControl. DelegateControl is a placeholder; when the page renders, it loads a concrete control instance that is based on an ID and a sequence number. DelegateControl looks for the lowest sequence number of a given ID and loads
the associated control. Individual Control features define the IDs and sequence numbers.

This feature allows site owners to override existing controls with their own specialized instances on existing sites, without altering the sites or their definitions. A common use of this technique is to replace the basic search control, which is defined by a DelegateControl on the master page with an ID of SmallSearchInputBox. You can replace this control when you brand a site by deploying your own control or Web Part and then creating a Control feature with the SmallSearchInputBox identifier and a low sequence number.

Custom Actions

Just as Control allows you to insert controls in defined placeholders without changing the target site or pages, the Custom Actions feature allows you to insert or hide links on pages and menus. CustomAction and HideCustomAction both require that you specify a location, and they affect the resulting page without directly altering its definition.

Use Custom Actions to show or hide different elements of the site, including the contents of many drop-down menus such as site actions, personal settings, list settings, and item edit menus. You can also use Custom Actions to show and hide links on the Site Settings page and on the Create Libraries, Lists, And Sites page. Custom actions enable greater control of additional navigation within the site than you can accomplish with the top-level menus.
You can use custom actions to display an option on the edit item menu of a list or document library’s items. For example, you could display a link to a function that merges a contact item’s data with a document template to create a form letter.

New Feature Elements in 2010

SharePoint 2010 adds a number of feature elements to the schema.

Property Bag

The PropertyBag element allows you to add properties to items in a site. The Save Site as Template functionality uses PropertyBag to create solutions that can duplicate the source site with high fidelity.

When using a PropertyBag to target a file, the use is identical to the Properties child element of the File element within a module. The difference is that the PropertyBag is independent of the need to deploy the file. In other words, you can apply properties to items that already exist.

Web Template

The WebTemplate element provides an alternative to traditional site templates. In most cases this element is now the preferred method for creating new templates for custom sites.

Workflow Association

The WorkflowAssociation allows declarative association of a workflow template to a list, content type, or site. Previously you had to use managed code to create a workflow association. Managed code is still a fully supported option.

Others

The feature schema definition, wss.xsd, includes four other workflow elements that are currently undocumented and unused by any of the built-in features that come with SharePoint 2010. They are:

  • Workflow Actions
  • Web Part Adder Extension
  •  User Migrator
  • Group Migrator

Doug (SPC  towards the end of the smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.