gpt4 book ai didi

object - 如何使用 SharePoint 客户端对象模型和 PowerShell 将文档上传到库?

转载 作者:行者123 更新时间:2023-12-02 05:50:40 26 4
gpt4 key购买 nike

我尝试将这些代码片段转换为 PowerShell:

ClientContext context = new ClientContext("http://spdevinwin");
2:
3: Web web = context.Web;
4:
5: FileCreationInformation newFile = new FileCreationInformation();
6: newFile.Content = System.IO.File.ReadAllBytes(@"C:\Work\Files\17580_FAST2010_S05_Administration.pptx");
7: newFile.Url = "17580_FAST2010_S05_Administration 4MB file uploaded via client OM.pptx";
8:
9: List docs = web.Lists.GetByTitle("Documents");
10: Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
11: context.Load(uploadFile);
12: context.ExecuteQuery();
13: Console.WriteLine("done");

和:

// FileInfo in System.IO namespace
var fileCreationInformation = new FileCreationInformation();

byte[] bytefile = System.IO.File.ReadAllBytes(“c:\\test\Test2.txt”);
fileCreationInformation.Content = bytefile;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = “http://astro/MyLibrary/MyFolder/Test2.txt”;

// CurrentList is a client OM List object
CurrentList.RootFolder.Files.Add(fileCreationInformation);
Context.ExecuteQuery();

但是我在 Update() 和 Add($file) 方法上遇到错误

最佳答案

使用 PowerShell 将文件上传到 SharePoint 的可接受方法是使用 SharePoint PowerShell Cmdlt,而不是使用客户端对象模型。您可以使用类似于以下代码的内容:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$Library = "My Library"
$siteurl = "http://MyWebApp/sites/SiteName"
$FilePath = "C:\Test\test2.txt"

# Get Web site
$Web = Get-SPWeb $SiteUrl

# Get Library
$docLibrary = $web.Lists[$Library]
$folder = $docLibrary.RootFolder

# Get the file
$File = Get-ChildItem $FilePath

# Build the destination SharePoint path
$SPFilePath = ($folder.URL + "/" + $File.Name)

$spFile = $folder.Files.Add($SPFilePath, $File.OpenRead(), $true) #Upload with overwrite = $true (SP file path, File stream, Overwrite?)

对于可用于上传文件、覆盖、批准和 checkin 的可重用函数,请使用我在此处创建的 cmdlet: http://pastebin.com/Tvb4LfZV

关于object - 如何使用 SharePoint 客户端对象模型和 PowerShell 将文档上传到库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11686305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com