How to change the content type for files in SharePoint


In this article we will be seeing how to change a batch of files in a document library from one content type to another using C#.

I have batch of files in a document library which belongs to the "Document "content type. I want to change the content type from "Document" to "_CustomContentType (which is a custom content type).

Share1.gif

Steps Involved:

  • Open Visual Studio 2010.
  • Create a console application.
  • Replace the code with the following.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;

    namespace ChangeContentType
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (SPSite site = new SPSite("http://serverName:1111/SitePages/Home.aspx"))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList list = web.Lists["Shared Documents"];
                        SPContentType oldCT = list.ContentTypes["Document"];
                        SPContentType newCT = list.ContentTypes["_CustomContentType"];
                        SPContentTypeId newCTID = newCT.Id;
                        if ((oldCT != null) && (newCT != null))
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (item.ContentType.Name == oldCT.Name)
                                {
                                    if (item.File.CheckOutType.ToString() == "None")
                                    {
                                        item.File.CheckOut();
                                        item["ContentTypeId"] = newCTID;
                                        item.Update();
                                        item.File.CheckIn("Checked in");
                                    }
                                    else
                                    {
                                        Console.WriteLine("File " + item.Name + "is checked out to" + item.File.CheckedOutByUser.ToString() + " and
    cannot be modified"
    );
                                    }
                                }
                                else
                                {
                                        Console.WriteLine("File "+item.Name+ " is associated with the content type "+ item.ContentType.Name+ " and
    shall not be modified"
    );
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("One of the content types specified has not been attached to the list "+list.Title);
                        }
                        Console.ReadLine();
                    }
                }
            }
        }
    }


    • Hit F5.

    Share2.gif