转载请注明出自天外归云的博客园: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. HDU 3316 My Brute(二维费用流)经典

    My Brute Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. iptables阐述防火墙

    一:前言   防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防 ...

  3. 在Eclipse中开发C/C++项目

    摘要:通过本文你将获得如何在Eclipse平台上开发C/C++项目的总体认识.虽然Eclipse主要被用来开发Java项目,但它的框架使得它很容易实现对其他开发语言的支持.在这篇文章里,你将学会如何使 ...

  4. Android API之android.provider.ContactsContract.Contacts

    android.provider.ContactsContract.Contacts 对应contacts数据表.RawContacts的一个聚合(aggregate)代表同一个人.每个人在数据表co ...

  5. 【TP3.2】跨库操作和跨域操作

    一.跨库操作:(同一服务器,不同的数据库) 假设UserModel对应的数据表在数据库user下面,而InfoModel对应的数据表在数据库info下面,那么我们只需要进行下面的设置即可. class ...

  6. 将Memcached作为服务自动启动

    1.最简单的做法 通常:启动Memcache的服务器端的命令为: /usr/local/bin/memcached -d -m 256 -u root -l 127.0.0.1 -p 12000 -c ...

  7. deque容器的运用一点一点积累

    #include<iostream> #include<deque> #include<cstdio> #include<cstring> #inclu ...

  8. eclipse生成可执行jar包[转]

    相信大家在开发java的时候一定会遇到要求将java工程打包成可运行的jar的需求,今天我在这篇博客中详细讲解一下生成可运行jar的两种方法,亲测完全可行. 1. 工程中不包含第三方的jar包 这种情 ...

  9. MQTT Stresser

    go环境请参考https://www.cnblogs.com/saryli/p/9833253.html Load testing tool to stress MQTT message broker ...

  10. JMeter分布式配置

    搭建进行分布式平台测试前提: 1 所有的防火墙应该关闭 2 所有的客户端应该都是在同一个子网中. 3 确保jMeter可以访问这个服务器 4 确保各个客户端的jMeter的版本都是一致的,不同版本的J ...