1

I need to create a SharePoint list with ~200 columns. Each column has a different name, and they all are choice types (with the same choice list values; same default value). Is there a way to do it in any way except to do it one by one, manually?

I managed to define the list via import of Excel file, but then all the columns are single-text-line. I need a Choice...

Michael

  • 1
    I guess this could be done using Powershell. Did you try? – Deepu Nair Jul 23 '18 at 4:28
0

This can be done with PowerShell. Here is some sample code that I used. This must be run on one of the SharePoint servers. If you are using SharePoint Online, it will need to be converted to client side code.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Add-FieldToList($SiteURL,$ListName, $FieldName, $FieldType, $IsRequired, $Choices, $Default)
{

    #Set the Error Action
    $ErrorActionPreference = "Stop"

    Try {

        #Get the List

        $List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)

        #Check if List with specific name exists
        if($List -ne $null)
        {
            if(!$List.Fields.ContainsField($FieldName))
            {
                #Add columns to the List
                if ($FieldType -eq [Microsoft.SharePoint.SPFieldType]::Choice)
                {
                    $newFieldName = $List.Fields.Add($FieldName,$FieldType,$IsRequired,$False,$Choices)
                }
                else
                {
                    $newFieldName = $List.Fields.Add($FieldName,$FieldType,$IsRequired)
                }

                if ($FieldType -eq [Microsoft.SharePoint.SPFieldType]::Note)
                {
                    $newField = $List.Fields.GetFieldByInternalName($newFieldName)
                    $newField.RichText = $false
                    $newField.Update()
                }

                if ($FieldType -eq [Microsoft.SharePoint.SPFieldType]::DateTime)
                {
                    $newField = $List.Fields.GetFieldByInternalName($newFieldName)
                    $newField.DisplayFormat = "DateOnly"
                    $newField.Update()
                }

        if ($Default -ne $null)
        {
                    $newField = $List.Fields.GetFieldByInternalName($newFieldName)
                    $newField.DefaultValue = $Default
                    $newField.Update()
        }

                #Update the List
                $List.Update()

                write-host "New Column '$FieldName' Added to the List!" -ForegroundColor Green
            }
            else
            {
                write-host "Field '$FieldName' Already Exists in the List" -ForegroundColor Red
            }
        }
        else
        {
            write-host "List '$ListName' doesn't exists!" -ForegroundColor Red
        }
    }
     catch {
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
    finally {
        #Reset the Error Action to Default
        $ErrorActionPreference = "Continue"
    }
}

#Parameters

$SiteURL="https://yoururl/yoursite"
$ListName = "Your List"

$Choices = New-Object System.Collections.Specialized.StringCollection
$Choices.Add("Yes")
$Choices.Add("No")
$Choices.Add("N/A")
$FieldType = [Microsoft.SharePoint.SPFieldType]::Choice
$FieldName="Your Field Name"
$IsRequired = $False
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired $Choices "N/A"
  • thank you! I am off on vacation but will try when I return. I don't have access to the server-side, and am a pretty basic user of SharePoint. What do I need to do to change this to client-side code? – user2978183 Jul 24 '18 at 18:54
0

I would also look at PNP tools they have many functions built in already (https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps).

I have used the tool to create lists in the past example below:

Add-PnPField -List $NameOfSite -DisplayName "Date of Entry" -InternalName "DateOfEntery" -Type DateTime -AddToDefaultView

Add-PnPField -List $NameOfSite -DisplayName "Date Required" -InternalName "DateRequired" -Type DateTime -AddToDefaultView

Add-PnPField -List $NameOfSite -DisplayName "Requester" -InternalName "Requester" -Type User -AddToDefaultView

Add-PnPField -List $NameOfSite -DisplayName "Assigned To" -InternalName "AssignedTo" -Type User -AddToDefaultView

Add-PnPField -List $NameOfSite -DisplayName "Item Details" -InternalName "ItemDetials" -Type Note -AddToDefaultView

Add-PnPField -List $NameOfSite -DisplayName "Response/Notes" -InternalName "ResponseNotes" -Type Note -AddToDefaultView

Add-PnPField -List $NameOfSite -DisplayName "SRC Status" -InternalName "Status" -Type Choice -AddToDefaultView -Choices "In progress","Completed"

  • I agree. I just happened to have the code already written (in the other answer), but using PnP makes it even easier. – mannaggia Jul 23 '18 at 19:13
  • thank you! I am off on vacation but will try when I return. – user2978183 Jul 24 '18 at 18:55
  • Andrew - did this line work for you? (added arrows since this interface does not support line breaks...). ----> Add-PnPField -List $NameOfSite -DisplayName "SRC Status" -InternalName "Status" -Type Choice -AddToDefaultView -Choices "In progress","Completed" <------ I get an error. The field is generated as a Choice file, but the choices are not updated (I get an empty choice list). – user2978183 Aug 8 '18 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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