SharePoint SPFile 可以对现有项目进行更改扩展吗?

发布于 2024-10-16 03:24:24 字数 1060 浏览 3 评论 0原文

我对此一筹莫展。我有一个用户界面,用于创建和编辑存储在 SharePoint 文档库中的文档。技巧部分是我需要允许用户更新文档,没问题只需使用 SPFile.SaveBinary() ,对吗?

这肯定会更新文件的内容,但不知何故旧文件名和旧扩展名仍然存在,这是一个问题。删除并重新添加列表项也不是解决方案,因为 url 中引用了该项目的 id。

我的问题是如何更新 SPFile 项目的扩展名和文件名元数据?

到目前为止,我使用对象库的所有尝试都失败了,我尝试更新下面的字段,但没有成功。看来必须有一种更简单的方法来做到这一点。

SPFile file = item.File;
file.Item[SPBuiltInFieldId.FileLeafRef] = resolvedFileName;
file.Item[SPBuiltInFieldId.FileRef] = "/File/" + resolvedFileName;
file.Item[SPBuiltInFieldId.BaseName] = System.IO.Path.GetFileNameWithoutExtension(resolvedFileName);
file.Item["Name"] = System.IO.Path.GetFileNameWithoutExtension(resolvedFileName);
file.SaveBinary(conduitFile);
file.Update();

[编辑] - 这是我的工作解决方案。

SPFile file = item.File;
string resolvedFileName = item.ID.ToString() + "-" + conduitFileName;
item["Title"] = resolvedFileName;
file.SaveBinary(conduitFile);
file.MoveTo(item.ParentList.RootFolder.Url + "/" + resolvedFileName, true);
file.Item["Name"] = resolvedFileName;
file.Update();

I am at my wits end on this one. I have a user interface that creates and edits documents stored in a SharePoint document library. The trick part is I need to allow the user to update the document no problem just use SPFile.SaveBinary() right?

This definitely updates the contents of the file but somehow the old file name and the old extension persist, this is a problem. Deleting and re-adding the list item is not a solution either because the id of the item is being referenced in the url.

My question is how can I update the extension and filename metadata of the SPFile item?

So far all of my attempts using the object library have failed, i've tried updating the fields below none have been successful. It seems like there has to be an easier way to do this.

SPFile file = item.File;
file.Item[SPBuiltInFieldId.FileLeafRef] = resolvedFileName;
file.Item[SPBuiltInFieldId.FileRef] = "/File/" + resolvedFileName;
file.Item[SPBuiltInFieldId.BaseName] = System.IO.Path.GetFileNameWithoutExtension(resolvedFileName);
file.Item["Name"] = System.IO.Path.GetFileNameWithoutExtension(resolvedFileName);
file.SaveBinary(conduitFile);
file.Update();

[EDIT] - Here is my working solution.

SPFile file = item.File;
string resolvedFileName = item.ID.ToString() + "-" + conduitFileName;
item["Title"] = resolvedFileName;
file.SaveBinary(conduitFile);
file.MoveTo(item.ParentList.RootFolder.Url + "/" + resolvedFileName, true);
file.Item["Name"] = resolvedFileName;
file.Update();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤ぢ 2024-10-23 03:24:24

将文件保存到库后,使用 MoveTo 方法并在 newUrl 参数中传递修改后的文件名。

SPFile.MoveTo 方法(字符串)
快速&简单:通过事件接收器使用 SharePoint 对象模型重命名上传的文件

After the file is saved to the library, use the MoveTo method and pass the modified file name in the newUrl parameter.

SPFile.MoveTo Method (String)
Quick & Easy: Rename Uploaded File Using SharePoint Object Model Via an Event Receiver

高速公鹿 2024-10-23 03:24:24

另一种比使用 MoveTo 更简单的方法是使用 SPListItem 的 BaseName 属性。您可以通过运行来设置它,

item["BaseName"] = resolvedFileName; //Whatever you want the new file name to be 
item.Update();

这比 MoveTo 更容易,因为您不必担心文件夹层次结构,也不必担心文件扩展名。

由于某种原因,该属性未在 MSDN 文档,但似乎运行良好,没有问题。

Another way that is simpler than using the MoveTo is to use the BaseName property of the SPListItem. You would set this by running

item["BaseName"] = resolvedFileName; //Whatever you want the new file name to be 
item.Update();

This is easier than MoveTo because you don't have to worry about the folder hierarchy and you don't have to worry about the file extension.

For some reason the property is not listed on the MSDN documentation, but it seems to work well without a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文