In this chapter, we will be covering the Site Columns. So far, we have been defining the list and library schemas on the lists and libraries themselves, but these are not reusable. Therefore, if you want to have two lists with the same schema, we would have to define the same schema twice. SharePoint has a solution for this, which is Site Columns and Content Types.
Site Columns define reusable column definitions and Content Types, which are made up of Site Columns, define reusable schemas for both lists and libraries.
In addition to defining schema, you can also attach workflows and event handlers to a Content Type. Site Columns and Content Types are stored in galleries at the site level and they are available to be used within that site and any of its children.
If you declare a Site Column or a Content Type in a child site, it is only available in the branch underneath that.
Unless there is a specific reason not to do so, the common practice is to declare your Site Columns and Content Types in the site collection root and that way they are available across the entire site collection.
Now let us have a look at a simple example in which we will create and use Site Columns and Content Types. We have already seen Content Types, although it may not have been obvious
Step 1 − Go to our Contacts list through Site Contents.
Step 2 − If you scroll down, you will see a section called Columns.
Step 3 − Go up to Advanced Settings. Select Yes for Allow Management of Content Types, and click OK.
You will see that we have a new section here called Content Types. This indicates that this list is based on the Contact Content Type.
Step 4 − Go to Site Settings.
Step 5 − Under Web Designer Galleries, click Site Content Types.
Step 6 − Scroll down the page and you will find the Contact Content Type, which is right there under List Content Types and then click the Contact link.
Step 7 − If you take a look at the columns then you will see that it matches the columns in our list. Basically, when you create a list of the Contacts list template, it associates this content type with the list and that is why you get all of these fields.
Now the fields here that make up the definition of a Content Type are known as Site Columns.
To see the Site Columns, let us go to Site Settings and select Site Columns under Web Designer Galleries,
Step 8 − You can see the columns that are associated with Contacts. So let us explore this a little bit further by creating our own custom site column and our own custom content type and then using those in lists. In our Course Documents Library, we have a column for the course and we defined this column in the library itself.
Step 9 − Maybe while building out your site, you realize that you want to have a course column in a few lists and libraries and you want to reuse that definition. Hence, what we can do is create the course column as a site column and then use it in different lists and libraries.
Let us go to the Site Column from the Site Settings.
Step 10 − Click the Create link.
Step 11 − Name this as the Course column and it will be a lookup field.
Step 12 − Put this into a group called “SharePoint Basics” so that we can find it easily later. It will look up on the Courses list, and the field we want to look up is the Title. Click OK.
Step 13 − You will see that a new group SharePoint Basics is created.
Step 14 − Our new site column is created in the “SharePoint Basics” group.
Step 15 − Let us go back to Course Documents and then go to the Library Settings. Go to Columns.
Remove the Course column, which we created in the library itself.
Step 16 − Let us add the new course column from the site columns. Therefore, instead of clicking Create Column, click Add from existing site columns.
Step 17 − Change the group to “SharePoint Basics” and Select Course on the left side. Click Add to add that column to the list and then click OK.
Step 18 − Let us go back to Course Documents.
Step 19 − You can see our new Course column, but it is empty because the information that was here previously was deleted when we deleted the original course column. Hence, let us add that back as shown below.
If this list had hundreds of items, it would turn a task that could take hours into a task that could take minutes.
In this section, we will take a look at creating a custom content type.
Step 1 − Let us go to the Site Settings, and then go to Site Content Types. Click the Create link.
There are two key things to consider here when we are creating a content type.
The first is that all content types are based on another content type or you could think of it as all content types inherit from another content type.
The second is that a content type will either work with lists or it will work with libraries. Whether it works with lists or libraries depends on the type you inherit from.
For example, if we want to create a custom contacts list, we would go to the List Content Types and then find Contact. We would create the content type, add things we need that were not already part of Contact, or remove things that were part of Contact that we do not want.
One strategy you can use while creating content types is to find a content type that already has most of the things that you need, inherit from it, and then customize it.
The other strategy is to start with a base content type and you just build on top of it.
In case of lists, that is the Item Content type. For Libraries, you want to inherit from Document, so we will go to Document Content Types.
Step 2 − Our content type is going to be for document libraries related to Courses. This is what we want here in terms of inheritance.
Step 3 − Let us call this Course Documents Type. Just as we did with site columns, we will put this into a group so we can easily identify it and that group name will be “SharePoint Basics”. Click OK.
Step 4 − You can see in the following screenshot that a couple of fields already exist from the Document Content Type, File Name and Title. We will add the Course field. Now, here we cannot just add a column to a content type, the column has to be a site column. Hence, we are going to choose Add from existing site columns and then apply filter on the “SharePoint Basics” group.
Step 5 − Select the Course column, click Add, and then click OK.
In this case, this is all the customization we want to do to our content type, so now we are ready to use it.
Step 6 − Let us create a new document library by clicking Site Contents → add an app and create a document library.
Step 7 − We will call this Library Test, and click Create. Open the test library and set the course document type to content type for this library. Go to Library on the Ribbon and then go to Library Settings.
Step 8 − To manage content types, go to Advanced Settings.
Step 9 − Set Allow Management of Content Types to Yes and then click OK.
Step 10 − You can see that this library is currently associated with the Document content type. We need to add our Course content type, by clicking Add from existing site content types.
Step 11 − Filter it again in “SharePoint Basics”. Select Course Documents Type, click Add, and then click OK.
So now, our library is associated with two content types, the Document content type and the Course documents type.
Step 12 − Next, go to the Test library and then click New Document.
When you click New Document or the dropdown arrow, you can see that we can create a document of either type. Now if you only want people to be able to create course documents, then just go back to the Library Settings. Remove the Document content type association by clicking on the Document in Content types section.
Step 13 − Click Delete This Content Type. Go back to the Library and click Files, and then click New Document.
Now you can see that only the Course Document Type option is available. These are the basics of working with content types in SharePoint.