Chapter 3. Data provisioning

The previous chapters showed you how many Microsoft SharePoint solutions rely on lists of items that contain data, such as contacts, files, and so on. When you develop a SharePoint solution, therefore, one of your main tasks is to provision data structures for these lists of items. In fact, whenever you need to develop a reusable and maintainable solution that will reside on many different site collections and has many different customers, you should formally define the data structures that you will use. Simply designing them through the SharePoint visual design interface from a web browser might seem easy (any end user can do it), but in the long run it will become a source of confusion. Formal definitions can be reused many times in multiple sites and can be versioned. Meanwhile, data structure definitions made manually through the visual design interface are difficult to reuse and can lead to duplication of definitions in multiple sites. Also, when you create SharePoint apps hosted on SharePoint, you can use the data model of lists and items provided by SharePoint for storing data and content related to your apps.

Note

Within the context of this book, the term data structure refers to the formal definitions of custom list definitions, content types, and site columns. Such formal definitions help to ensure data consistency across lists and sites.

This chapter explores the rules for custom lists and the tools that SharePoint 2013 provides to create them. To learn how these tools behave in a real-world scenario, you will investigate how to define a custom list of contacts that can use custom forms and can be browsed through specific list views. The list in this case study will be based on two content types: Customer and Supplier.

Site columns

The first and main step in provisioning a custom data structure is to define site columns. A site column describes a reusable data type model that you can use in many different content types and list definitions, across multiple SharePoint sites. Unless you have never used SharePoint at all, you will have already defined many site columns using a web browser, within the appropriate section of the Site Settings page. To create a more flexible and reusable solution, you can also define a site column using some XML code, which in SharePoint is called a feature element.

More Info

For further details about features and feature elements, read Chapter 11

A simple site column defined in a feature element shows a very simple site column definition for a Text column that contains the company name of the sample contact.

Aside from the Elements tag itself, which is simply a container element, the interesting part of the preceding column definition is the Field element. The most important feature of this element is the ID attribute, which is a globally unique identifier (GUID) that uniquely identifies the site column. You can use the ID attribute to reference this specific site column everywhere. Notice that you can create unique GUIDs by using the GUIDGEN tool provided with Microsoft Visual Studio 2012.

A simple site column defined in a feature element declares that the Company Name column will have an internal Name attribute of DevLeapCompanyName. Name is a required attribute, and like the ID attribute, it should also be unique, because it provides an alternative way to exclusively reference the column from code. In general, this example uses the developer’s company name value as a prefix to better ensure the uniqueness of this name. The Name attribute value cannot contain spaces or any characters other than numbers (0 through 9) and letters (a through z and A through Z). Any other characters will be converted into the corresponding hexadecimal representation. For example, if you want to name a field Company Name, you must define it as Company_x0020_Name. If you want to name a field Revenue %, you must define it as Revenue_x0020__x0025_. The last thing to keep in mind is that the Name attribute cannot be longer than 32 characters.

The preceding site column definition also defines the optional StaticName attribute, which is another way of defining the internal name. The StaticName can be useful for referencing your field in custom code, regardless of the encoding used in the Name field. Finally, the site column definition defines the field’s DisplayName attribute, whose value is the title that users will see in their browsers. This last attribute can take advantage of the multilanguage support provided by Microsoft .NET in general, so declaring its value as a resource string reference (“$Resources:<Assembly_Name>,<Resource_Name>;”) instead of an explicit value will result in a multilanguage value.

The Type attribute is mandatory for site column definitions. It defines the data type assigned to the field. This Type attribute value can be one of a predefined set of SharePoint field types, or it can be a custom field type that you have defined and deployed. Table 3-1 presents some of the main field types provided by SharePoint.

More Info

For a complete list of field types, refer to the online product reference at http://msdn.microsoft.com/en-us/library/ms437580(v=office.15).aspx.

Table 3-1. Common predefined field types

Field type name

Description

Boolean

Represents a Boolean value (TRUE or FALSE), stored as a bit in Microsoft SQL Server and accessible as an SPFieldBoolean object through the Server Object Model.

Choice

Allows the user to select a single value from a predefined set of values. The XML schema of the Field element must declare the values (for further details, see A Choice site column defined in a feature element). It is stored as an nvarchar in SQL Server, and is accessible as an SPFieldChoice object through the Server Object Model.

MultiChoice

Allows the user to select multiple values from a predefined set of values. The XML schema of the Field element has to declare the values. It is stored as an ntext in SQL Server, and is accessible as an SPFieldMultiChoice object through the Server Object Model.

Currency

Defines a currency value. Currency is bound to a specific locale, using an LCID attribute. It can have constraints using Min, Max, and Decimals attributes. It is stored as a float in SQL Server and is accessible as an SPFieldCurrency object through the Server Object Model.

DateTime

Saves a date and time value. DateTime is stored as a datetime in SQL Server, and is accessible as an SPFieldDateTime object through the Server Object Model.

Lookup and LookupMulti

