【问题标题】:Upload a file to Sharepoint 2010 with powershell 2.0使用 powershell 2.0 将文件上传到 Sharepoint 2010
【发布时间】:2014-05-12 21:07:45
【问题描述】:

几天以来,我一直在努力使用 powershell 将文件上传到 Sharepoint 2010。 我在一台装有 powershell v2 的 win7 机器上尝试上传到 SP 2010 站点。

我有两个主要问题

  1. $Context.web 值始终为空,即使在 Executequery() 之后也没有 显示错误。我的 $Context 变量获取服务器版本 (14.x.x.x.x),仅此而已
  2. $Context.Load($variable) 总是返回错误找不到“Load”的重载和参数计数:“1”。

我将 Sharepoint DLL 复制到我的 Win7 机器上,并将引用导入到我的脚本中。 下面的脚本是我从网上获取的许多部分的混合。

我已经尝试在定义 Load 方法的客户端上下文上添加重载但没有成功 http://soerennielsen.wordpress.com/2013/08/25/use-csom-from-powershell/

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

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

$site = "https://Root-of-my-site"
$listname = "My-folder"

$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)

[Microsoft.SharePoint.Client.Web]$web = $context.Web
[Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($listName)

$Folder = "C:\temp\Certificates"
$List = $Context.Web.Lists.GetByTitle($listname)

Foreach ($File in (dir $Folder))
{
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.Content = get-content -encoding byte -path $File.Fullname
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}

错误是

找不到“Load”的重载和参数计数:“1”。 在 C:\temp\uploadCertToSharepoint.ps1:48 char:14 + $Context.Load

有人可以帮我解决这个问题吗? 我需要在几周内将大约 400 个带有临时字段的文件上传到共享点站点,而此刻我完全陷入困境。不幸的是,无法运行脚本服务器端。

谢谢, 马可

【问题讨论】:

  • 我在使用您的脚本时没有收到指定的错误。我在其中更改了 1 行,它按预期工作。也许您遇到了其他错误?
  • 糟糕,抱歉,已在 SharePoint 2013 和 Powershell 3 上进行了测试。:)

标签: sharepoint-2010 powershell-2.0 sharepoint-clientobject


【解决方案1】:

由于ClientRuntimeContext.LoadGenerics Method,因此出现此错误:

public void Load<T>(
    T clientObject,
    params Expression<Func<T, Object>>[] retrievals
)
where T : ClientObject

泛型方法在 PowerShell(V1、V2)AFAIK 中原生支持。

解决方法是使用MethodInfo.MakeGenericMethod method 调用通用方法,如文章Invoking Generic Methods on Non-Generic Classes in PowerShell 中所述

ClientRuntimeContext.Load方法的情况下,可以使用以下PS函数:

Function Invoke-LoadMethod() {
param(
   $clientObjectInstance = $(throw “Please provide an Client Object instance on which to invoke the generic method”)
) 
   $ctx = $clientObjectInstance.Context
   $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
   $type = $clientObjectInstance.GetType()
   $clientObjectLoad = $load.MakeGenericMethod($type) 
   $clientObjectLoad.Invoke($ctx,@($clientObjectInstance,$null))
}

然后,在您的示例中,该行:

$Context.Load($Upload)

可以用这个代替:

Invoke-LoadMethod -clientObjectInstance $Upload

参考文献

【讨论】:

    【解决方案2】:

    它会引发错误,因为在 powershell 2.0 中您不能直接调用泛型方法。 您需要使用MakeGenericMethod 创建封闭方法。尝试使用下面的代码。

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    
    $site = "http://server"
    $listname = "listName"
    $Folder = "C:\PS\Test"
    
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
    
    [Microsoft.SharePoint.Client.Web]$web = $context.Web
    [Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($listName)
    
    $method = $Context.GetType().GetMethod("Load")
    $closedMethod = $method.MakeGenericMethod([Microsoft.SharePoint.Client.File])
    
    Foreach ($File in (dir $Folder))
    {
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.Content = (get-content -encoding byte -path $File.Fullname)
        $FileCreationInfo.URL = $File
        $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
        $closedMethod.Invoke($Context, @($Upload, $null) )
        $Context.ExecuteQuery()
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 2013-12-12
      • 2011-08-25
      • 2023-04-06
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      相关资源
      最近更新 更多