在Word VSTO插件中访问文档库属性:脱离SharePoint的检索
我正在开发一款从VB.NET迁移到C#的Word VSTO插件,核心需求是在Word环境内直接访问文档库属性,无需依赖SharePoint服务,哪怕文件已经不在SharePoint存储中也能获取——这些属性在Word的「文件→信息」面板中是可见的。
尝试过的无效/受限方案
在找到可行方法前,我试过以下几种常规途径,但都遇到了问题:
BuiltInDocumentProperties:完全无法获取目标文档库属性CustomDocumentProperties:同样无法定位到目标属性ContentTypeProperties:仅能正常读取Description、DocId和DocUrlId三个属性,调用其余属性时直接报错
成功解决方案
最终通过Word对象模型中的CustomXMLParts找到了突破口:定位到NamespaceUri为http://schemas.microsoft.com/office/2006/metadata/properties的自定义XML部件,就能从中提取到所有需要的文档库属性。
C#代码示例
using Microsoft.Office.Core; using Microsoft.Office.Interop.Word; public static string GetDocumentLibraryProperty(Document doc, string propertyName) { foreach (CustomXMLPart part in doc.CustomXMLParts) { if (part.NamespaceURI == "http://schemas.microsoft.com/office/2006/metadata/properties") { try { var node = part.SelectSingleNode($"//*[local-name()='{propertyName}']"); return node?.Text ?? string.Empty; } catch { return string.Empty; } } } return string.Empty; }
内容的提问来源于stack exchange,提问作者Eliah Thommen
相关产品推荐
相关产品推荐