Behave almost the same as Choice and MultiChoice; however, the set of values to choose from is taken from another list of items within the same site. These field types are stored as int types in SQL Server, and are accessible as SPFieldLookup objects through the Server Object Model.

Note

Stores multiple lines of text. Note is stored as an ntext in SQL Server, and is accessible as an SPFieldMultiLineText object through the Server Object Model.

Number

Defines a floating-point number. Number can have constraints using Decimals, Div, Max, Min, Mult, and Percentage. It is stored as a float in SQL Server and is accessible as an SPFieldNumber object through the Server Object Model.

Text

Describes a single line of text of a configurable maximum length. Text is stored as an nvarchar in SQL Server, and is accessible as an SPFieldText object through the Server Object Model.

URL

Defines a URL with a specific LinkType (Hyperlink or Image). URL is stored as an nvarchar in SQL Server and is accessible as an SPFieldUrl object through the Server Object Model.

User and UserMulti

Describe a lookup for a single user or a set of users. These are stored as an int types in SQL Server, and are accessible as SPFieldUser objects through the Server Object Model.

The last attribute defined in the site column example is the Group attribute, which simply defines a group membership to make it easier to find custom fields through the web browser administrative interface. Group is an optional attribute, but it is better that you define it whenever you create a custom site column, in order to organize your columns in personalized custom groups.

Although it’s not an exhaustive keyword reference, Table 3-2 shows some of the many other interesting attributes that you can use when defining custom site columns. For a complete reference of the available attributes, you can read the following page on MSDN: http://msdn.microsoft.com/en-us/library/aa979575.aspx.

Table 3-2. Interesting optional Boolean attributes available for the Field element

Field attribute

Description

Hidden

Can assume a value of TRUE or FALSE. When TRUE, the field will be completely hidden from the UI and will be accessible only through code, using the Object Model.

ReadOnly

Can assume a value of TRUE or FALSE. When TRUE, the field will not be displayed in new and edit forms, but can be included in read-only data views. It will remain accessible using the object model.

Required

Can assume a value of TRUE or FALSE. Its name implies its role.

RichText

Can assume a value of TRUE or FALSE. It determines whether a text field will accept rich text formatting.

ShowInDisplayForm

Can assume a value of TRUE or FALSE. When FALSE, the field will not be displayed in the display form of the item containing the field.

ShowInEditForm

Can assume a value of TRUE or FALSE. When FALSE, the field will not be displayed in the editing form of the item containing the field.

ShowInNewForm

Can assume a value of TRUE or FALSE. If it is FALSE, the field will not be displayed in the form to add a new item containing the field.

While A simple site column defined in a feature element introduced a basic definition, A Choice site column defined in a feature element adds another level of complexity by declaring a Choice field that will be used to select the contact’s country affiliation.

This example shows how you can define a set of available values for a Choice field. Note that the list defines a Default element.

Another interesting task that you can accomplish when defining a site column is to declare a custom validation rule for its content. To do that, you simply define a Validation element as a child of the Field definition. The Validation element can have a Message attribute, which defines an error message to display to end users when validation fails, and a Script attribute, which defines a JavaScript rule that performs the validation. Alternatively, you can define a rule using the Formulas syntax of SharePoint, putting the rule inside the Validation element.

More Info

For further details on calculated fields and formulas in SharePoint, refer to the “Calculated Field Formulas” MSDN page, at http://msdn.microsoft.com/en-us/library/bb862071.aspx.

Content types

A content type schema defines a model for a specific SharePoint complex data type, and is based on a set of site column references, together with some other optional information related to forms, rendering templates, a specific document template (only in the case of document items), and custom XML configuration.

Chapter 2 showed how SharePoint uses a hierarchical structure for defining content types, which consists of a base content type named System with a single child named Item. SharePoint then applies an inheritance paradigm (similar to object-oriented class inheritance) to define each content type descendant of Item. Figure 3-1 shows an excerpt of the hierarchical inheritance tree for native content types. As a consequence of this behavior, you must define inheritance information for each new content type that you declare. For more details, read the “Content type IDs” section later in the chapter.

A simple content type defined in a feature element, together with its site columns provides an example of the Contact content type, defined by referencing a set of site columns.

A diagram of the hierarchy of content type definitions. Shown at the top, the root is the System content type. The first and only child of System is Item, from which many other content types inherit, including Document, Event, Contact, Task, Folder, and so on. On a third inheritance level, directly below Document, are XmlDocument and Picture.

Figure 3-1. The content types inheritance hierarchy in SharePoint.

This feature element example contains a ContentType element, which defines some descriptive information, such as the Name, Group, and Description. The ContentType element also defines a Version attribute, which indeed is used for managing versioning, as its name implies, but is still reserved by Microsoft for future use. Last, but most important, is the ID attribute, which defines the unique identifier for this content type in the site collection where it is defined. Inside the ContentType element is a FieldRefs element, which is the parent of a list of FieldRef or RemoveFieldRef elements. Each element in this list references a specific site column to be added or removed from this content type. You might notice that this example references all the site columns defined earlier in the feature element file. In fact, unless you are defining site columns for use in multiple content types, it’s common to define the referenced site columns within the same feature element file—just before the content type that will use them.

A simple content type defined in a feature element, together with its site columns also references a site column with the name Title and the ID {fa564e0f-0c70-4ab9-b863-0177e6ddd247}. This is the SharePoint native site column that defines the Title field for each SharePoint item. In the content type example, we changed the DisplayName value from Title, which still retains its internal name, to Full name, which will be the displayed name for this content type. By default, the Title field is also used by SharePoint to render the Edit Control Block menu, which allows you to display, edit, and manage a list item from the list UI.

Content type IDs

The ID attribute of a content type is not a simple GUID, as it was with the site columns definition; instead, it’s a more complex value that describes the hierarchical inheritance of the type. In fact, every content type ID is composed of the ID of its hierarchical parent content type, followed by a hexadecimal value that’s unique to the current content type. You could say that a content type ID defines its genealogy. This logic is recursive, starting with the System content type and extending all the way down to the current content type. Table 3-3 shows an excerpt of the base hierarchy of SharePoint content type IDs.

Table 3-3. An excerpt of the base hierarchy of SharePoint content type IDs

Content type

ID

System

0x

Item

0x01

Document

0x0101

XmlDocument

0x010101

Picture

0x010102

Event

0x0102

 

Contact

0x0106

Task

0x0108

 

Folder

0x0120

Table 3-3 demonstrates that the root content type is System, which is a special hidden content type with an ID value of 0x. The Item content type is the only child of System and has an ID value of 0x01 (the System ID + 01). The Document content type, which is a child of Item, has an ID value of 0x0101 (the Item ID + 01), while its sibling Event has an ID of 0x0102 (the Item ID + 02).

In general, the rule used to define content type IDs states that you can build an ID using either of two techniques:

  • Parent content type ID + two hexadecimal values (cannot be 00)

  • Parent content type ID + 00 + hexadecimal GUID

Microsoft generally uses the first technique to define base content type IDs. Third parties, such as vendors or ISVs, typically use the latter technique to define custom content type IDs. If you want to define a hierarchy of custom content types of your own, follow these steps:

  1. Identify the base content type from which you want to inherit.

  2. Add 00 at the end of the base content type ID.

  3. Add a hexadecimal GUID just after the 00.

  4. Append two hexadecimal values to declare every specific child of your content type.

As a concrete example, suppose that you want to define a custom content type inherited from the Document base content type. You would start with 0x0101, which is the Document ID, append 00 to it, and then append a hexadecimal GUID, making your ID something like 0x010100BDD3EC87EA65463AB9FAA5337907A3ED.

If you wanted to use your custom content type as a base for some other inherited content types, you would append 01, 02, and so on for each child content type, as in the following:

  • Base ID0x010100BDD3EC87EA65463AB9FAA5337907A3ED

  • Child 10x010100BDD3EC87EA65463AB9FAA5337907A3ED01

  • Child 20x010100BDD3EC87EA65463AB9FAA5337907A3ED02

More Info

Content type IDs have a maximum length of 512 bytes. Because every two hexadecimal characters correspond to a single byte, a content type ID has a maximum length of 1,024 characters.

With that in mind, we can go back to the example custom Contact content type. First, you need to choose the base content type from which you want to inherit. For example purposes, assume that you decide to use the generic base Item as the parent content type. That means the custom content type ID will start with 0x01, followed by 00 and then a hexadecimal GUID. The end result is the same as the ID highlighted in bold in A simple content type defined in a feature element, together with its site columns:

ID="0x0100A60F69C4B1304FBDA6C4B4A25939979F"

The goal of the case study is to define a custom list that is based on a couple of content types (Customer and Supplier) inherited from this base Contact content type. Customer and Supplier content type definitions shows the definitions of the Customer and Supplier content types.

Both of these content types extend the base Contact content type; each adds a specific site column. The Customer content type adds a required field to define the customer level (A, B, or C) for each Customer instance, while the Supplier content type adds a field to reference a local account, which you can browse as a SharePoint user. You can see the inheritance hierarchy of these custom types in Figure 3-2, which shows a portion of the Site Content Type page of a site collection.

A screen shot of the list of content types defined in a site collection. It illustrates that the DevLeapContact content type is inherited by both the DevLeapCustomer and DevLeapSupplier content types.

Figure 3-2. The Site Content Type page of a site collection where the custom content types are provisioned.

Finally, consider that Visual Studio 2012 automatically calculates the content type IDs when you add a new content type to a SharePoint project. In fact, if you try to add a content type to a SharePoint project within Visual Studio 2012, you will be prompted with a one-step wizard, regardless of whether you are creating a Windows SharePoint Services Solution Package (WSP) or a SharePoint app. In the wizard’s first and only step, you must choose the basic content type from which you would like your custom content type to inherit (Figure 3-3).

