Hi all:
I'm currently having trouble deleting a content type from a Sharepoint list programmatically. What I wanted to do is that I want to install a newer version of a custom content type (it has extra fields), so I opted to delete the original one and re-add the latest one. Below is the code:
bool requireUpdate = false
SPContentType tempCtId = existingWeb.AvailableContentTypes["Item"];
List<SPListITem> itemsWithTEmpCt = new List<SPListItem>();
foreach (SPListItem item in listToUpdate.Items)
{
if (item.ContentType.Name.Equals(ctToUpdate))
{
item["Content Type"] = "Item";
itemsWithTempCt.Add(item);
requireUpdate = true;
}
}
if (requireUpdate)
{
foreach (SPListItem itemWithTempCt in itemsWithTempCt)
{
itemWithTEmpCt.File.CheckOut();
itemWithTempCt.Update();
itemWithTEmpCt.File.CheckIn("converted ct");
}
}
// reset vars
requireUpdate = false;
itemsWithTempCt = new List<SPListItem>();
// changing the folder's ct, if any
foreach (SPListItem folder in listToUpdate.Folders)
{
if (folder.ContentType.Name.Equals(ctToUpdate))
{
folder["Content Type"] = "Item";
itemsWithTempCt.Add(folder);
requireUpdate = true;
}
}
if (requireUpdate)
{
foreach (SPListItem itemWithTempCt in itemsWithTempCt)
{
itemWithTempCt.Update();
}
}
listToUpdate.ContentTypes[ctToUpdate].Delete(); //<--- exception here
SPContentType contentTypeToSet = web.AvailableContentTypes[ctToUpdate];
listToUpdate.ContentTypes.Delete(tempCtId.Id);
listToUpdate.ContentTypes.Add(contentTypeToSet);
//....
I opted to add an Item content type because there was only 1 content type on that list (hence I cannot delete it). What happened was that at the line where it says "exception here", an exception was thrown:
Content Type is still in use.
After some googling, I have tried many different scenarios, but none of them worked so far:
-folders that have version == 1.0 did not get their content type updated... hence content type still in use (fair enough).
-all documents and folders have their content types set to "Item", but it still complained content type is still in use. Nothing in recycle bin, no minor version of files (the doc lib does not allow it), no permission problems.
-the items are in a workflow, though they have been completed.
The list is located at the site level e.g. web.ParentWeb will get you the list. I have absolutely no ideas what can still be using the content type. Does anyone have any ideas/or encountered such problem before?
Thanks.