【问题标题】:Sharepoint development using PowerShell使用 PowerShell 进行共享点开发
【发布时间】:2019-08-30 11:20:03
【问题描述】:

我正在尝试使用 powershell 创建共享点 webpart。但是每次我加载 (Context.Load()) 东西时,我都会遇到错误。

这是我的代码。

[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location))
[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location))

$tenantAdmin = "#####@ajoncloud.onmicrosoft.com"
$tenantAdminPassword = "#########"
$secureAdminPassword = $(convertto-securestring $tenantAdminPassword -asplaintext -force)
$siteURL = "https://ajoncloud.sharepoint.com/LND"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin,$secureAdminPassword) 
$ctx.Credentials = $credentials

这部分工作得很好。但是如果我尝试以下语句,我会得到错误。

$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()

这是我遇到的错误。

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."

【问题讨论】:

    标签: asp.net powershell sharepoint csom


    【解决方案1】:

    当我自己尝试时,您的代码运行良好。也许尝试下面的代码并尝试在您的 SharePoint 中获取特定的部分。指定某个列表并尝试获取字段值。起初我在运行您的代码时遇到错误,但是当我尝试不同的用户并指定以下列表时,它开始为两个用户工作。

    [Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location))
    [Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location))
    
    $tenantAdmin = "your admin account"
    $tenantAdminPassword = "your admin password"
    $secureAdminPassword = $(ConvertTo-SecureString $tenantAdminPassword -AsPlainText -Force)
    $siteURL = "your site/subsite"
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
    $ctx.credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin,$secureAdminPassword) 
    
    $listTitle = "The name of your list" 
    $list = $ctx.Web.Lists.GetByTitle($listTitle)
    $fields = $list.Fields
    $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000,'ID','Created','Modified','Title')
    $items = $list.GetItems($qry)
    $ctx.Load($fields)
    $ctx.Load($items)
    $ctx.ExecuteQuery()
    
    foreach($item in $items){
        $item.FieldValues.Title
    }
    

    【讨论】:

    • 我试过这个,但在执行 ExecuteQuery() 语句时又遇到了同样的错误。错误:使用“0”参数调用“ExecuteQuery”的异常:“远程服务器返回错误:(403) Forbidden。”
    • 您确定您有足够的权限吗?另外,您确定您填写了正确的网址吗?可以尝试使用管理员帐户吗?
    • 是的,我有足够的权利。我能够使用 C# 成功执行这些语句,但不能使用 powershell。
    • 您使用的是哪个版本的 PowerShell?
    • 所以我不知道发生了什么,我重新启动了我的系统,它现在可以工作了。
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多