2012-07-27 2 views
3

나는 PowerShell에서 이러한 코드 조각을 변환 시도했다 :SharePoint 클라이언트 개체 모델과 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

당신은 당신이 해낸 코드와 수신 한 오류를 표시 할 수? –

답변

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