Sharepoint online - Powershell - Rename\Move file

谁都会走 提交于 2019-12-25 03:00:55

问题


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.

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.

Can you please advise?

Thanks Doron


回答1:


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

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

Example

$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()
  }
}



回答2:


There is a method File.MoveTo: http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.file.moveto%28v=office.15%29.aspx

It's been discussed also here: https://sharepoint.stackexchange.com/a/97057

Rewrite it to powershell by the same approach as you did in your GetItems code.



来源:https://stackoverflow.com/questions/23589753/sharepoint-online-powershell-rename-move-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!