当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:根据特定内容类型创建列表项

作者:精品下载站 日期:2024-12-14 14:31:16 浏览:15 分类:玩电脑

SharePoint Online:根据特定内容类型创建列表项


要求:在 SharePoint Online 中添加具有特定内容类型的列表项。

如何在 SharePoint Online 中添加特定内容类型的新列表项?

假设我们有一个自定义的内容类型,即联系人列表的“商务联系人”,并且想要添加给定内容类型的新项目。要在 SharePoint Online 中创建特定内容类型的新列表项,只需从该列表的“新建”菜单中选择所需的内容类型即可!

[玩转系统] SharePoint Online:根据特定内容类型创建列表项

就我而言,我从“新建”项目下拉列表中选择了“业务联系人”,而不是简单地单击列表中的“新建”按钮。

PowerShell 创建特定内容类型的列表项:

您是否正在寻找 PowerShell 脚本来在 SharePoint Online 中创建特定内容类型的列表项?本博文将向您展示如何使用 PowerShell 在 SharePoint Online 列表中创建“业务联系人”内容类型的列表项。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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
} 

PnP PowerShell 从特定内容类型添加列表项


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ContentTypeName = "Business Contacts"
$ListName = "Contacts"
 
#Connect to SharePoint Online Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Create List Item based on specific content type
Add-PnPListItem -List $ListName -ContentType $ContentTypeName -Values @{"Title" = "Ruta" ; "FirstName" ="Graciela"} 

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