繁体   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