0

I have added a new column in library in SharePoint 2010 site using powershell. The column is getting added, however content type is not getting updated for the libraries. When I try to get new column in workflow, it is not showing up. Any advise on this?

I have used below script:

$ver = $host | select version 
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"} 
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
{ 
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" 
Add-PSSnapin Microsoft.SharePoint.PowerShell 
} 
$SourceWebURL = "http://sitecollection/sites/site" 
$ssite = Get-SPSite $SourceWebURL
$sweb = $ssite.OpenWeb("subsite")
for ($i = 0; $i -lt $sweb.Lists.Count; $i++)
#foreach($sList in $sweb.Lists) 
{
$sList = $sweb.Lists[$i];   

    if($sList.BaseType -eq "DocumentLibrary")
            {
                $spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
                $sList.Fields.Add("Name",$spFieldType,$false)
                $sList.Update()
                $f = $sList.Fields["Name"]
                $f.DefaultValue =$sList.Title
                $f.Update()
            }
}
2
  • Did you add the column to content type? Can you share what you have done so far? Mar 24, 2015 at 7:17
  • @NadeemYousuf: I have updated the question
    – Aquarius24
    Mar 24, 2015 at 7:55

2 Answers 2

1

PowerShell code to add a list field to content type:

$field = $list.Fields[$FieldName]
$listCT = $list.ContentTypes[$ContentTypeName];
$link = new-object Microsoft.SharePoint.SPFieldLink($field)
$listCT.FieldLinks.Add($link)
$listCT.Update($true)
$list.Update()
4
  • I am getting error - You cannot called method on null valued expression in $listCT.FieldLinks.Add($link)
    – Aquarius24
    Mar 24, 2015 at 11:11
  • Make sure that you are passing correct content type name in $listCT = $list.ContentTypes[$ContentTypeName];. The error may occur due to null value in $listCT. Check if it is properly initialized. Mar 24, 2015 at 11:29
  • I am passing it as - $listCT = $sList.ContentTypes["Document"];
    – Aquarius24
    Mar 24, 2015 at 11:31
  • Are content types allowed in the list? Mar 24, 2015 at 11:49
0

The lists inherit from the content type, so when you update a content type, you can (optionally) choose to propagate the update to the lists. But if you update a list, you can't propagate to the parent (which is the content type).

So, if you want to push the change to your content type, update the specific content type (if it's an out-of-the-box one, I'll recommend you not to change it, create a new one that inherits from it instead) and then push the changes to the lists that inherits from this content type.

For that, and after referencing your content type and making all the change that you want, you can call the method update(true) like this :

oContentType.Update(true);

the boolean 'true' indicates that you want to push the changes to all the child content type.

Your Answer

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

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