7 min read

(For more resources on Microsoft SharePoint, see here.)

SharePoint content 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:

  1. Click Start | All Programs | PowerGUI | PowerGUI Script Editor.
  2. In the main script editing window of PowerGUI, add the following script:

    # Defining script variables
    $SiteUrl = "http://intranet.contoso.com"
    $ListName = "Shared Documents"
    # Loading Microsoft.SharePoint.PowerShell
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.
    SharePoint.Powershell'}
    if ($snapin -eq $null) {
    Write-Host "Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }
    $SPSite = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl}
    if($SPSite -ne $null)
    {
    Write-Host "Connecting to the site" $SiteUrl ",list "
    $ListName
    $RootWeb = $SPSite.RootWeb
    $SPList = $RootWeb.Lists[$ListName]
    Write-Host "Creating new content type from base type"
    $DocumentContentType = $RootWeb.AvailableContentTypes["Document"
    ]
    $ContentType = New-Object Microsoft.SharePoint.SPContentType -
    ArgumentList @($DocumentContentType, $RootWeb.ContentTypes, "Org
    Document")

    Write-Host "Adding content type to site"
    $ct = $RootWeb.ContentTypes.Add($ContentType)

    Write-Host "Creating new fields"
    $OrgDocumentContentType = $RootWeb.ContentTypes[$ContentType.Id]
    $OrgFields = $RootWeb.Fields
    $choices = New-Object System.Collections.Specialized.
    StringCollection
    $choices.Add("East")
    $choices.Add("West")
    $OrgDivision = $OrgFields.Add("Division", [Microsoft.SharePoint.
    SPFieldType]::Choice, $false, $false, $choices)
    $OrgBranch = $OrgFields.Add("Branch", [Microsoft.SharePoint.
    SPFieldType]::Text, $false)
    Write-Host "Adding fields to content type"
    $OrgDivisionObject = $OrgFields.GetField($OrgDivision)
    $OrgBranchObject = $OrgFields.GetField($OrgBranch)
    $OrgDocumentContentType.FieldLinks.Add($OrgDivisionObject)
    $OrgDocumentContentType.FieldLinks.Add($OrgBranchObject)
    $OrgDocumentContentType.Update()
    Write-Host "Associating content type to list" $ListName
    $association = $SPList.ContentTypes.Add($OrgDocumentContentType)
    $SPList.ContentTypesEnabled = $true
    $SPList.Update()
    Write-Host "Content type provisioning complete"
    }
    $SPSite.Dispose()

  3. Click File | Save to save the script to your development machine’s desktop. Set the filename of the script to CreateAssociateContentType.ps1.
  4. Open the PowerShell console window and call CreateAssociateContentType. ps1 using the following command:

    PS C:UsersAdministratorDesktop> . CreateAssociateContentType.ps1

  5. As a result, your PowerShell script will create a site structure as shown in the following screenshot:

  6. Now, from your browser, let’s switch to our SharePoint Intranet:
    http://intranet.contoso.com.
  7. From the home page’s Quick launch, click the Shared Documents link.
  8. On the ribbon, click the Library tab and select Settings | Library Settings.
  9. Take note of the newly associated content type added to the Content Types area of the library settings, as shown in the following screenshot:

  10. 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.
  11. From the ribbons Documents tab, click Manage | Edit Properties.
  12. Take note of how the item now has the Content Type option available, where you can pick newly provisioned Org Document content type.
  13. 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:

$ContentType = New-Object Microsoft.SharePoint.SPContentType -
ArgumentList @($DocumentContentType, $RootWeb.ContentTypes, "Org Document")

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:

$OrgDivision = $OrgFields.Add("Division", [Microsoft.SharePoint.SPFieldType]::Choice, $false, $false, $choices)

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:

$OrgBranch = $OrgFields.Add("Branch", [Microsoft.SharePoint.SPFieldType]::Text, $false)

We add both fields to the content type using the following commands:

$OrgDocumentContentType.FieldLinks.Add($OrgDivisionObject)
$OrgDocumentContentType.FieldLinks.Add($OrgBranchObject)

The last part is to associate the newly created content type to a library, in our case Shared Documents. We use the following command to associate the content type to the library:

$association = $SPList.ContentTypes.Add($OrgDocumentContentType)

To ensure the content types on the list are enabled, we set the ContentTypesEnabled property of the list to $true.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here