簡體   English   中英

使用 powershell 將文件上傳到 SharePoint 文檔庫

[英]Upload file to SharePoint Document library using powershell

我想將同一文件上傳到所有網站集中具有相同層次結構的多個網站集。 我想使用 PowerShell 並包含自動簽入/簽出功能。

我已經能夠在 SharePoint 中上傳文件。 下面是代碼。

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

# create the Variable Path and Pass the source folder path
$path = “D:\ABC\DEF\26Nov\”;

# create the Variable destination and pass the URL of the SharePoint List
$destination = "complete URL of File will be mentioned here";

# Store the current user default credentials in the Variable Credentials
$credentials = [System.Net.CredentialCache]::DefaultCredentials;

# Create the object of the Webclient
$webclient = New-Object System.Net.WebClient;

# Pass the user credentials
$webclient.Credentials = $credentials; Get-ChildItem

# “For Each” loop will upload all of the files one by one onto the destination using the UploadFile method
Get-ChildItem $path | ForEach-Object { $webclient.UploadFile($destination + “/” + $_.Name, “PUT”, $_.FullName)};

通過此代碼,文件已上傳但已簽出。 我希望它自動簽入。 如果文件在那里,則首先自動簽出,然后簽入。

這是外行風格的簡單腳本,經過測試並且可以正常工作,可以將文件從驅動器上傳到 SharePoint 文檔庫

http://soreddymanjunath.blogspot.in/2014/07/add-file-to-document-library-using.html

cls

asnp "*sh*"

$url=Read-Host "Enter Site Url" 

$web=Get-SPWeb -Identity $url

if($web)
{
try
{
$list = $web.Lists.TryGetList("Documents")

$files = Get-ChildItem -Path "D:\Manju" -Force -Recurse

    foreach ($file in $files)
    {
      $stream = $file.OpenRead()

      $done= $list.RootFolder.Files.Add($file.Name, $stream, $true)

      Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green         

    }
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
}
}

else
{
Write-Host "Site Doesn't exist"
}

$list.Update();

我在我的服務器上成功運行了這個腳本。 它將來自C:\\Users\\student\\Documents\\My Documentsdocx文件擴展名Upload Demo到名為Upload Demo的文檔庫。 上傳后,它會檢查該庫中的所有文檔。

我使用了這兩個參考文獻中的腳本:

.

Add-PSSnapin Microsoft.SharePoint.PowerShell 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
$spWeb = Get-SPWeb -Identity "http://mia-sqlbi.adventureworks.msft/"
$spFolder = $spWeb.GetFolder("Upload Demo")
$spFileCollection = $spFolder.Files 

Get-ChildItem "C:\Users\student\Documents\My Documents" -filter ?*.docx? | ForEach {
    $spFileCollection.Add("Upload Demo/$($_.Name)",$_.OpenRead(),$true) 
} 

function global:Get-SPSite($url) {
    return new-Object Microsoft.SharePoint.SPSite($url)
}

$siteColletion = Get-SPSite("http://mia-sqlbi.adventureworks.msft/");

$folder = $siteColletion.RootWeb.Folders["Upload Demo"];

$collFiles = $folder.Files;

for ($intIndex=0; $intIndex -ne $folder.Count; $intIndex++) {
    if ($folder[$intIndex].CheckedOutBy.LoginName -eq "adventureworks\student") {
      $folder[$intIndex].CheckIn("");
   }
}

$siteColletion.Dispose()

這是它應該是什么樣子的屏幕截圖:

在此處輸入圖片說明

非常感謝您的回復,我嘗試按照您的建議進行操作。

Add-PSSnapin Microsoft.SharePoint.PowerShell     

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

$spWeb = Get-SPWeb -Identity "//abc/enterprise/en-sg/RenderingAssets/" $spFolder =  $spWeb.GetFolder("oneMSCOM") $spFileCollection = $spFolder.Files 

Get-ChildItem "D:\test" -filter ?*.txt? | ForEach {
    $spFileCollection.Add("oneMSCOM/$($.Name)",$.OpenRead(),$true) 
}

function global:Get-SPSite($url){ 
    return new-Object Microsoft.SharePoint.SPSite($url) 
}

$siteColletion = Get-SPSite("://abc/enterprise/en-sg/RenderingAssets/");

$folder = $siteColletion.RootWeb.Folders["Upload Demo"];

$collFiles = $folder.Files;

for ($intIndex=0; $intIndex -ne $folder.Count; $intIndex++) { 
    if ($folder[$intIndex].CheckedOutBy.LoginName -eq "FAREAST\v-kisriv") { 
        $folder[$intIndex].CheckIn(""); 
    }
}

$siteColletion.Dispose()

在此文件應上傳的完整 URL 是:

://abc/enterprise/en-sg/RenderingAssets/oneMSCOM/

RenderingAssets : Document Libray
OneMSCOM : folder.
Add-PSSnapin microsoft.sharepoint.powershell
$web = Get-SPWeb "http://dc01:9876"
$lst = $web.Lists.TryGetList("Style Library")
$DirLoc = "C:\Work\Style Library\20April2015"

$relativeUrl = $lst.RootFolder.ServerRelativeUrl;

function UploadToLibrary($spurl,$filepath){
    Get-ChildItem -path $filepath -Directory |ForEach-Object {
        $FldrName=($spurl,$_.Name -join "/")
        $CheckFolder= $web.GetFolder($FldrName)
        if(-not $CheckFolder.Exists)
        {
            $tmp=$web.GetFolder($spurl).SubFolders.Add($FldrName);
        }
        UploadToLibrary ($spurl,$_.Name -join "/") ($filepath,$_.Name -join "\") 
    }
    $FilesToAdd=$web.GetFolder($spurl)
    Get-ChildItem -path $filepath -File |ForEach-Object {
        $_.FullName
        $bytes = [System.IO.File]::ReadAllBytes($_.FullName)
        $x=$FilesToAdd.Files.Add($_.Name,$bytes,$true);
    }
}

UploadToLibrary $relativeUrl $DirLoc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM