9

I created a content-type and applied it inside a document library. Now, if I go to the conent-type gallery and delete one of the fields, and then go back to the library and view the available columns, I still see the field I deleted listed as a possible column.

How do I update this to match? I don't want to delete the content-type (like in this post); I just want the deployed columns in that content-type to match the current content-type.

1 Answer 1

10

Next time you perform the changes to the ContentType make sure to push changes to the lists while updating the ContentType.

For now, you might need to perform some clean-up job here, just to make sure the field is deleted from all the lists, with your Custom ContentType. Use the following PowerShell script for clean-up! Vårsegod :)

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$site = Get-SPSite "http://your.site.com"
foreach($web in $site.AllWebs)
{ 
    #iterates through all the Lists        
    for ($i = 0; $i -lt $web.Lists.Count; $i++) 
    { 
      $list = $web.Lists[$i];                               
      $MyListContentTypes = $list.ContentTypes                                                          
      if ($MyListContentTypes -ne $null)
      {

         $ct = $list.ContentTypes["You Desired Content Type Name"]                                      
         if($ct)                                                            
         {

             Write-Host "In list Content Type and deleting the field"  $ct.Fields["You Field Name"].Id;
             $ct.FieldLinks.Delete($ct.Fields["You Field Name"].Id)                                  
             $ct.Update()
         }      
       }
    }     
  }

Edit: How to push down changes to all the Content types via SharePoint Server Object Model:

using (SPWeb oWebsite = new SPSite("http://MyServer/sites/MySiteCollection/MyWebSite").OpenWeb())
{
    SPList oList = oWebsite.GetList("MyWebSite/Lists/MyList");
    SPFieldCollection collFields = oWebsite.Fields;

    string strNewColumn = collFields.Add("MyNewColumn", SPFieldType.Text, false);

    SPFieldLink oFieldLink = new SPFieldLink(fields[strNewColumn]);
    SPContentType oContentType = oList.ContentTypes["Specification"];
    oContentType.FieldLinks.Add(oFieldLink);

    oContentType.Update(true);
}

Edit 2: Update a Content Type using PowerShell:

  Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
    //get the specific Root web
    $spweb= Get-SPWeb "http://your.site.com"
    //get the fields collection
    $fields = $spWeb.Fields
    //get the specific field
    $field = $fields.GetFieldByInternalName("Name of you field")
    //get the specific Content Types from Root Web
    $contentType = $spWeb.ContentTypes["You Content Type"]
    //delete a field from a Content Type
    $contentType.FieldLinks.Delete($field) 
   //add a field to a Content Type
    $contentType.FieldLinks.Add($field)
    //update the Content Type and push changes to the children                                     
    $contentType.Update($true)
9
  • Thanks for the script. I tried the "push changes to the lists" thing, but the columns remain. If I go to the library and view the content-type, I can still see the column present - just the source is no longer listed.
    – bgmCoder
    Nov 22, 2012 at 22:28
  • It depends how are you are pushing down the changes. When you edit a site content type through the user interface and make a change on a content type settings page, all the settings contained on that page are overwritten during the push-down operation. However, using the object model provides greater granularity in push-down operations. As you make changes to a site content type through the object model, your code is actually making those changes to the in-memory representation of the site content type. Read more: msdn.microsoft.com/en-us/library/ms442695(v=office.14).aspx Nov 22, 2012 at 22:36
  • Okay, Mr. Mahmood, I'll give that a try and tell you how it goes. When you say, "using the object model" - do you mean "using" it via powershell?
    – bgmCoder
    Nov 22, 2012 at 22:47
  • Object model mean using the SharePoint server object model. I have updated my response. Alternatively, you can also use PowerShell to accomplish the same task. Nov 22, 2012 at 22:52
  • Hmmm... Methinks you posted the second snippet in C#.
    – bgmCoder
    Nov 22, 2012 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.