在线Sharepoint-Powershell-重命名\移动文件 [英] Sharepoint online - Powershell - Rename\Move file

查看:98
本文介绍了在线Sharepoint-Powershell-重命名\移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将在线共享点用作Office 365的一部分(我的脚本未从共享点服务器本身运行).完成处理后,我想在共享点中重命名\ Move文件.我找到了一种将文件下载到PC的方法,但是我似乎找不到找到重命名文件或将其移动到站点中其他目录的方法.

We are using online share point as part of our office 365 (my scripts are not running from the sharepoint server itself). I want to rename\Move files in the share point after i finish up processing them. I found a way to download the file to my pc but i cant seem to find the way to rename the file or move it to a different directory in the site.

环境:在线共享点 Powershell:Powershell 3.0

Environment : Sharepoint online Powershell : Powershell 3.0

我正在使用以下代码获取商品列表

i am using the following code to get the list of items

`$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
 $ctx.Credentials = $credentials
 $list = $ctx.Web.Lists.GetByTitle('Documents')
 $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
 $camlQuery.ViewXml = '<View Scope="RecursiveAll">
                        <Query>
                            <Where>
                                <And>
                                    <Contains>
                                        <FieldRef Name="FileDirRef" />
                                        <Value Type="Text">' + $path+ '</Value>
                                    </Contains>
                                    <Eq>
                                        <FieldRef Name="File_x0020_Type" />
                                        <Value Type="Text">XLS</Value>
                                    </Eq>
                                </And>
                            </Where>
                        </Query>
                    </View>'


Write-Host $camlQuery.ViewXml

#$camlQuery.FolderServerRelativeUrl="/Doron";
$items = $list.GetItems($camlQuery)
$ctx.Load($items)
$ctx.ExecuteQuery()`

但找不到重命名文件或将其移动到其他目录的方法.

but cant find the way to rename the file or move it to a different directory.

可以请教吗?

谢谢 多伦(Doron)

Thanks Doron

推荐答案

File.MoveTo method moves the file to the specified destination URL.

以下示例演示了如何使用PowerShell中的CSOM将文件从库中的一个文件夹移动到另一个文件夹.

The following example demonstrates how to move files from one folder to another within a library using CSOM in PowerShell.

$listTitle = "Documents"
$sourceFolder = "/Shared Documents/Archive"
$destFolder = "/Shared Documents/Archive/06"


$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
$ctx.credentials = $credentials

#Load items
$list = $ctx.Web.Lists.GetByTitle($listTitle)
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$query.FolderServerRelativeUrl=$sourceFolder;
$items = $list.GetItems($query)
$ctx.Load($items)
$ctx.ExecuteQuery()


#Move file(s)
foreach ($item in $items){

  if($item.FileSystemObjectType -eq [Microsoft.SharePoint.Client.FileSystemObjectType ]::File) { 

     $destFileUrl = $item["FileRef"].ToString().Replace($sourceFolder,$destFolder)
     $item.File.MoveTo($destFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
     $ctx.ExecuteQuery()
  }
}

这篇关于在线Sharepoint-Powershell-重命名\移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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