需要将文档从文件共享上载到SharePoint 2013库,并将元数据放入特定列 [英] Need to upload documents from a file share into a SharePoint 2013 library and put metadata into certain columns

查看:74
本文介绍了需要将文档从文件共享上载到SharePoint 2013库,并将元数据放入特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个我每周安排一次的powershell脚本来做这件事。我对powershell很新,所以这是我从其他人那里借来的,并试图靠自己做,但我不确定我在正确的轨道上

I want to do this with a powershell script that i schedule once a week.Im pretty new to powershell,so this is what i have borrowed from other people and tried to do on my own, but im not sure im on the right track

$ webUrl =" mysite"

$ docLibraryName =" mylibrary"
$
$ docLibraryUrlName =" \ mylibrary"

$ localFolderPath =" \\server\Temp \"
$
$ outputFile =" CSVFile.csv"





#export metadata

Get-ChildItem $ localFolderPath -Recurse | Select-Object manufacturingPlant,BatchNo,ProcessedDate |

$webUrl = "mysite"
$docLibraryName = "mylibrary"
$docLibraryUrlName = "\mylibrary"
$localFolderPath = "\\server\Temp\"
$outputFile = "CSVFile.csv"


#export metadata
Get-ChildItem $localFolderPath -Recurse | Select-Object manufacturingPlant, BatchNo,ProcessedDate|

Export-Csv $ outputFile -Encoding ascii -NoTypeInformation



$
#Open web and library

$ web = Get-SPWeb $ webUrl

$ docLibrary = $ web.Lists [$ docLibraryName]
$




$ files =([System.IO.DirectoryInfo](Get-Item $ localFolderPath))。GetFiles()

ForEach($ file中的文件)

{

#Open file

$ fileStream =([System.IO.FileInfo](Get-Item $ file.FullName))。OpenRead()



#Add file

$ folder =  $ web.getfolder($ docLibraryUrlName)



写主机"复制文件" $ file.Name"到" $ folder.ServerRelativeUrl" ..."

$ spFile = $ folder.Files.Add($ folder.Url +" /" + $ file.Name,[System.IO。 Stream] $ fileStream,$ true)

Export-Csv $outputFile -Encoding ascii -NoTypeInformation


#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]


$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
ForEach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

#Add file
$folder =  $web.getfolder($docLibraryUrlName)

write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)

#Get CSV文件内容并迭代CSV中的每一行

#Get CSV file contents and Iterate through each Row in the CSV

$ metadata = import-csv $ outputfile 

ForEach($ metadatas中的元数据){

$
$ manufacturingPlant = $($ metadata.manufacturingPlant )

$ BatchNo = $($ metadata.BatchNo)

$ ProcessedDate = $($ metadata.ProcessedDate)

$ metaoutput = @ {" DirectoryName" = $ metadata.Directory; "长度" = $ metadata.Length}



$ uploaded = $ docLibraryName.Files.Add($ metadata.FileName,$ metaoutput,$ TRUE)

write-host"成功"



$
#关闭文件流

$ fileStream.Close() ;

}

#Dispose web

$ web.Dispose()

$metadata = import-csv $outputfile 
ForEach ($metadata in $metadatas){

$manufacturingPlant = $($metadata.manufacturingPlant )
$BatchNo = $($metadata.BatchNo)
$ProcessedDate = $($metadata.ProcessedDate)
$metaoutput = @{"DirectoryName" = $metadata.Directory; "Length" = $metadata.Length}

$uploaded = $docLibraryName.Files.Add($metadata.FileName,$metaoutput, $TRUE)
write-host "Success"


#Close file stream
$fileStream.Close();
}
#Dispose web
$web.Dispose()


在PowerShell中没有编译器帮助你需要在每一步之后逐步进行测试:


推荐答案

:首先实现逻辑枚举指定文件中的文件共享和测试,然后实现将每个文件上传到Sharepoint站点上的指定doclib
的部分并进行测试,之后实现逻辑,将元数据分配给上传的文档并再次测试。在测试时向控制台(Write-Host)或日志文件添加跟踪(参见例如

将输出写入PowerShell中的文件
。)
in PowerShell where there is no compiler's help you have to go step by step with testing after each step: at first implement logic which enumerates files in specified file share and test, then implement part which uploads each file to specified doclib on Sharepoint site and test, after that implement logic which assigns metadata to the uploaded documents and again test. While testing add traces to console (Write-Host) or to log file (see e.g. Write output to the file in PowerShell).


这篇关于需要将文档从文件共享上载到SharePoint 2013库,并将元数据放入特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