A screen shot of the SharePoint Customization Wizard. From the drop-down menu, you must choose a base content type from which your custom content type will inherit. The wizard prompts you with the most likely choice.

Figure 3-3. The wizard for creating a new content type.

After you make your choice and click finish to close the wizard, SharePoint displays a graphical designer useful to define the columns of the content type and its overall configuration. Figure 3-4 shows the two tabs available in the Content Type designer: Columns and Content Type.

A screen shot of the Content Type designer. The Columns tab is active; here you define columns, descriptive fields, and behavioral aspects of the target content type. The designer’s second tab is named Content Type.

Figure 3-4. The two tabs available in the Content Type designer.

As you can see, the Columns tab is active. Here you can reference the site columns to use in the current content type. Note, however, that you can specify existing site columns only. The Content Type tab enables you to define the name, the description, and the group of the current content type. Lastly, through this second tab you can also determine whether the content type will inherit columns from its parent type or not, as well as if the current type will be read-only and/or hidden. Based on your settings, the designer creates an XML element manifest file that is similar to what you can code manually. Although this might seem like a worthwhile shortcut, it is somewhat limited. When you need a finer degree of flexibility in defining custom content types, manually creating or editing the XML file is a better solution.

More about content types

Sometimes you need a more restricted content type; in such cases, SharePoint offers several other interesting attributes to help you out. For example, the ReadOnly attribute makes the content type read-only when its value is set to TRUE. Likewise, when the Sealed attribute is set to TRUE, it seals a content type so that only a site collection administrator using the Server Object Model can unseal it for editing. Lastly, the Hidden attribute is useful for making a content type invisible so that contributors cannot create new items of this type in list views, but you will still have access to it through your custom code. If you want to declare a content type as completely invisible—not only for end users but also for site collection administrators—you can make it belong to a special group named _Hidden.

In addition, you can configure a content type not only through ContentType element attributes, but also by declaring some child elements. One of these is the FieldRefs child element discussed earlier in this chapter. Another useful element is XmlDocuments, with which you can define any kind of custom XML configuration to apply to the content type. SharePoint itself uses this element to declare custom controls and pages for the content type. Using the XmlDocuments element inside a content type definition shows how to use this element.

Using the XmlDocuments element inside a content type definition shows that the XmlDocuments element is just a container for one or more XmlDocument elements. Every XmlDocument element can have a NamespaceURI attribute that declares the scope of the custom configuration defined. Using the XmlDocuments element inside a content type definition declares a configuration that defines custom ASCX control files that are used for rendering display, edit, and add forms for instances of the current content type. The ASCX control files referenced should be deployed inside the CONTROLTEMPLATES special folder of SharePoint, through a farm-level (full-trust) solution. The content of each XmlDocument element derives from the referenced NamespaceURI. The only requirement is that the XML content must be valid against its declared XML schema.

When you consider that in a farm-level (full-trust) solution you can access any custom XmlDocument that you define while provisioning content types later through the Server Object Model, you can see that the model provides you with an extremely customizable environment.

Document content types

Content types inherited from the Document base content type (ID: 0x0101) are a special case that you must analyze a bit more carefully than usual. In fact, every document has numerous specific configurations that it must handle. For instance, in the “Content types” section earlier in the chapter, you learned that a document can have a document template, a document information panel, or both.

Defining the Invoice content type, inherited from the Document content type shows the definition for a custom document content type that declares an Invoice document model.

The Document portion of the ID is highlighted in bold to remind you of the underlying behavior of SharePoint. The DocumentTemplate element (also highlighted) has a TargetName attribute that defines the URL (relative for the site collection) of the template item to use for every new Invoice instance. Defining a custom document information panel for an Invoice content type, inherited from the Document content type shows how to define a custom document information panel for a Document content type, assuming that you have already designed and deployed the panel.

Defining a custom document information panel for an Invoice content type, inherited from the Document content type declares the absolute URL of the document information panel by using the xsnLocation element. It also disables caching in the Microsoft Office client by setting the cached element to FALSE. Lastly, it defines how the document should behave relative to this new panel, through the openByDefault element, which is set to TRUE, meaning that the panel should open by default. The xsnScope element is required, but for now it is reserved by Microsoft for internal use only.

List definitions

Now that you have defined your content types, you are ready to use them in a real list of contacts, comprising customers and suppliers. In fact, generally, whenever you define a set of custom content types, you also define one or more list definitions that use these content types. A list definition is simply a formal representation, using an XML schema, of a list data model from which you are able to create one or more instances of items corresponding to that model.

In SharePoint, a list definition is a combination of two files: a Schema.xml file, which defines the data structure and configuration of the list definition model, and a feature element file that describes the ListTemplate, which defines the information required for provisioning and deploying the list definition model.

List schema file

