Powershell to add/delete content type to particular library alone in a site collection

Hi,
I am trying to add and delete content type from a particular picture library (site collection) using powershell.
I have tried the below, but it is not working.
Below scenarios are not working:
1) If the content type to be added is there in library, then the loop is still moving to "No content type exists".
2) Content type is not getting deleted from library
3) Set the newly added content type as default content type.
Below is the powershell, which I am trying:
function ChangeContentType($url)
$site = Get-SPSite($url);
$web =$site.RootWeb
$lookForList = "PicLibrary"
$lookForCT = "Img1ContentType"
$lookForRemoveCT = "Image2CT"
write-host "Checking site:"$web.Title
#Make sure content types are allowed on the list specified
$docLibrary = $web.Lists[$lookForList]
if ($docLibrary -ne $null)
$docLibrary.ContentTypesEnabled = $true
$docLibrary.Update()
if (($web.ContentTypes | where { $web.Name -eq $lookForCT }) -eq $null)
write-host "No content type exists with the name" $lookForCT "on list" $docLibrary.Title
#Add site content types to the list
write-host "Adding content type " $lookForCT "on list" $docLibrary.Title
$ctToAdd = $web.ContentTypes[$lookForCT]
$ct = $docLibrary.ContentTypes.Add($ctToAdd)
write-host "Content type" $ct.Name "added to list" $docLibrary.Title
$docLibrary.Update();
else
write-host "content type exists with the name" $lookForCT "on list" $docLibrary.Title
#Remove Content type
if (($web.ContentTypes | where { $web.Name -eq $lookForRemoveCT }) -eq $null)
write-host "No content type exists with the name" $lookForRemoveCT "on list" $docLibrary.Title
else
$ctToRemove = $web.ContentTypes[$lookForRemoveCT]
write-host "Removing content type" $ctToRemove.Name "from list" $docLibrary.Title
$docLibrary.ContentTypes.Delete($ctToRemove.Id)
$docLibrary.Update()
else
write-host "The list" $lookForList "does not exist in site" $web.Title
#Dispose object
$site.Dispose()
$web.Dispose()
How to fix this?
Thanks

Hi,
Thanks for the reply.
I checked it. If the script is run multiple times, the add and delete content type will throw error.
How to delete content type? I have tried the below:
$docLibrary = $web.Lists[$lookForList]
if ($docLibrary -ne $null)
$docLibrary.ContentTypesEnabled = $true
$docLibrary.Update()
#get the content Type
Write-host "Getting content type $lookForCT in site $web"
#Confirm that the content type exists.
$contentType = $web.ContentTypes | where {$web.ContentTypes.Name -eq $lookForCT}
##Abort if not found.
if ($contentType -eq $null)
Write-host "$lookForCT not found in site collection."
else
if ($web.ContentTypes.Name -eq $lookForCT)
Write-host $docLibrary.ContentTypes.Name
if($docLibrary.ContentTypes.Name -eq $lookForCT)
write-host "Content type $lookForCT is already added to list" $docLibrary.Title
else
write-host "No content type exists with the name" $lookForCT "on list" $docLibrary.Title
write-host "Adding Content Type"
$ctToAdd = $web.ContentTypes[$lookForCT]
$ct = $docLibrary.ContentTypes.Add($ctToAdd)
$docLibrary.Update()
write-host "Content type" $ct.Name "added to list" $docLibrary.Title
#get the content Type
Write-host "Getting content type $lookForRemoveCT in site $web"
#Confirm that the content type exists.
$contentType = $web.ContentTypes | where {$web.ContentTypes.Name -eq $lookForRemoveCT}
##Abort if not found.
if ($contentType -eq $null)
Write-host "$lookForRemoveCT not found in site collection."
else
if ($web.ContentTypes.Name -eq $lookForRemoveCT)
Write-host $docLibrary.ContentTypes.Name
if($docLibrary.ContentTypes.Name -eq $lookForRemoveCT)
write-host "Content type $lookForRemoveCT is added to list" $docLibrary.Title.
write-host "Removing Content type $lookForRemoveCT"
$ctToRemove = $web.ContentTypes[$lookForRemoveCT]
$docLibrary.ContentTypes.Delete($ctToRemove.Id)
$docLibrary.Update()
else
write-host "No content type exists with the name" $lookForRemoveCT "on list" $docLibrary.Title
I am getting error in delete now. Error is "Exception calling "Delete" with "1" argument(s): "Specified argument was out of the range of valid values.
Parameter name: id"
Thanks

