2012-07-27 78 views
3

我试着将这些代码片段的PowerShell:如何使用SharePoint Client对象模型和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(); 

但我得到了更新(错误)和添加($文件)方法

+1

你能展示你想出的代码和你收到的错误吗? – 2012-07-30 12:11:24

回答

0

相反与使用客户端对象模型相比,使用PowerShell将文件上传到SharePoint的可接受方式是使用SharePoint PowerShell Cmdlts。您可以使用类似于下面的代码的东西:

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?) 

对于您可以用它来上传文件,覆盖,审批和检查,使用我在这里创造了cmdlet的可重复使用的功能: http://pastebin.com/Tvb4LfZV

0

如果您在服务器上运行,您将只能使用SharePoint PowerShell cmdlet。他们不能远程工作,特别是对于SharePoint Online。

您没有包含所有的PowerShell代码,但我猜你在每次获取列表并将文件加载到服务器之后都没有调用ClientContext.Load和ClientContext.ExecuteQuery。

Reference for using many PowerShell CSOM scenarios

相关问题