从远程计算机上载和签入文件到SharePoint

时间:2017-03-16 23:03:38

标签: powershell sharepoint sharepoint-2010

我正在尝试使用Powershell将文件上载和签入到SharePoint Intranet站点。此脚本将在远程计算机上运行,​​而不是托管站点的服务器。在instructions given here之后上传文件相对容易。

以下是我用来上传文件的过程的一小部分。它们已成功上传但仍留给我。无论如何使用Powershell检查这些文件?

$destination = "http://mycompanysite.com/uploadhere/Docs” 
$File = get-childitem “C:\Docs\stuff\To Upload.txt”

# Upload the file 
$webclient = New-Object System.Net.WebClient 
$webclient.UseDefaultCredentials = $true
$webclient.UploadFile($destination + "/" + $File.Name, "PUT", $File.FullName)

不幸的是,使用SharePoint PowerShell模块无法正常工作,因为它在远程计算机上运行。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以更轻松地使用Microsoft.SharePoint.Client,您可以从此处下载:Download SharePoint Server 2013 Client Components

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" #loads sharepoint client runtime
$destination = "http://mycompanysite.com/uploadhere"
$listName = "Docs"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($destination)
$context.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials #signs you in as the currently logged in user on the local host
$context.RequestTimeout = 10000000
$list = $context.Web.Lists.GetByTitle($listName)
$context.Load($list)
$context.ExecuteQuery()

$stream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$fileOptions = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileOptions.Overwrite = $true
$fileOptions.ContentStream = $stream
$fileOptions.URL = $File
$upload = $list.RootFolder.Files.Add($fileOptions)
$context.Load($upload)
$context.ExecuteQuery()

$upload.CheckIn("My check in message", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn) #other options can be looked at here: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.checkintype.aspx
$context.ExecuteQuery() #finally save your checkin