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). Steps Involved:
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.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: