转载请注明出自天外归云的博客园: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】学习笔记六:循环

    循环是一个结构,导致一个程序要重复一定的次数 条件循环也一样,当条件变为假,循环结束 For循环 在python for循环遍历序列,如一个列表或一个字符. for循环语法:   ——for iter ...

  2. Python之reduce

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之reduce #http://python.jobbole.com/82597/ #1)red ...

  3. 类的专有方法(__len__)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.imooc.com/code/6252 #类的专有方法(__len__) #如果一个类 ...

  4. TP3.2中filed和find()使用

    1.总结:filed和find(),进行一维数组查询指定字段时,可以进行配合使用,获得结果:key:value; 但官方没有明确指出. 2.filed和getFiled最终的结果是不一样的,一个获得的 ...

  5. LeetCode-342:Power of Four

    This  is another  "Pick One" Problem :[Problem:342-Power of Four] Given an integer (signed ...

  6. App上架重磅通知:App Store安全新规17年1月生效

    作者:沙铭 来源:公众号 沙铭世界观 ID:mobview 做推广的也许并不了解什么是ATS(App Transport Security),不过这却是一个定时炸弹,引爆点在2016年底,后果就是你不 ...

  7. Loading...加载图收集

    收集来源:http://cs.fangjia.com/zoushi/

  8. ajax操作登录

    js文件中的内容(ajax.operate.js) ;(function ($, window) { var _ajaxOperate = window.ajaxOperate || {}; _aja ...

  9. 修改jQuery.validate验证方法和提示信息

    1.添加验证方法 在jquery.validate.js文件中直接添加验证方法,例如: jQuery.validator.addMethod("Specialstring", fu ...

  10. 《JAVA与模式》之适配器模式(转载)

    适配器模式比较简单,偷个懒,直接转载一篇. 个人理解: * 类适配器是通过继承来完成适配 * 对象适配器是通过传递对象来完成适配 * 不管哪种,其实都是通过引用特殊接口的对象来完成特殊接口的适配调用 ...