SharePoint自动化系列——Upload files to SharePoint library using PowerShell.
转载请注明出自天外归云的博客园: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.的更多相关文章
- SharePoint自动化系列——Site/Web/List级别的导航菜单
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 需求:在不同的测试用例中,对脚本中不确定的因素需要和用户交互来确定,比如选择哪个site,选择哪个 ...
- SharePoint自动化系列——创建MMS terms
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ PowerShell脚本实现MMS group.termSet.terms的自动化创建: Add- ...
- SharePoint自动化系列——Add/Remove “Hold” from items
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题1: 1.如果SharePoint item被添加了hold,通过UI界面来对SharePoi ...
- SharePoint自动化系列——通过PowerShell在SharePoint中批量做数据
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ PowerShell是基于.NET的一门脚本语言,对于SharePoint一些日常操作支持的很好. ...
- SharePoint自动化系列——通过Coded UI录制脚本自动化创建SharePoint Designer Reusable Workflow
Coded UI非常好,我开始还在想,怎么样能让一个通过SharePoint Designer创建的Workflow publish三百五十次?想不到一个好的方法,也不知道SharePoint Des ...
- SharePoint自动化系列——Add content type to list.
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...
- SharePoint自动化系列——Add/Remove "Record" from items
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 目的:批量的将SharePoint items变成records或者将records变成普通的it ...
- SharePoint自动化系列——Content Type相关timer jobs一键执行
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...
- SharePoint自动化系列——Create a local user and add to SharePoint
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 实现过程:在本地创建一个local user并将该user添加到Administrators组中, ...
随机推荐
- SettingsEditplus
迁移时间:2017年5月20日10:51:51Author:Marydon官网下载 http://www.editplus.com/设置一 1.修改字体大小及背景色 首选项-->Genera ...
- poi读取execl的日期
当execl中的列为日期格式时,后台读取到是一个数字,通过如下代码可以直接读取并转换到Date类型 HSSFDateUtil.getJavaDate(cell.getNumericCellValue( ...
- import 导包三种方法
# -*- coding: utf-8 -*- #python 27 #xiaodeng #导包三种方法 #(常用)完整的导入,也是最基本的方法 import re #自己定义别名,一般情况下尽量少用 ...
- Android倒计时CountDownTimer小记
Android 超简便的倒计时实现: CountDownTimer CountDownTimer由系统提供 查资料的时候 发现了CountDownTimer这个类之后 果断抛弃了曾经的倒计时做法 功 ...
- SVN访问版本库精细的权限控制
SVN精细权限控制本章将详细介绍前一章所涉及的两个配置文件, svnserve.conf 和 authz.conf,通过对配置逐行的描述,来阐明其中的一些细节含义.除此之外的其他配置.安装等内容,不是 ...
- IDEA创建maven项目jar更新缓慢问题
转自:http://www.cnblogs.com/ae6623/p/4416256.html 国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. 最新 ...
- Python 死循环和嵌套循环
何为死循环:在编程中,一个无法靠自身的控制终止的循环被称为死循环. 死循环的使用:死循环并非一无是处,C语言中死循环while true或 while 1 是单片机编程的普遍用法,死循环一直运行等待中 ...
- 教你动手做一个 iOS 越狱 app
前言 俗话说得好, 万事开头难. 仅仅是上图一个如此简单地不能再简单的小app, 其实都不算是app, 只是注入了一段代码进系统中, 等到特定的函数方法调用的时候就会被我们hook掉, 执行我们写的代 ...
- map reduce相关程序
Test_1.java /** * Hadoop网络课程模板程序 * 编写者:James */ import java.io.IOException; import java.text.DateFor ...
- 主流ETL(Extract-Transform-Load)工具选型,Kettle Spoon、Datastage、Powercenter介绍
参考:三大主流ETL工具选型 ETL工具 Kettle Spoon 开源ETL工具,所以免费,用java开发的. Ascential公司的Datastage(在2005年被IBM收购现在是 IBM 的 ...