SharePoint Online: Create List Item based on Specific Content Type
Requirement: Add list item with specific content type in SharePoint Online.
How to Add a New List Item of a specific content type in SharePoint Online?
Say we have a customized content type “Business Contacts” for the contacts list and want to add a new item of the given content type. To create a new list item of a specific content type in SharePoint Online, Simply select the desired content type from the “New” menu of that list!
In my case, I’ve selected “Business Contacts” from the “New” item drop down instead of simply clicking on the “New” button of the list.
PowerShell to create a list item of particular content type:
Are you looking for a PowerShell script to create a list item of a particular content type in SharePoint Online? This blog post will show you how to use PowerShell to create a list item of the “Business Contacts” content type in a SharePoint Online list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Contacts"
$ContentTypeName="Business Contacts"
Try {
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get the content type from list
$ContentTypeColl = $List.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()
#Get the content type
$CType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
If($CType -ne $Null)
{
#Create a List Item of specific content type
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $List.AddItem($ListItemInfo)
#Set List Item Fields - Internal Names
$ListItem["Title"] = "Ruta"
$ListItem["FirstName"] = "Graciela"
$ListItem["Company"] = "Buckley Miller & Wright"
$ListItem["WorkPhone"] = "440-780-8425"
$ListItem["Email"] = "[email protected]"
#Set Content type for the Item
$ListItem["ContentTypeId"] = $CType.ID
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-host "New Item Added to the List!" -ForegroundColor Green
}
else
{
Write-host "Content Type Doesn't exist in the List!" -ForegroundColor Yellow
}
}
Catch {
write-host -f Red "Error Adding Items to List!" $_.Exception.Message
}
Salaudeen, thank you for this Post.
My problem is, that new items always created with the default content type and not with the selected one.
Do you have any advise?
Yes! That’s how it works. The default content type will be used when you create a new item. You have to set your content type as “Default”. How to set the Default Content type in SharePoint Online?