谁能告诉我该怎么做?我发现的例子似乎都不起作用。
我的网站是https://blah.sharepoint.com/technology,我的列表称为“pfa”。我想添加一些文本列。
我确实通过我的 Powershell 与 SharePoint Online 建立了连接,因为我已经设法从各种命令中获得了一些结果。
谢谢。
谁能告诉我该怎么做?我发现的例子似乎都不起作用。
我的网站是https://blah.sharepoint.com/technology,我的列表称为“pfa”。我想添加一些文本列。
我确实通过我的 Powershell 与 SharePoint Online 建立了连接,因为我已经设法从各种命令中获得了一些结果。
谢谢。
CSOM API 附带:
添加列表或网站栏。
下面的示例演示了如何将GeoLocation
字段添加到Contacts
列表中:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
function Provision-Field([Microsoft.SharePoint.Client.ClientContext]$Context,[string]$ListTitle,[string]$FieldSchema)
{
$list = $Context.Web.Lists.GetByTitle($ListTitle)
$List.Fields.AddFieldAsXml($FieldSchema,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Context.Load($List)
$Context.ExecuteQuery()
}
$UserName = "username@contoso.onmicrosoft.com"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$URL = "https://contoso.sharepoint.com/"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($URL)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
$Context.Credentials = $Credentials
Provision-Field $Context "Contacts" "<Field Type='Geolocation' DisplayName='Location'/>"
端点:
http://<site url>/_api/web/fields('<field id>')
http://<site url>/_api/web/lists(guid'<list id>')/fields('<field id>')
从 PowerShell 使用 SharePoint 2013 REST API 一文介绍了如何将 HTTPS 请求发送到 SharePoint REST Web 服务。
使用文章中的Invoke-RestSPO函数,以下示例演示了如何在 PowerShell 中使用 REST API 将注释字段添加到列表:
Function Add-SPOField(){
Param(
[Parameter(Mandatory=$True)]
[String]$WebUrl,
[Parameter(Mandatory=$True)]
[String]$UserName,
[Parameter(Mandatory=$False)]
[String]$Password,
[Parameter(Mandatory=$True)]
[String]$ListTitle,
[Parameter(Mandatory=$True)]
[String]$FieldTitle,
[Parameter(Mandatory=$True)]
[System.Int32]$FieldType
)
$fieldMetadata = @{
__metadata = @{'type' = 'SP.Field' };
Title = $FieldTitle;
FieldTypeKind = $FieldType;
} | ConvertTo-Json
$Url = $WebUrl + "_api/web/Lists/GetByTitle('" + $ListTitle + "')/fields"
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password
Invoke-RestSPO $Url Post $UserName $Password $fieldMetadata $contextInfo.GetContextWebInformation.FormDigestValue
}
. ".\Invoke-RestSPO.ps1" #InInvoke-RestSPO function
$UserName = "username@contoso.onmicrosoft.com"
$Password = Read-Host -Prompt "Please enter your password"
$WebUrl = "https://contoso.sharepoint.com/"
Add-SPOField -WebUrl $WebUrl -UserName $UserName -Password $Password -ListTitle "Documents" -FieldTitle "Comments" -FieldType 3
这是通过 PowerShell 使用 SharePoint Oneline 创建了 CSOM 列表的人http://www.hartsteve.com/2013/06/sharepoint-online-powershell/
SharePoint online 的 PowerShell 内容仅限于一些基本的管理任务。当您在 PowerShell 中使用 CSOM 时,您可以做更多事情。