【问题标题】:Sharepoint online - Powershell - Rename\Move fileSharepoint 在线 - Powershell - 重命名\移动文件
【发布时间】:2014-05-12 04:16:57
【问题描述】:

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

环境:Sharepoint 在线 Powershell:Powershell 3.0

我正在使用以下代码来获取项目列表

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

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

你能建议吗?

谢谢 多伦

【问题讨论】:

    标签: powershell sharepoint file-rename


    【解决方案1】:

    File.MoveTo method 将文件移动到指定的目标 URL。

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

    示例

    $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】:

      有一个方法 File.MoveTo: http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.file.moveto%28v=office.15%29.aspx

      这里也讨论过: https://sharepoint.stackexchange.com/a/97057

      使用与在 GetItems 代码中相同的方法将其重写为 powershell。

      【讨论】:

      • 谢谢。按照您提供的链接中的示例,Context.Load 似乎不接受除了 GetItems 的结果之外的任何内容。它说“表达式或语句中出现意外的标记 'icol'”。
      猜你喜欢
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 2013-05-26
      • 2015-06-03
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      相关资源
      最近更新 更多