The list schema file is an XML document that describes all the metadata for the list data structure. The following are the main areas of the Schema.xml file for a list definition:

  • Content Types. This section defines the content types that will be available within the list definition.

  • Fields. This section declares the list-level site columns, which correspond to the entire set of site columns referenced by all the content types associated with the list definition.

  • Views. This section defines the views that will be available to the end user for navigating among the items of list template instances.

  • Forms. This section declares the ASPX pages that will be provided to the end user to add, display, and update items of a list instance based on the current list definition.

  • Validation. This section defines the validation rules for list items.

  • Toolbar. This section declares the type of toolbar that must be provided in the browser interface.

In addition to the preceding list, the complete XML schema contains some other elements as well. Excerpt of a list definition schema file shows an excerpt from a Schema.xml file that describes a list definition, together with these main sections.

The List element

The List element is the root of the schema file and declares some basic attributes for the list definition. The Title attribute defines the name of the list definition. The BaseType attribute defines the base list type to use for the current list definition. The global onet.xml file of SharePoint (for further details, please read Chapter 13) declares the list of all the available integer values for the BaseType values within a BaseTypes element.

Note

The global onet.xml file is located in the SharePoint15_RootTEMPLATEGLOBALXML folder.

The available BaseTypes values are

  • 0. Generic/Custom List

  • 1. Document Library

  • 2. Not used, may be reserved for future use

  • 3. Discussion Forum (deprecated, use 0 instead)

  • 4. Vote or Survey

  • 5. Issues List

For example, Excerpt of a list definition schema file used a BaseType with a value of 0 because we are defining a generic/custom list definition. The Url attribute is optional and defines the path to the root directory containing any ASPX file specific for the list definition. The FolderCreation attribute is also optional, and informs SharePoint whether to show (TRUE) or not show (FALSE) the New Folder command on the list toolbar. Finally, the Direction attribute is optional and declares the reading direction: RTL (right to left) or LTR (left to right). In Excerpt of a list definition schema file, the Direction value is read from a resource string so that the list will be compliant with the current locale settings of the site collection. Lastly, to make the users aware of the existence of the different available content types (Contact, Customer, and Supplier) when they are creating new items, we need to explicitly enable content types on the list definition, setting the EnableContentTypes attribute to a value of TRUE. There are many other attributes available for the List definition element; Table 3-4 shows some of them.

More Info

For a complete reference of all the available attributes for the List element, refer to the official product documentation on MSDN, at http://msdn.microsoft.com/en-us/library/ms415091(v=office.15).aspx.

Table 3-4. Some of the main attributes for the List element of a Schema.xml list definition file

Attribute

Description

DisableAttachments

Optional Boolean value to disable attachments on the list.

EnableMinorVersions

Optional Boolean value that controls versioning with major and minor version of items.

ModeratedList

Optional Boolean value to enable content approval on inserted items.

PrivateList

Optional Boolean value to specify that the list is private.

VersioningEnabled

Optional Boolean value to enable versioning on the list. This value can be changed when creating a list instance.

The MetaData element

The main child element of List is the MetaData element, which wraps all the other elements in the Schema.xml file.

One of the main child nodes of MetaData is the ContentTypes element. This element declares the entire list of content types referenced by the current list definition. The ContentTypes section of metadata for the sample list definition declares the ContentTypes element for the custom Contacts list.

The ContentTypes section of metadata for the sample list definition defines all the content types already defined in the previous section, repeating their IDs to link these copies to the original definitions. Why repeat these declarations instead of simply referencing them in some way—such as by just linking their IDs, for example? During a content type’s lifetime, its structure might change. To prevent and avoid any data loss, SharePoint copies content type definitions inside the list definitions that use them. Doing so preserves data models and data instances even if someone later changes them. Imagine what would happen if you had a simple content type reference rather than a copy; if you were to provision a Customer content type and use it in a custom list, then a few months later, when you have thousands of customer instances in your list, you delete a column from the Customer content type—or worse, you delete the entire content type! Having a complete copy of the content type definition allows SharePoint to maintain your data, even when the original content type changes or is removed.

On the other hand, whenever you want to make a change to one of your provisioned content types and you want that change applied to every instance in a site collection, you need to explicitly force the update through the browser-based content type administration page, through code using the Server Object Model, or by manually updating any references in the provisioned XML files, including the Schema.xml files for list definitions.

The ContentTypes section of metadata for the sample list definition defines all three content types (Contact, Customer, and Supplier) and declares the base Contact as hidden, which forces users to explicitly create Customer or Supplier instances.

Another child of MetaData is the Fields element. It defines the list-level columns used to store metadata of item instances. These list-level columns are almost the same as the site columns defined in the first section of this chapter. Once again, their definitions are duplicated rather than referenced, and for the same reason: to support changes of the models without data loss during the site columns’ lifetimes. The Fields section of the list definition contains all the columns used by any of the content types declared in the same Schema.xml file. The Fields section of the MetaData element for the sample list definition shows the Fields element declared for the custom Contacts list.

Just as with the ContentTypes section, the Fields section is simply a wrapper for the copies of all the previously defined site columns. Notice that the ID values for the site columns are the same as those of the global site columns, serving to keep the global site columns linked to the local list-level columns.

Figure 3-5 shows how the List Settings page of a list based on the custom Contacts list definition looks in a web browser. Note that all three content types and all the list-level columns are present.

A screen shot of the List Settings page, depicting the three content types (Contact, Customer, and Supplier), as well as all the related columns and their relationships with parent content types.

Figure 3-5. The List Settings page of a list instance based on the custom Contacts list definition.

Just after the Fields section comes the Views element, which is a child of MetaData. This section is really interesting because it is where you define the views on data that will be available to the end users in the web browser. Each View element, which is a child of Views, defines a data view declaring some configuration attributes (illustrated in Table 3-5).

More Info

For a complete list of all the available View attributes, refer to the official documentation on MSDN, at http://msdn.microsoft.com/en-us/library/ms438338(v=office.15).aspx.

Table 3-5. Some of the main attributes for the View element of a Schema.xml list definition file

Attribute

Description

Type

The type of view. Type can be HTML, Chart, or Pivot.

BaseViewID

An Integer value that declares the ID of the view. BaseViewID must be unique within a Schema.xml file.

Url

The public URL to access the view from the browser.

DisplayName

The name of the view in the web browser.

DefaultView

A Boolean value that declares if the view is the default view for the current list.

MobileView

A Boolean value that specifies if the current view has to be made available to mobile devices.

MobileDefaultView

A Boolean value that declares if the view, enabled for mobile access, is the default view for mobile devices.

SetupPath

Defines the site-relative path to the ASPX file corresponding to the current view model. It allows provisioning a custom page for the current view.

WebPartZoneID

A string that declares the ID of the WebPartZone control where the current view will be loaded, within the ASPX Web Part page.

The View element also allows you to declare some other configuration details using child elements. The default View definition for the sample list shows the default view definition for the list of contacts.

The default View definition for the sample list declares a BaseViewID with a value of 1, and specifies that this view will be the default (DefaultView), not only for classic web browsers, but also for mobile devices (MobileDefaultView). The URL to access the view will be AllItems.aspx, and this page will be based on the SetupPath file pagesviewpage.aspx filling out the WebPartZone control whose ID is Main.

The child elements of the View tag in The default View definition for the sample list inform SharePoint to use the Standard value for the toolbar. The maximum number of rows (RowLimit) is set to return a value of 50, enabling paging.

Note

If not specified, the default RowLimit is 30.

After these configuration elements, The default View definition for the sample list defines some other elements that determine the data to show, declaring a Query element to filter and sort data, and a set of ViewFields elements to show, as well as some optional grouping rules. The Query element is simply a Collaborative Application Markup Language (CAML) query that defines the values to extract from the source list, the ordering rule, and which values will be shown in the current view. For example, The default View definition for the sample list queries all the items in the list, sorting them by the value of their ID fields.

Note

CAML is an XML-based querying language that can be used to define filtering, sorting, and grouping on SharePoint data. The CAML language reference is available on MSDN, at http://msdn.microsoft.com/en-us/library/ms467521(v=office.15).aspx. In case you are a SharePoint 2010 developer, consider that CAML hasn’t changed that much between SharePoint 2010 and SharePoint 2013.

Another important child section of the View element is the ViewFields element, which declares the fields to show in the resulting view. These fields are referenced by their internal names, using a specific FieldRef element.

The last child elements in the View are the XslLink and JsLink elements. Since SharePoint 2010, SharePoint can render views using XSLT transformations. The XslLink element specifies the path to the XSLT file used to render the view. This XSLT file path is relative to the folder SharePoint15_RootTEMPLATELAYOUTSXSL. Moreover, starting from SharePoint 2013, the JsLink element allows declaring a JavaScript file to include and use for rendering the view.

Note

SharePoint15_Root refers to the SharePoint root folder, which is typically located at C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15.

As an alternative to providing an explicit XSLT file path, you can use an Xsl element to simply declare the XSLT transformation inside the Schema.xml file. Because you may want to reuse the XSLT transformation, however, a better choice is to reference an external file. This is especially useful when you are developing a full-trust solution. The capability to define the XSLT transformation inside the Schema.xml file is provided for those situations, such as for sandboxed solutions and SharePoint apps, when you want to avoid copying files to the file system of the target SharePoint farm.

The Forms element is another important configuration section for the list definition, as shown in The Forms configuration section of the custom Contacts list definition.

The Forms element contains a set of Form elements that declare the forms available to the end user. Each Form element requires a Type attribute that takes one of the following values:

  • DisplayForm. The form to display a list item

  • EditForm. The form to edit an existing list item

  • NewForm. The form to add a new list item

