您的位置:首页 > 其它

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

2014-12-24 23:36 281 查看
如何为文档库里面已经存在的文件修改后缀名
这个博客是由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)
}


希望这个对您有帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: