Archive

Archive for the ‘Content Type’ Category

How To: Remove Content Type from List / Library Programmatically

July 30, 2013 4 comments

Guys,

Quick code for removing default content type from list / library.

Generally, we need it while we create new list / library with our custom Content Type on feature activation for our custom solution.

 /// <summary>
 /// Removes the default CT from list.
 /// </summary>
 /// <param name="currentWeb">Represent current web of web application</param>
 /// <param name="listNames">Represent list names from where default content type need to remove</param>
 private void RemoveDefaultCTFromList(SPWeb currentWeb, List<string> listNames)
 {
 for (int i = 0; i < listNames.Count; i++)
 {
 try
 {
 SPList list = currentWeb.Lists.TryGetList(listNames[i]);
 if (list != null)
 {
 SPContentType itemCT = list.ContentTypes["Item"];
 if (itemCT != null)
 {
 list.ContentTypes.Delete(itemCT.Id);
 list.Update();
 }

 list.EnableFolderCreation = false;
 list.Update();
 }
 else
 {
 Log.LogWriter(null, string.Format("List '{0}' does not exist in current web", listNames[i]));
 }
 }
 catch (Exception ex)
 {
  // Error handling code
 }
 }
 }

Hope, it will help to somebody to quick reuse.

Happy SharePointing !!!