如何为文档库里面已经存在的文件修改后缀名

来源:互联网 发布:大数据在银行的应用 编辑:程序博客网 时间:2024/05/29 17:00

如何为文档库里面已经存在的文件修改后缀名

这个博客是由SharePoint开发人员支持组的工程师Aaron Miao贡献的。原文地址 http://blogs.technet.com/b/sharepointdevelopersupport/archive/2013/10/08/how-to-change-file-extension-of-an-existing-item-in-sharepoint-document-library.aspx 

这个博客是由SPFamer翻译的。

人们可能由于各种原因,想要更改文档库里面已经存在的文件的后缀名。比如,在SharePoint 2007里,打开文档库里JSON后缀名的文件没有任何问题,但是一旦你升级到SharePoint 2010,你就不能够再打开了。你会的到类似于下面的消息:

An error occurred during the processingof /Shared Documents/test.json.

The page must have a <%@ webserviceclass=”MyNamespace.MyClass” … %> directive.

要想从SharePoint页面打开JSON文件,一个选择是把文件后缀名改为TXT。但是没有办法修改文件的名字。这个可以通过下面的两个方法数显,可以用PowerShell,也可以用SharePoint server OM.

Use SPFile.MoveTo

$site = Get-SPSite"http://yoursite"$web  = $site.RootWeb$list =$web.Lists["SharedDocuments"]$item =$list.GetItemById(0)$file = $item.File$file.MoveTo($item.ParentList.RootFolder.Url+ "/” +”test.txt")$file.Update()


Use SPFile.CopyTo

$site = Get-SPSite"http://yoursite"$web  = $site.RootWeb$list =$web.Lists["SharedDocuments"] $caml = '  <Where>    <Eq>      <FieldRefName="File_x0020_Type" />      <ValueType="Text">json</Value>    </Eq>  </Where>' $query = new-object Microsoft.SharePoint.SPQuery$query.Query = $caml $items =$list.GetItems($query) foreach($item in $items){      $file = $item.File      $url = $file.ServerRelativeUrl      $newurl = $url.replace(".json", ".txt")      $file.CopyTo($newurl)}


希望这个对您有帮助。

 

0 0
原创粉丝点击