none
Check if column exists, if column does not exist create column RRS feed

  • Question

  • Hello colleagues,

    I am a beginner in PowerShell and am astonished at how powerful this tool is.

    Look at this scenario: I need to go through a collection of sites, search all lists that are of type "DocumentLibrary", check if there exists the column with the name of the server "DocumentCategories" and if there is no I must create this column. Look what I've developed so far:

    $webUrl = "http://myurl"
    $site = Get-SPSite $webUrl
    
    write "Set libraries"
    $baseWeb = $site.OpenWeb("my_url_extend")
    
    Function ProcessLibraries {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory = $true)] 
            $BaseWeb
        )
        write $BaseWeb.ServerRelativeUrl
        $docLibs = $BaseWeb.Lists | ? { $_.BaseType -eq 'DocumentLibrary' }
    
        $docLibs | % {
            $list = $_
            write $list.RootFolder.Url  
    
    
    LOST HERE....HELP... 
    By testing the variable $docLibs, it works perfectly bringing all the Document Libraries. From there I was unable to check inside each of them to see if they have the "DocumentCategories" field. I ask the masters for help.


    • Edited by SPTuga Tuesday, June 4, 2019 4:16 PM
    • Moved by Richard MuellerMVP Tuesday, June 4, 2019 11:50 PM Not a TechNet Wiki question
    • Moved by jrv Wednesday, June 5, 2019 2:52 AM Correct forum
    Tuesday, June 4, 2019 4:14 PM

Answers

  • Hi

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    $webUrl = "https://siteurl"
    $listname = "listname"
    $fieldname = "fieldname"
    $IsRequired = $False
    $FieldType = [Microsoft.SharePoint.SPFieldType]::Text
    
    $site = Get-SPSite $webUrl
    $Web = $site.OpenWeb()
    $list = $web.Lists.TryGetList($listname)
    $field = $list.Fields[$fieldname];
    
    if($field -eq $null)
    {
        $list.Fields.Add($fieldname,$FieldType,$IsRequired)
        $list.Update()
        Write-Host $fieldname "is created"
    }
    else
    {
        Write-Host $fieldname "is already exist"
    }


    Please remember to mark the replies as answers if they helped.
    Linyu - SharePoint Engineer
    QQ Group: 72637444

    • Marked as answer by SPTuga Wednesday, June 26, 2019 4:42 PM
    Thursday, June 6, 2019 7:39 AM
  • You can use the below script to do it for all the Document Libraries in the site excluding the SharePoint hidden libraries.

    Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
    $url = "http://site collection url"
    $fieldname = "DocumentCategories"
    $site = Get-SPSite $url
    $web = $site.RootWeb
    $lists = $web.Lists
    for($i=0;$i -lt $lists.Count;$i++)
    {
        $name = $lists[$i].Title
        if(($lists[$i].GetType().Name -eq "SPDocumentLibrary") -and ($lists[$i].Hidden -ne $true))
        {
            $f = $lists[$i].Fields[$fieldname]
            if($f-eq $null)
            {
                $lists[$i].Fields.Add("$fieldname","Text",0)
                $lists[$i].Update()
                Write-Host "Field $fieldname added successfully in $name Document Library"
            }
            else
            {
                Write-Host "Field $fieldname already exists in $name Document Library"
            }
        }
    }

    Best Regards,


    Vaibhav Antriwale

    • Marked as answer by SPTuga Wednesday, June 26, 2019 4:42 PM
    Thursday, June 6, 2019 9:58 AM

All replies

  • I don't have local environment so I can only suggest the way. But don't worry, other experts like Dennis will provide a more detailed script.

    When you get the libraries collection, you need loop it one by one.

    To one library, get the .fields collection or check if there is any getbyname method. If not, you have to loop it too to find if the field exist.

    For field adding, you may need to call .fields.add method.


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Thursday, June 6, 2019 1:11 AM
  • Hi

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    $webUrl = "https://siteurl"
    $listname = "listname"
    $fieldname = "fieldname"
    $IsRequired = $False
    $FieldType = [Microsoft.SharePoint.SPFieldType]::Text
    
    $site = Get-SPSite $webUrl
    $Web = $site.OpenWeb()
    $list = $web.Lists.TryGetList($listname)
    $field = $list.Fields[$fieldname];
    
    if($field -eq $null)
    {
        $list.Fields.Add($fieldname,$FieldType,$IsRequired)
        $list.Update()
        Write-Host $fieldname "is created"
    }
    else
    {
        Write-Host $fieldname "is already exist"
    }


    Please remember to mark the replies as answers if they helped.
    Linyu - SharePoint Engineer
    QQ Group: 72637444

    • Marked as answer by SPTuga Wednesday, June 26, 2019 4:42 PM
    Thursday, June 6, 2019 7:39 AM
  • You can use the below script to do it for all the Document Libraries in the site excluding the SharePoint hidden libraries.

    Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
    $url = "http://site collection url"
    $fieldname = "DocumentCategories"
    $site = Get-SPSite $url
    $web = $site.RootWeb
    $lists = $web.Lists
    for($i=0;$i -lt $lists.Count;$i++)
    {
        $name = $lists[$i].Title
        if(($lists[$i].GetType().Name -eq "SPDocumentLibrary") -and ($lists[$i].Hidden -ne $true))
        {
            $f = $lists[$i].Fields[$fieldname]
            if($f-eq $null)
            {
                $lists[$i].Fields.Add("$fieldname","Text",0)
                $lists[$i].Update()
                Write-Host "Field $fieldname added successfully in $name Document Library"
            }
            else
            {
                Write-Host "Field $fieldname already exists in $name Document Library"
            }
        }
    }

    Best Regards,


    Vaibhav Antriwale

    • Marked as answer by SPTuga Wednesday, June 26, 2019 4:42 PM
    Thursday, June 6, 2019 9:58 AM