Similar Messages

  • Assign default content type to any library automactially when site get created or people add the library

    we are on on premise environment, and we have a requirement that when people create a site or a sub site or even when they add a new document library , we want to ensure our custom content type is assigned as default content type to this library.
    the question is that is there any non-full trust solution, i know we probably can do it via feature stapling, or custom site template stuff. but they are NOT considered as we want PLA compliance

    Hi,
    As I understand, you would like to asscoiate a custom content type to a library when the library is created.
    With OOB option, you could try add this content type to an existing library, and save library as template. Then the library template should appear in Your Apps page, that is to say, users can directly create document library based on this template.
    As I tested, new document library created based on this template contains custom content type by default.
    With programatically methods, please refer to the similar threads below:
    https://social.technet.microsoft.com/Forums/en-US/ec9e243b-ea4c-4a46-8780-75a3a5659164/mysite-add-content-type-to-document-library-by-default?forum=sharepointadminprevious
    https://social.technet.microsoft.com/Forums/en-US/e58eb107-daa5-4a51-a139-dca2b1cf8591/set-default-content-type-for-document-libraries-on-a-custom-site-template?forum=sharepointdevelopmentprevious
    Regards,Please
    remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected] .
    Rebecca Tu
    TechNet Community Support

  • Can I add Custom Content Type created programmatically to Custom List Definition?

    I have created Custom content type programmatically in the Feature Receiver. Followed by this have another feature which creates List Template. I want to add the custom content type to the list template.
    Using VS 2012, I added the custom content type to the list definition. The Schema.xml for the list definition looks as below:
    <ContentTypes>
    <ContentTypeRef ID="0x010100D7D9F4B1F4A9684BB44389571024B2EC00C393BB21B8AD7B41B62A87DF0501504D" />
    </ContentTypes>
    ID was automatically added by the VS tool.
    List Template is created, the new content Type is also added, but the Name is displayed as "Document" instead of the custom content type name. So I see two CTs with the name "Document".
    How can I achieve this? Any help is appreciated.

    Hi,
    Please add EnableContentTypes="TRUE" in the <List > tag.
    The following materials would be helpful:
    How to add custom content type to a custom list
    http://innersharepoint.blogspot.de/2009/10/how-to-add-custom-content-type-to.html
    SharePoint Custom List Definition with Content Type
    https://achrafsp.wordpress.com/2013/03/31/sharepoint-list-definition-with-content-type/
    Create a Custom SharePoint List Definition
    http://www.mssharepointtips.com/tip.asp?id=1188
    Best Regards
    Dennis Guo
    TechNet Community Support

  • Mismatch between the Content Database size and the total of each site collection' storage used.

    Hi All,
    Environment:  SharePoint 2010 with SP2.
    Issue: One of the Content databases size in our farm shows 200 GB as used. There are 25 site collections in the DB and we counted
    the total storage used of all site collections in that Content DB and is not more than 40 GB. (used "enumsites" command and counted the each sitecollections storage used).
    What actions/troubleshooting were done?
    Ran one script which will find the actual size of site collection and how much space used in the disk. But didn’t find major difference in the report.
    Checked “Deleted from end user recycle bin “in all the site collections and no major storage is noticed.
    Planning to Detach the problematic Content DB and attach ,will check whether any major effect is there
    Why the Content DB shows 200 GB as used when the total storage used of all site collections is just 40 GB.
    Appreciate suggestions from any one.
    Best Regards,
    Pavan Kumar Sapara.
    s p kumar

    Hi,
    Thanks for your reply.
    As there is only 20 MB of unallocated space for the above said DB, SQL DB team informed that they cannot perform the DB shrink at this moment.
    So we are thinking to Offload all the site collections to other new DB and then will Drop the problematic database. In this way we can overcome the
    issue.
    Answer for your queries.
    Are the mismatched sizes causing an issue? Are you short on diskspace for DB storage or SQL backups?
    No, there is no issue with the mismatched sizes. We are not on short on disk space. We just worried why it occupies that much size(200 GB) when
    the total storage used of all site collections in that DB is 40 GB.
    Best Regards,
    Pavan Kumar Sapara.
    s p kumar

  • How to show content of a list which resides in other site collection in Office 365?

    Hi All,
    Can anyone help on How to show content of a list which resides in other site collection in Office 365 . Using Rest and Soap services this is not possible as we have limitations in office 365.So if anyone have any idea please let me know.
    Note : We are not allowed to use CSOM for this requirement.
    Many Thanks,
    Vinod.

    Hi Vinod,
    You can use cross site publishing feature of SharePoint 2013.
    http://blogs.msdn.com/b/justinvoels/archive/2012/09/24/sharepoint-2013-search-driven-publishing-and-cross_2d00_site-collection-publishing-overview.aspx
    https://technet.microsoft.com/en-us/library/jj635883.aspx
    Alternative to cross-site publishing in SP Online
    https://www.youtube.com/watch?v=chwHhEmIERg
    Best Regards,
    Brij K
    http://bloggerbrij.blogspot.co.uk/

  • How do I add a content Type (PDF) to the Applications in Firefox preferences

    When i Try to open/download a PDF file I get a blank new tab.
    I checked the Applications Tab in preferences and I do not have PDF files in the Content type. No problem in downlodading/viewing PDF files from gmail.

    Hello George,
    This tutorial should step you through the basic process of configuring the device and connecting the thermocouple:
    Tutorial: Connect Thermocouples to a Data Acquisition (DAQ) Device
    http://www.ni.com/gettingstarted/setuphardware/dataacquisition/thermocouples.htm#Connecting a Thermocouple to Your Device
    From there, there are a number of things you can do- I'd recommend taking a look at the LabVIEW shipping examples (Help>>Find Examples...) as well as the DAQmx getting started tutorials:
    Getting Started with NI-DAQmx: Main Page
    http://www.ni.com/white-paper/5434/en
    At first glance, the 6363 you're using should have enough resolution to acquire usable data from a thermocouple- if you attempt reading raw voltages be sure that the acquisition range is configured for +/- 0.1V, though.
    Regards,
    Tom L.

  • Looking for help with PowerShell script to delete folders in a Document Library

    I'd like to create a PowerShell script to delete old folders in a Document library that are over 30 days old. Has anyone created something like this?
    Orange County District Attorney

    Hello Sid:
    I am trying to do the same and Iam running the script to delete the subfolders inside a folder  but I have some errors. 
    Could you please take a look?
    _______Script________
    $web = Get-SPWeb -Identity https://myportal.mydomain.com
    $list = $web.GetList("ar_mailingactivity")
    $query =  New-Object Microsoft.SharePoint.SPQuery 
    $camlQuery = '<Where><And><Eq><FieldRef Name="ContentType" /><Value Type="Computed">Folder</Value></Eq><Leq><FieldRef Name="Created" /><Value Type="DateTime"><Today OffsetDays="-30" /></Value></Leq></And></Where>'
    $query.Query = $camlQuery
    $items = $list.GetItems($query)
    for ($intIndex = $items.Count - 1; $intIndex -gt -1; $intIndex--)
       $items.Delete($intIndex);
    ________Errors_______
    Unable to index into an object of type System.Management.Automation.PSMethod.
    At C:\Script.ps1:2 char:22
    + $list =$webGetList <<<< "ar_mailingactivity"]
    + CategoryInfo
    :InvalidOperation: (ar_mailingactivity:String) [], RuntimeException
    + FullyQualifiedErrorID
    :CannotIndex
    You cannot call a method on  a null-valued expression.
    At c:\Script.ps1:6 char:24
    + $items = $list.GetItems <<<< ($query)
    + CategoryInfo
    :InvalidOperation: (GetItems:String) [], RuntimeException
    + FullyQualifiedErrorID
    :InvokeMethodOnNull

  • How to make New Document and Upload Document to have same Content Type in Document Library in Sharepoint 2010

    Hi,
    How to make 'New Document' and 'Upload Document' to have same content type(Custom) in Document Library in Sharepoint2010.
    Note : I have created custom Content Type since I have custom columns along with upload column..
    Regards, Shreyas R S

    go to library settings 
    Change new button order and default content type
    set your custom content type to be #1
    when you upload new document it will automatically have your custom content type
    Hope that helps|Amr Fouad|MCTS,MCPD sharePoint 2010

  • How to assign permissions to choice values when using content types in a library

    Hi,
    I just now started using content types in document libraries instead of folder structure.  The structure I was given is as follows:
    Master group (library name)
    L.A.
      South Street
           Number 1
           Number 2
      North Street
           Number 1
           Number 2
    Boston
      West Street
           Number 1
           Number 2
    East street
           Number 1
           Number 2
    India
      North Street
           Number 1
           Number 2
      West Street
           Number 1
           Number 2
    So, I created a Content Type called Areas (which included LA, Boston, and India) and I created choice field values for each columns (South street and North street).  Then, I gave the choice values as Number 1 and Number 2.
    Now, the complexity was in terms of permission levels, wherein the client was accustomed to using folder structure.  And hence, they had given a set to users to have access only to Number 1 of West street of Boston.  Now that I have the content
    type structure in place, how do I configure the granular permission for the 3rd level of the hierarchy?
    Shonilchi..

    Thanks for your post. I had already done this via Windows Explorer, and it works for all of the My Docs files
    but not Acrobat files, which still come up in List View. I am running Windows XP still, is that a factor?

  • Max number of content types per list/library

    What is the maximum number of content types for a list or library?
    I read a thread on this forum where they talk about a maximum of 2000 content types per library for SP 2013:
    http://social.msdn.microsoft.com/Forums/en-US/c53df140-8ed6-4438-8a49-95cdc6ac5fd5/number-of-content-types-per-library?forum=sharepointgeneral
    Is this also true for lists in SP 2010?
    Thanks for your help!
    Thorsten

    Hello Thorsten,
    I don't see a hard limit in the capacity management article on Technet:
    http://technet.microsoft.com/en-us/library/cc262787(v=office.14).aspx#ListLibrary
    But I do see limits to number of columns, which of course has a correlation to content types.
    - Dennis | Netherlands | Blog |
    Twitter

  • SharePoint 2010 Content Type Hub

    Hi,
    I have a Content Type Hub in the SharePoint Server and it is being subscribed to one of the Web application and it’s site collections.
    If I make any changes in existing column of the Content Type in Content Type Hub and subscribe to the required site collection, the changes are not being propagated to libraries columns which are part the attached Content Type from the Conte Type Hub.
    But the Content Types are being subscribed and changes are available at Site column of the Subscribed Sites but not in the existing Library level.
    If I add the New column in CT hub and the new column appears in the existing Library of the subscribed sites.
    Please assist me how can I propagate the Content Type changes from CT Hub to the existing Library of the subscribed site?
    Regards,
    Vidya Lokesh

    It's a known issue.
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/b865e6a0-39d8-4aae-9df6-f07ff9d1d96c/content-type-changes-are-not-reflected-in-subscribing-document-libraries?forum=sharepointgeneralprevious
    MS chose to avoid pushing down to lists and libraries, presumably to avoid people trashing their systems by making silly changes and pushing them down to thousands of lists and libraries.
    The only surefire way is to delete the CT from the list/library and re-add it. You could script something up to do that I guess but I don't know of any examples of it floating about.
    PS. You're in the wrong forum, this relates to 2010 not 2013, although in this case that's not a major issue.

  • Content Types

    So here is my scenario. We are a small business (3 people) who develop learning content and want to use a SP doc library to manage it.
    Our hierarchy of content is as follows:
    Curriculum 1
         ----> Course 1
                -----> Lesson 1
                -----> Lesson 2
        -----> Course 2
                ------> Lesson 3
               ------> Lesson 4
    Curriculum 2 with courses and lessons and so on...
    Our document is a Lesson. Lessons are used in courses. Courses are used in a Curriculum.
    My though was that I could use Content Types for Courses that will contain all Course Names and the same for Curriculum for all Curriculum names as it will it a lot easier as we add more and more Lessons to the document Library.
    But never having used content types before I am not sure how to do that. I know where the Content Types are added but I don't know how to add the data to the content types and then attach it to the Lesson documents.
    Anyone have any ideas on what I might need to do?

    it all depends on the total number of docs, docs/course, courses/curr, etc.But just at a glance of what you've shown here, I would say you will definitely want to use document sets, to enable them, activate the site collection level feature named document
    sets.  this is essentially a folder turned into a content type, so you can store documents within the "document" (set).  great for metadata and properties in this scenario, as you can set a property on the document set and have all of the content
    within it share that property value.  you could then set more specific metadata on the sub-items.  I would think something like this might work:
    1 document library for each curriculum with custom columns that are specific to the entire curriculum as a whole (you could create a library with the columns, then save it as a template to reuse for new curriculum without having to redo the column settings). 
    also, make sure to allow management of content types on the library, this will let you specify which content types are allowed
    1 content type which inherits from document set CT.  set the columns which would be used for the course, you can also mark if those are shared to children or just apply to the doc set itself.
    1 content type which is a child of Document which is fairly generic, but has the basic columns which apply to all your documents, from there, if you wanted, you could create children CTs of this to allow for a more customizable feel for different types of
    lessons.  maybe an online lesson vs inperson lesson uses different types of metadata columns, etc (this type of project is really all based on your business needs and extremely specific to each individual company/dept, etc.  people quite frequently
    hire consultants and bus. analysts specifically to design this hierarchy)
    add the content types to your library and start messing about creating them and test how you like the shareing of properties, and views, etc.  if you're going it alone on this in a small business, there will be a lot of trial & error and just getting
    a feel for what you want/need.  hopefully we can help you through it here in the forums
    for reference (also, fyi, libraries are the same as lists, but with docs.  so setting things up are the same either way.  Also, 90% of the stuff you find for 2010 in this area will also work or be extremely similar in 2013).  there's a lot
    here, I would just skim some of it and try things out yourself.:
    http://msdn.microsoft.com/en-us/library/ff630940(v=office.14).aspx - Create Lists with Custom Content Types in SharePoint 2010
    http://office.microsoft.com/en-us/sharepoint-designer-help/create-and-edit-content-types-HA101774775.aspx - create/edit custom content
    types with SharePoint Designer
    http://www.learningsharepoint.com/2010/10/22/create-content-type-in-sharepoint-2010/ - quick walkthrough from creating the columsn, content type, then adding
    to a list/library
    http://www.sharepointnoob.com/2012/03/29/howto-create-site-column-sharepoint-2010/ - creating site columns
    http://office.microsoft.com/en-us/sharepoint-server-help/introduction-to-document-sets-HA101782466.aspx - intro to document sets
    http://msdn.microsoft.com/en-us/library/office/ee559339(v=office.14).aspx - more in depth on doc sets
    http://technet.microsoft.com/en-us/library/cc262873(v=office.14).aspx - planning information architecture for SP
    http://technet.microsoft.com/en-us/library/cc262900(v=office.15).aspx - information management & governance in SP
    Christopher Webb | MCM: SharePoint 2010 | MCSM: SharePoint Charter | MCT | http://christophermichaelwebb.com

  • Cannot delete Document Content Type. Getting error "Document Content Type Still in use" powershell, sharepoint 2013

    not able to delete "Document" content type from a library. I have a custom content type. I have added the custom content type to all the items of library. also, i have added the custom columns from "Document" content type to my custom
    content type. Still cannot delete the "Document" content type. I am doing the same process in different site collections and different libraries. But not able to delete in a few say- 10 in 100. Get the error - "Document content type is still
    in use"

    Hello 
    Here you go
    http://blog.octavie.nl/index.php/2012/09/14/error-the-content-type-is-in-use-explained
    https://social.technet.microsoft.com/Forums/en-US/e81020e3-2c12-4f39-a2f4-f1fd88ba6547/content-type-is-still-in-use-on-document-library
    Please remember to click 'Mark as Answer' on the answer if it helps you

  • Deploying a Reusable Workflow to a List Content Type using PowerShell

    We have a situation where deployment of a reusable workflow for a site content type cannot be completed through the web interface due to the number of libraries where the content type is in use (time-out on deploy and update).
    It was hoped that this could be accomplished with PowerShell but the method of deploying to a list content type appears to be different than it is to a list (all content types).
    The below snippet works fine for a list / all content types:
    function AddWorkflowToLibraries ($SiteCollection, $ctName, $WfName, $WfAssociationName)
    $site = Get-SPSite $SiteCollection
    [Guid]$wfTemplateId = New-Object Guid
    #Step through each web in site collection
    $site | Get-SPWeb -limit all | ForEach-Object {
    $web = $_
    $_.Lists | ForEach-Object{
    if($_.AllowContentTypes -eq $true)
    if($_.ContentTypes.Item("$ctName") -ne $null)
    write-host "Enabling workflow on" $_.Title "in" $_.ParentWebUrl
    $ct = $_.ContentTypes[$ctName]
    $culture = New-Object System.Globalization.CultureInfo("en-US")
    $template = $site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)
    if($template -ne $null)
    $tasklist = "Tasks"
    $historylist = "Workflow History"
    if(!$web.Lists[$historylist])
    $web.Lists.Add($historylist, "A system library used to store workflow history information that is created in this site. It is created by the Publishing feature.",
    "WorkflowHistory", "00BFEA71-4EA5-48D4-A4AD-305CF7030140", 140, "100")
    if (!$web.Features["00BFEA71-4EA5-48D4-A4AD-305CF7030140"]) {
    Enable-SPFeature -Identity WorkflowHistoryList -Url $web.Url
    $wfHistory = $web.Lists[$historylist]
    $wfHistory.Hidden = $true
    $wfHistory.Update()
    if(!$web.Lists[$tasklist])
    $web.Lists.Add($tasklist, "This system library was created by the Publishing feature to store workflow tasks that are created in this site.", "WorkflowTasks", "00BFEA71-A83E-497E-9BA0-7A5C597D0107", 107, "100")
    $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])
    $association.AllowManual = $true
    $_.AddWorkflowAssociation($association)
    $_.Update()
    else
    Write-Error "Workflow Template not found"
    AddWorkflowToLibraries <Site Name> <Content Type Name> <Workflow Template Name> <Association Name>
    However changing the association as follows causes the script to still execute without a problem but the workflow doesn't appear for the content and the associations collection is empty:
    function AddWorkflowToLibraries ($SiteCollection, $ctName, $WfName, $WfAssociationName)
    $site = Get-SPSite $SiteCollection
    [Guid]$wfTemplateId = New-Object Guid
    #Step through each web in site collection
    $site | Get-SPWeb -limit all | ForEach-Object {
    $web = $_
    $_.Lists | ForEach-Object{
    if($_.AllowContentTypes -eq $true)
    if($_.ContentTypes.Item("$ctName") -ne $null)
    write-host "Enabling workflow on" $_.Title "in" $_.ParentWebUrl
    $ct = $_.ContentTypes[$ctName]
    $culture = New-Object System.Globalization.CultureInfo("en-US")
    $template = $site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)
    if($template -ne $null)
    $tasklist = "Tasks"
    $historylist = "Workflow History"
    if(!$web.Lists[$historylist])
    $web.Lists.Add($historylist, "A system library used to store workflow history information that is created in this site. It is created by the Publishing feature.",
    "WorkflowHistory", "00BFEA71-4EA5-48D4-A4AD-305CF7030140", 140, "100")
    if (!$web.Features["00BFEA71-4EA5-48D4-A4AD-305CF7030140"]) {
    Enable-SPFeature -Identity WorkflowHistoryList -Url $web.Url
    $wfHistory = $web.Lists[$historylist]
    $wfHistory.Hidden = $true
    $wfHistory.Update()
    if(!$web.Lists[$tasklist])
    $web.Lists.Add($tasklist, "This system library was created by the Publishing feature to store workflow tasks that are created in this site.", "WorkflowTasks", "00BFEA71-A83E-497E-9BA0-7A5C597D0107", 107, "100")
    $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])
    $association.AllowManual = $true
    $_.ContentTypes[$ctname].AddWorkflowAssociation($association)
    $_.ContentTypes[$ctname].Update()
    else
    Write-Error "Workflow Template not found"
    AddWorkflowToLibraries <Site Name> <Content Type Name> <Workflow Template Name> <Association Name>
    The only change is:
    $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])
    $association.AllowManual = $true
    $_.ContentTypes[$ctname].AddWorkflowAssociation($association)
    $_.ContentTypes[$ctname].Update()
    But unlike the list version, the association doesn't appear to be saved and no error is generated.
    Is anyone aware of what may cause this or have an example in C# that may explain something my script is missing?

    Hi Garry,
    After you associate the workflow to the content type, you should update the update the content type using
    $ct.UpdateWorkflowAssociationsOnChildren($true,$true,$true,$false)
     method.
    Here is the completed script:
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $site=Get-SPSite "http://serverName"
    $web=$site.OpenWeb()
    $list=$web.Lists["ListC"]
    $taskList=$web.Lists["Tasks"]
    $historyList=$web.Lists["Workflow History"]
    $ct=$list.ContentTypes["Link"]
    $culture=New-Object System.Globalization.CultureInfo("en-US")
    $wfTemplate=$web.WorkflowTemplates.GetTemplateByName("Three-State",$culture)
    $associationWF=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($wfTemplate, "myThreeStateWF",$taskList,$historyList)
    $ct.WorkflowAssociations.Add($associationWF)
    $ct.UpdateWorkflowAssociationsOnChildren($true,$true,$true,$false)
    Here is a demo about how to update it using C#
    http://www.thorntontechnical.com/tech/sharepoint/sharepoint-2010-associate-workflow-to-content-type-in-a-feature
    Wayne Fan
    TechNet Community Support

  • Cant delete orphaned content type, it says : "The content type is part of an application feature."

    I created site from existing site template that previous vendor working with company created. I could figure that they saved site as template and when you do that sharepoint creates sandboxed solution. When I create site based on that template, it adds orphaned
    content type and when I try to delete it, it gives error : "The content type is part of an application feature.".
    I looked on internet but solutions dont seem to fit in my scenario.
    Following are possible solutions I read on net.
    1) Deactivate feature that has content type and all dependencies for content type and then delete content type --- In my case there is no feature in feature galary that you can deactivate as it is part of site template.
    2)Get the WSP and edit in visual studio, I am working on client machine and I do not have access to sharepoint server. When I try to create new visual studio project with "Import sharepoint solution package", it says Sharepoint server is not installed
    on this computer. I tried to get Microsoft.Sharepoint dll from Microsoft virtual lab but I can only work on web client and windows client doesnt work. Web client is not letting me copy files into my local desktop.
    Any suggestions ? I just need to take that content type out and generate WSP and upload it back
    Dhaval Raval

    Below is how I recreated problem , my findings and solution to this problem (Everything is done through Sharepoint UI)
    Problem :
    Create site and create custom content type using sharepoint UI.
    Save site as template.
    Use above template to create new site.
    New site contains orphaned content type, when you try to delete that unnecessary content type sharepoint gives error: The content type is part of an application feature. Sharepoint will not let you delete this content type.
    Delete original site from which template was created.
    Findings:
    When you save site as template, sharepoint creates and activates Sandboxed solution in solution gallery of Root Site.
    Sandbox solution has same name that you gave when saving site as template.
    Activation of Sandboxed solution adds feature having name of original site from which template was created, and this is Site Collection Feature. I could not find any site feature for it and also I could not find any site feature for custom content type
    as well. I think when you create content type through Sharepoint UI, it doesnt create site feature for that.
    Deactivating site collection feature for that site template doesn't let you delete that orphaned content type from newly created site.
    Solution:
    If you deactivate Sandboxed solution from Solutions gallery, it will remove corresponding site collection feature as well as that orphaned content type from site. Reactivating that solution doesn't add orphaned content type back to newly created site.
    Above process only deletes orphaned content type from newly created existing site but you will have same problem when you create more new sites from that site template.
    I would say once your problem is fixed with deleting content type, solution is to replace site template. In this way newly created sites wont have same problem again.
    Dhaval Raval

Maybe you are looking for