转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/

日常的SharePoint站点测试中,我们经常要做各种各样的数据,今天又写了几个脚本,发现自己写的脚本越来越多,所以我决定整理一下,并把一些常用的可复用的方法陆续发布上来。

今天先讲一下用PowerShell上传文件到SharePoint library中的方法,代码如下:

Add-PSSnapin Microsoft.SharePoint.PowerShell
function CreateAgendaDocumentData
{
param($siteUrl,$listTitle,$filePath,$fileName)
$site = Get-SPSite $siteUrl
$web = $site.rootweb
$List = $web.lists[$listTitle]
$folder = $List.RootFolder
$File= Get-ChildItem $filePath
$fileStream =([System.IO.FileInfo](Get-Item $File.FullName)).OpenRead()
[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.Url + "/" + $fileName, [System.IO.Stream]$fileStream, $true)
$spFile.Item.Update()
$fileStream.Close()
}

以上代码中橘子色的字体是你需要输入的信息,包括SharePoint site的url,list的title,本地文件的路径以及你希望把它上传到SharePoint中所起的新名字。

使用的方法如下:

$siteUrl = "填写SharePoint站点的site url"
$listTitle = "填写想要上传文件的list的title"
$filePath = "填写想要上传的本地文件路径"
CreateAgendaDocumentData -siteUrl $siteUrl -listTitle $listTitle -filePath $filePath

之后就可以看到文件已经上传到指定的list(library)中。

我们可以通过上述方法批量的进行上传文件——Upload a large amount of files to SharePoint.

Add-PSSnapin Microsoft.SharePoint.PowerShell
function CreateDocuments($siteUrl,$listTitle,$filePath,$fileName,$amount)
{
$site = Get-SPSite $siteUrl
$web = $site.rootweb
$List = $web.lists[$listTitle]
$folder = $List.RootFolder
$File= Get-ChildItem $filePath
$fileStream =([System.IO.FileInfo](Get-Item $File.FullName)).OpenRead()
for($i=0;$i -lt $amount;$i++)
{
$newfileName = $fileName + $i.ToString()
[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.Url + "/" + $newfileName, [System.IO.Stream]$fileStream, $true)
$spFile.Item.Update()
}
$fileStream.Close()
}
function CallMethod()
{
$siteUrl = Read-Host "Site URL"
$listTitle = Read-Host "List Title"
$filePath = Read-Host "File Path"
$fileName = Read-Host "File Name Template"
$amount = Read-Host "File Amount"
$amount = [int]$amount
Write-Host "Creating..." -ForegroundColor Green
CreateDocuments $siteUrl $listTitle $filePath $fileName $amount
Write-Host "Finished!" -ForegroundColor Magenta
}
CallMethod

保存到ps1文件中右键通过PowerShell运行即可。

其实在实际操作中,有时不光要上传文件,还需要给其所在item设定相关field的value,这个我以后会单独整理一篇文章来讲解相关的所有操作。

如果大家觉得有帮助,请点个赞,我会陆续写完关于用PowerShell实现SharePoint自动化方面的一系列文章。欢迎大家和我交流,给我提问。

SharePoint自动化系列——Upload files to SharePoint library using PowerShell.的更多相关文章

  1. SharePoint自动化系列——Site/Web/List级别的导航菜单

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 需求:在不同的测试用例中,对脚本中不确定的因素需要和用户交互来确定,比如选择哪个site,选择哪个 ...

  2. SharePoint自动化系列——创建MMS terms

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ PowerShell脚本实现MMS group.termSet.terms的自动化创建: Add- ...

  3. SharePoint自动化系列——Add/Remove “Hold” from items

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题1: 1.如果SharePoint item被添加了hold,通过UI界面来对SharePoi ...

  4. SharePoint自动化系列——通过PowerShell在SharePoint中批量做数据

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ PowerShell是基于.NET的一门脚本语言,对于SharePoint一些日常操作支持的很好. ...

  5. SharePoint自动化系列——通过Coded UI录制脚本自动化创建SharePoint Designer Reusable Workflow

    Coded UI非常好,我开始还在想,怎么样能让一个通过SharePoint Designer创建的Workflow publish三百五十次?想不到一个好的方法,也不知道SharePoint Des ...

  6. SharePoint自动化系列——Add content type to list.

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...

  7. SharePoint自动化系列——Add/Remove "Record" from items

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 目的:批量的将SharePoint items变成records或者将records变成普通的it ...

  8. SharePoint自动化系列——Content Type相关timer jobs一键执行

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...

  9. SharePoint自动化系列——Create a local user and add to SharePoint

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 实现过程:在本地创建一个local user并将该user添加到Administrators组中, ...

随机推荐

  1. python之函数用法setattr(),了解即可

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setattr(),了解即可 #http://www.cnblogs.com/hong ...

  2. 26、线性表(List)

    1.List List接口是Collection的子接口,List是一个可重复集合 2.ArrayList和LinkedList ArrayList和LinkedList是List接口最常见的两个实现 ...

  3. 利用kaptcha生成验证码的详细教程

    kaptcha是一个简单好用的验证码生成工具,有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.Ka ...

  4. [转载]如何使用eclipse 生成runnable jar包

    步骤如下: 1.先找到你的工程中提供接口的类(要包含main方法). 2.在该类中右键选择 Run as. 3.选择 Run configurations. 4.在main窗口中选择main clas ...

  5. lr如何获取当前系统时间戳

    lr如何获取当前系统时间戳 一般使用time函数,获取当前unix时间戳 lr程序如下: int t1; char a[20]; t1=time();//获取当前系统时间 //根据不同情况,将时间存储 ...

  6. apachebench的简单使用

    apachebench的简单使用 2013-03-08 15:48:47 分类: LINUX ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchm ...

  7. TouchID 指纹解锁

    概述 TouchID 指纹解锁 详细 代码下载:http://www.demodashi.com/demo/10701.html 一.软硬件支持 指纹验证功能的最低硬件支持为iPhone5s, iPa ...

  8. 安装到LG手机出错

    [2013-07-10 07:44:31 - txrjsms] ERROR: Application requires API version 11. Device API version is 8 ...

  9. @NotEmpty、@NotNull 和 @NotBlank 的区别

    1. 三者主要区别如下: @NotEmpty :用于集合类,不能为null,且size>0 @NotNull:不能为null,但可以为empty,没有size的约束 @NotBlank:只用于S ...

  10. '<>' operator is not allowed for source level below 1.7

    报错:'<>' operator is not allowed for source level below 1.7 这是eclipse的编译环境与项目的要求不对应造成的,这个错误一般是导 ...