kwizcom banner advertisement

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 of specific content type in SharePoint Online?
Say we've a customized content type "Business Contacts" for contacts list and want to add a new item of the given content type. To create new list item of a specific content type in SharePoint Online, Simply select the desired content type from the "New" menu of that list!
sharepoint online add list item with specific content type

In my case, I've selected "Business Contacts" from the "New" item drop down instead of simply clicking on "New" button of the list.

PowerShell to create a list item of particular content type:
#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"] = "gruta@cox.net"

        #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
} 
SharePoint Online: Create List Item based on Specific Content Type SharePoint Online: Create List Item based on Specific Content Type Reviewed by Salaudeen Rajack on March 27, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.