Every form also requires a URL where it can be accessed. Forms might include an optional SetupPath attribute from which to load the ASPX page model, as well as a WebPartZoneID attribute, which specifies the ID of the Web Part zone used to load the rendering control of the form. As an alternative to the SetupPath attribute, you could have a Path attribute, which defines a physical file system path relative to the _layouts folder for a template file, and a Template attribute, which specifies the name of the template to use. You can also use CAML syntax to define the template for the body, buttons, opening section, and closing section of each of these forms, using these specific child nodes of the Form element: ListFormBody, ListFormButtons, ListFormClosing, and ListFormOpening.

The last configuration section shown is the Validation element. This element, introduced with SharePoint 2010, supports defining validation rules that can apply to each item of the list. Declaring a sample validation rule for the custom Contacts list definition items shows how to declare a custom validation rule together with a validation error message that end users will see if validation fails.

The validation rule forces the items to have a Title field with a value not equal to Blank. Notice that list-level validation rules work properly only with fields shared by all the content types of the list. If you enforce a rule against a field that is not defined in all the content types of the list, then your rule will always throw an error when applied to the wrong content types. For example, if you define a rule at the list level for the DevLeapCustomerLevel field of the Customer content type, you will not be able to add or update any Supplier instances, because the DevLeapCustomer field is not present in the Supplier content type. In such cases, you should instead define the validation rule at the site column level.

Defining a custom view

When defining custom list definitions, you’ll frequently want to declare some custom views that correspond to the business rules of your data model. For example, the sample model could feature one view that shows only customers and another that shows only suppliers. This section demonstrates how to define the former view; the latter’s definition will be almost identical.

First, define a new View element under the Views element of the Schema.xml file. The new view will have a unique BaseViewID; in this example it will be 2. The DisplayName will be All Customers, the Type will be HTML, and the Url will be AllCustomers.aspx. All the other attributes values of the View element are trivial. You can see the complete definition of this view in Defining a custom view for a custom Contacts list definition.

There are some areas of interest in this view definition. First, the code defines a Query to filter only items with a ContentType value of DevLeapCustomer and orders the result by the item ID. Then it references all the fields of the Customer content type, defining a set of FieldRef elements within the ViewFields element. Lastly, a custom XSLT transformation is defined for rendering the custom view. SharePoint will search for this XSLT file, Contacts_Main.xsl, in the SharePoint15_RootTEMPLATELAYOUTSXSL folder. The file has to be placed in that folder using the solution-provisioning tools provided by Visual Studio 2012 to create a full-trust solution. (For further details, see Chapter 4) Otherwise, as you have already seen, you can define the XSLT code directly in the View schema definition, inside an Xsl element.

The XSLT file you reference or define in the View definition is a common XSLT transformation that will receive a wide range of parameters at run time from SharePoint. In the XSLT code, for example, you can access the XmlDefinition variable, which provides the XML definition of the current View. To define an XSLT for a custom view, you must provide an XSLT template that matches the BaseViewID of the targeted view. For the Contacts example, the following template was defined:

<xsl:template match="View[@BaseViewID="2"]" mode="full">
  <!-- Here is our custom XSLT transformation -->
</xsl:template>

The XSLT also receives a parameter named Rows that contains all the items to be rendered. The content of the Rows parameter provided to a custom XSLT for rendering a list view shows an excerpt of the XML content of the Rows parameter. You can read it simply by using an XSLT template that copies the source content with an <xsl:copy-of /> element.

The content of the Rows parameter provided to a custom XSLT for rendering a list view illustrates that the Rows parameter provides each row along with its data columns, specified as attributes of a Row element. To output the content of the rows, you simply need to retrieve the values of these attributes, placing them inside the proper HTML elements to adhere to the graphical layout that you need to render.

However, many SharePoint developers do not like writing XSLT files, because XSLT is inflexible (although very powerful) from a syntax viewpoint. Luckily, starting with SharePoint 2013, you have the option to provide a custom JavaScript file through the JsLink child element of the View element, in order to move rendering templates into client-side code. Generally speaking, this technique is known as client-side rendering (CSR). A custom view definition for the custom Contacts list definition using JavaScript rendering uses this new technique to define a custom view.

