Microsoft SharePoint : Creating Various Content Types

Packt Publishing

The 50 recipes in this book take you straight into the advanced concepts of SharePoint and PowerShell administration. Totally practical and fully adaptable to your own business, they’ll raise your professionalism to new heights.

SharePointcontent types are used to make it simpler for site managers to standardize what content and associated metadata gets uploaded to lists and libraries on the site. In this article, we'll take a look at how you can create various content types and assign them to be used in site containers.

As a subset of more complex content types, a document set will allow your users to store related items in libraries as a set of documents sharing common metadata. This approach will allow your users to run business processes on a batch of items in the document set as well as the whole set. In this article, we'll take a look at how you can define a document set to be used on your site.

Since users mostly interact with your SharePoint site through pages and views, the ability to modify SharePoint pages to accommodate business user requirements becomes an important part of site management. In this article, we'll take a look at how you can create and modify pages and content related to them. We will also take a look at how you can provision simple out-of-the-box web parts to your SharePoint publishing pages and configure their properties.

Creating basic and complex content types

SharePoint lists and libraries can store a variety of content on the site. SharePoint also has a user interface to customize what information you can collect from users to be attached as an item metadata.

In the scenario where the entire intranet or the department site within your organization requires a standard set of metadata to be collected with list and library items, content types are the easiest approach to implement the requirement.

With content types, you can define the type of business content your users will be interacting with. Once defined, you can also add a metadata field and any applicable validation to them. Once defined, you can attach the newly created content type to the library or list of your choice so that newly uploaded or modified content can conform to the rules you defined on the site.

Getting ready

Considering you have already set up your virtual development environment, we'll get right into authoring our script.

It's assumed you are familiar with how to interact with SharePoint lists and libraries using PowerShell.

In this recipe, we'll be using PowerGUI to author the script, which means you will be required to be logged in with an administrator's role on the target Virtual Machine.

How to do it...

Let's take a look at how we can provision site content types using PowerShell as follows:

Click Start | All Programs | PowerGUI | PowerGUI Script Editor.

In the main script editing window of PowerGUI, add the following script:

On the ribbon, click the Library tab and select Settings | Library Settings.

Take note of the newly associated content type added to the Content Types area of the library settings, as shown in the following screenshot:

Navigate back to the Shared Documents library from the Quick launch menu on your site and select any of the existing documents in the library.

From the ribbons Documents tab, click Manage | Edit Properties.

Take note of how the item now has the Content Type option available, where you can pick newly provisioned Org Document content type.

Pick the Org Document content type and take note of the associated metadata showing up for the new content type, as shown in the following screenshot:

How it works...

First, we defined the script variables. In this recipe, the variables include a URL of the site where the content types are provisioned, http://intranet.contoso.com, and a document library to which the content type is associated:

$ListName = "Shared Documents"

Once a PowerShell snap-in has been loaded, we get a hold of the instance of the current site and its root web. Since we want our content type to inherit from the parent rather than just being defined from the scratch, we get a hold of the existing parent content type first, using the following command:

$DocumentContentType = $RootWeb.AvailableContentTypes["Document"]

Next, we created an instance of a new content type inheriting from our parent content type and provisioned it to the root site using the following command:

Here, the new object takes the following parameters: the content type representing a parent, a web to which the new content type will be provisioned to, and the display name for the content type.

Once our content type object has been created, we add it to the list of existing content types on the site:

$ct = $RootWeb.ContentTypes.Add($ContentType)

Since most content types are unique by the fields they are using, we will add some business- specific fields to our content type. First, we get a hold of the collection of all of the available fields on the site:

$OrgFields = $RootWeb.Fields

Next, we create a string collection to hold the values for the choice field we are going to add to our content type:

$choices = New-Object System.Collections.Specialized.StringCollection

The field with list of choices was called Division, representing a company division. We provision the field to the site using the following command:

In the preceding command, the first parameter is the name of the field, followed by the type of the field, which in our case is choice field. We then specify whether the field will be a required field, followed by a parameter indicating whether the field name will be truncated to eight characters. The last parameter specifies the list of choices for the choice field.

Another field we add, representing a company branch, is simpler since it's a text field. We define the text field using the following command:

Creating and configuring document sets

SharePoint document set is the new feature allowing users to group documents within their libraries in order to share common metadata value between the set of document. As an added benefit, with document sets, your users will be able to run out-of-the- box and custom workflows on the entire set and individual documents within a set.

