In this article, we are going to discuss an important and useful feature of SharePoint; i.e., Content Type. As we all know, the content type is the set of columns that we can reuse in all lists and libraries in a site. We are going to create a Content List and its column and will add this content type to all custom lists present in the site. Follow the below steps and code snippets to perform the above action.
Step 1
Set the credential by giving input of user ID and Password for SharePoint.
Step 2
Get the web ysing client context.
Step 3
The function CreateSpContentType () is used for the creation of List content type using the unique ID & Group. Here the title of the content type is set as “Employee Info”.
Step 4
The function CreateSiteField () is used then to create the fields of the content type which will represent the employee details. First, we must create site columns & then we can add it to the content type fields.
Step 5
After Creating the content type and its field, the function AddContentType () is used to attach the content type with all available custom lists in the site.
In this function, first we have to get all custom lists present in the site by using the base id (for custom List the Base Id=100) & then for adding the content type into the list we have to get the content type by using its unique ID which we used previously while creating the content type. (The Property ContentTypesEnabled should set to be true).
After running the code, we can check the content type in Site content type option under site setting. Then clicking on the content type title (Employee Info), we can check the field of content type. Please refer to the below image for better reference.