In A custom view definition for the custom Contacts list definition using JavaScript rendering, shows the JsLink element (highlighted in bold) configured as the default (Default="TRUE”) rendering template. SharePoint will look for the JavaScript file at a URL relative to the current site collection, because of the ~site token at the very beginning of the URL. You can deploy the JavaScript code of the CustomCustomerView.js file to the target site simply working at the website level, using a sandboxed solution or an app deployment process. In the JavaScript code, you can reference the Client Object Model of SharePoint in order to query the current list configuration, as well as the items to render. This technique is extremely powerful. While provisioning lists for Office 365, for example, you can use this technique to move all the rendering logic to the client side, using jQuery or CSS rendering templates. With its XSLT and JavaScript support, SharePoint opens up some great business opportunities; because it gives you the capability to display fully customized rendering of list views, your solutions can support fully customized template layouts, even in extreme web content management solutions.

More Info

For more information about CSR, you can read the document “How to: Customize a list view in apps for SharePoint using client-side rendering,” available at http://msdn.microsoft.com/en-us/library/jj220045.aspx.

The ListTemplate definition file

ListTemplate is the feature element file that declares all the deployment properties needed to provision the list definition. It must be provisioned into a custom feature together with the Schema.xml file. The ListTemplate feature element for the sample Contacts list definition shows the ListTemplate for the sample Contacts list definition.

The Type attribute is the most important attribute in the ListTemplate element. Type takes an integer value that should be unique at the site collection level. The code sample uses a value of 10001 to avoid overlapping with values of out-of-the-box list templates. In general, you should use a large integer value to avoid overlapping with SharePoint. Consider that values in the range between 100 and 1200 are already taken, and developers should allocate numbers greater than 10000. The uniqueness of this attribute allows you to define custom UI extensions that will target the entire set of lists with that Type value.

The other attributes are straightforward. The BaseType attribute states the base type for the current list definition. The Name attribute represents the internal name of the list, and the DisplayName is the text shown to end users, together with the Description and the Image. You can load the values of these descriptive attributes from external resource strings to provision list definitions in a multilanguage environment. The OnQuickLaunch Boolean attribute value controls whether SharePoint shows any instance of the list in the Quick Launch menu. You can also provision a list instance through a custom feature of type ListInstance, which will be explained in Chapter 4.

Finally, the SecurityBits attribute defines the security behavior of the list. This is a two-digit string, where the first digit controls whether users can read all items (1) or only their own items (2). The second digit defines edit access permissions. The possible values are

  • 1. Users can edit any item.

  • 2. Users can edit only their own items.

  • 4. Users cannot edit items.

For example, a value of 22 for the SecurityBits attribute means that users can see and edit only their own items, while the default value of 11 means that users can see and edit all the items in the list.

More Info

For a complete list of attributes for the ListTemplate element, refer to the official product documentation on MSDN, at http://msdn.microsoft.com/en-us/library/ms462947(v=office.15).aspx.

Working with lists in Visual Studio 2012

Just as you can define content types with Visual Studio 2012 and its designers, you can also define basic lists. In fact, whenever you add an item of type List to a SharePoint project, regardless of whether it is a solution or an app, you are provided with a graphical designer that allows you to design fields, content types, and views, and provide descriptive information for the list. First, you are prompted with the wizard shown in Figure 3-6. Here you can specify the name of the target list and create a customizable list definition based on a basic content type or a list instance based on an existing list definition.

A screen shot of the SharePoint Customization Wizard, which includes fields for the name and the type of the target list to create. You can choose between a custom list definition or a list instance of an already existing list definition.

Figure 3-6. The wizard for creating a new list in a SharePoint solution or app.

After you complete the page and click Finish, you can configure the resulting item through a specific designer. If you created a new list definition, you will have access to a designer with three tabs, for configuring fields, content types, and views of the custom list definition. Figure 3-7 shows the designer for this chapter’s example Contacts list, displaying the columns defined in the schema of the list definition.

A screen shot of the Columns tab of the designer of a custom list definition. Using the tab’s grid interface, you can configure the columns of the target list. Check boxes enable you to specify which fields are required.

Figure 3-7. Configuring the fields of a custom list definition within Visual Studio 2012.

The designer also provides also a Content Types button; click it to open the dialog box shown in Figure 3-8. Here you can determine the content types associated with the current list template.

A screen shot of the Content Type Settings dialog box. The Show On New Menu check boxes enable you to configure the content types associated with the current list definition. Select a content type from the list on the left and click the Set As Default icon to specify the default content type for the list.

Figure 3-8. The dialog box for configuring the content types associated with a list definition.

Once you have defined the content types and the columns, you can determine the views for the custom list definition. Click the Views tab to access the controls shown in Figure 3-9.

A screen shot of the Views tab. The dialog box allows configuring views of the target list definition. In particular, you can configure the name of the view, the number of items to show for each page, the fields to render, and their ordering.

Figure 3-9. Determining the views for the custom list definition.

Whether you are defining an instance of your custom list definition or simply declaring an instance of an already existing list definition, you can configure some descriptive aspects of the target list using the List tab, shown in Figure 3-10.

A screen shot of the List tab, which includes fields for the title, relative URL, and description of the target list. Using the check boxes, you can define whether the list will be placed on the Quick Launch menu or if the list should be hidden from browsers.

Figure 3-10. The List tab for configuring the list instance descriptive parameters.

By default, Visual Studio 2012 always defines a list instance together with the list definition. If you do not want to provision a list instance, you can comment the code of the ListInstance element created within the Elements.xml file available inside the list item in the Visual Studio project outline.

Summary

This chapter described how to define XML files to provision SharePoint data models and structures. In particular, it showed how to use feature element files to deploy site columns, content types, and list definitions. It also discussed how to do similar things using the designer provided by Microsoft Visual Studio 2012, instead of using low-level XML files. These features promise a great return on investment and a common maintenance plan.

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

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