The document set functionality in SharePoint is achieved using a concept of content types where you define your custom document set template in a form of the content type. Once your content type is defined, you can bind it to a set of desired libraries and let users work with the set.

Since document set configuration requires quite a few steps to make it available on the site, we can use PowerShell to script those configuration steps to help us quickly provision document sets to multiple environments. In the scenario where you have several document sets to be created and deployed, PowerShell will make it easier to get your site set up without incurring a significant downtime.

Getting ready

Considering you have already set up your virtual development environment , we'll get right into authoring our script.

We'll assume you are also familiar with the concept of content types and how you can use PowerShell to provision content types to your site as described in the previous recipe.

In this recipe, we'll be using PowerGUI to author the script, which means you will be required to be logged in with an administrator's role on the target Virtual Machine.

How to do it...

Let's see what's involved in creating documents sets with PowerShell:

Click Start | All Programs | PowerGUI | PowerGUI Script Editor.

In the main script editing window of PowerGUI, add the following script:

Under the Welcome Page Columns group, under the Available columns section, select both Branch and Division columns and choose Add to add them to the list of columns visible on the document set home page, as shown in the following screenshot:

From the ribbon, click the Documents tab, then click the New Document | Org Document Set, as shown in the following screenshot:

Provide the Title and other required metadata for the document set and click OK to create a new document set.

In the Shared Documents library, locate the newly created document set and open it. Take note of the entered metadata and other document set property values available on the home page of the set, as shown in the following screenshot:

How it works...

Document sets represent complex content types, so their provisioning is similar to provisioning a content type.

We start by defining our script variables: a URL of the site where the document set is to be provisioned, http://intranet.contoso.com, and a document library to which the document set will be associated:

$ListName = "Shared Documents"

Once a PowerShell snap-in has been loaded, we get a hold of the instance of the current site and its root web. Our document set will inherit from SharePoint 2010 the base document set content type. First, we get a hold of the existing parent document set content type, using the following command:

Creating and editing publishing pages with PowerShell

Just about any SharePoint intranet and extranet site consists of a series of collaboration and publishing pages which your users interact with the site.

When it comes to maintaining content on the site, your organization may want to bulk change the content throughout the site, or migrate existing content to be hosted within SharePoint. As an example, you may be asked to pre-create a few hundred news article pages where your users can populate the content for them before the news release section of the site launches.

Whether you have been tasked with this assignment on an existing intranet or the new site, you can be sure that using PowerShell will minimize the impact on the environment downtime and you will have a reusable script to perform the deployment to multiple environments.

In this recipe, we will see how you can create a new SharePoint publishing page and edit its properties. We'll also take a look at how you can automatically approve the page and make it available for others to see.

Getting ready

Considering you have already set up your virtual development environment, we'll get right into authoring our script.

In this recipe, we'll be using PowerGUI to author the script, which means you will be required to be logged in with an administrator's role on the target Virtual Machine.

How to do it...

We'll take a look at how you can create publishing pages using PowerShell.

Click Start | All Programs | PowerGUI | PowerGUI Script Editor.

In the main script editing window of PowerGUI, add the following script:

How it works...

Note that in this recipe, we're publishing a site collection with a different URL and not our collaboration site collection.

Next, we load PowerShell SharePoint snap-in and retrieve the current site collection object. The demonstration environment contains the sub-site under the main site URL. The sub-site is used for demo press releases and we get a hold of it using the following command:

$PressReleaseWeb = $SPSite.OpenWeb("/PressReleases")

We then convert the press release web object to a publishing site object using the following command:

Before we go ahead and create an instance of the page, we got a hold of the page layout which the page uses. The page layout will drive which web part zones are going to be displayed on the page. We get the Blank Web Part page page layout:

Once our filename for the new page has been constructed, we can go ahead and create a new page object as shown in the following code:

$page = $pubWeb.GetPublishingPages().Add($pageFileName, $pageLayout)

Now that the page has been created, we can edit its properties. Have you noticed how we first got a hold of the ListItem object of the page before editing the properties? We do this since the page object doesn't have direct access to some of the properties of the page item. The page is just an item within the page library and in order to access its underlying object functionality we use the ListItem property and make updates to the underlying object directly as follows:

$pageItem["PublishingContactEmail"]="brads@contoso.com"

As a part of this recipe, we also retrieved the existing page in order to demonstrate how you can modify the existing page on the publishing site. We start by retrieving items in the page library with the filename. The page filename is default.aspx:

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.