本文介绍了读取在Sharepoint中创建的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经做了一些严肃的谷歌搜索,以了解如何阅读word文档的属性,最后我发现了这篇文章: http://social.msdn.microsoft.com/forums/en-US/vsto/thread/11f1fd8b-0ee0-4f3b- 8a7e-f5dc92cf48d5 / 我将这段代码修改为属性读者: public static class Word2007CustomPropertyReader { public static string ReadProperty( CustomX MLParts &NBSP; customXmlParts,&NBSP;的串&NBSP;字段)&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; { CustomXMLPart xmlPart = null ; CustomXMLNode xmlNode =  ; null ; foreach (的 CustomXMLPart &NBSP;部分&NBSP;的中&NBSP; customXmlParts)&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;的如果&NBSP;(part.DocumentElement.BaseName&NBSP; ==&NBSP; _wssPropertiesRootElementName)&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; xmlPart&NBSP; =&NBSP;一部分; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的断裂; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;(!xmlPart&NBSP; =&NBSP;的空)&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&的xmlNode NBSP; =&NBSP; xmlPart.SelectSingleNode(_documentManagementXpath); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的!如果&NBSP;(的xmlNode&NBSP; =&NBSP;的空)&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的的foreach &NBSP;(的 CustomXMLNode >&NBSP; xmlNode.ChildNodes)&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP /跨度>&NBSP;节点&NBSP的&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; string key = node.BaseName; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的如果&NBSP;(密钥NBSP; ==&NBSP;字段)&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; return node.Text; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} &NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的返回 string .Empty; } 私人 静态 readonly string _wssPropertiesRootElementName = " properties" ; private static &NBSP;的只读&NBSP;的串&NBSP; _documentManagementXpath&NBSP; =&NBSP; ; } 要从我的winform中读取一个属性,我必须运行一个Microsoft.Office.Interop.Word.Application的一个实例,并创建一个Microsoft.Office.Interop.Word.Document实例并检索CustomXMLParts。 对我而言,这感觉有点尴尬。是否有其他方法可以访问CustomXMLParts?或者其他一些方法来读取Sharepoint创建的属性?解决方案 Hello EckePecke, 这里有一些资源可以查询CustomXMLParts并查看OpenXML文件的各个部分。 在下面的文章"文档信息面板和文档属性"中。在$ http://msdn.microsoft.com/en-us/library /bb447589.aspx 查看此词语。 自定义文档属性 -------- -------------------------------------------------- ---------------------- 用户定义的属性包含在Open XML格式的自定义文件属性部分中。对于存储在SharePoint Foundation库中的文档,此部分包含一个自定义属性,该属性指定分配给文档的内容类型的内容类型ID,如以下示例所示。 复制 <?xml version =" 1.0"编码= QUOT; UTF-8英寸standalone ="是"?> <属性 xmlns =" http://schemas.openxmlformats.org/officeDocument/2006/custom-properties " ; 的xmlns:VT =" http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes "> ; < property fmtid =" {D5CDD505-2E9C-101B-9397-08002B2CF9AE}" PID =" 2英寸name =" ContentTypeId"> < vt:lpwstr> 0x01010042D2ECEB487FD14A878A8B12B45AD0DF< / vt:lpwstr> < / property> < / Properties> 内容类型ID属性永远不会从文档提升到它所在的文档库。 注意 在以二进制格式(如.doc)保存的Office 2010文档中,所有自定义属性(包括映射到SharePoint Foundation列的属性)都存储在同一位置。您无法将SharePoint Foundation列绑定到保存为二进制格式的 Office 2010文档中的文档属性。如果在SharePoint Foundation中的Open XML格式中保存二进制文件,SharePoint Foundation会尝试合理化文档中存在的属性。如果文档包含一个自定义属性,该属性的与要保存它的SharePoint Foundation文档库中的列具有相同的名称和数据类型,则SharePoint Foundation会假定这两个属性相同并重定位文档属性因此,在Open XML格式中。 但是,文档属性的合理化不会从一开始就以Open XML格式保存的文件执行。 讨论结束 请参阅博客"Microsoft Office SharePoint及相关"在¥b $ b http://blogs.msdn.com/b/joerg_sinemus/archive/2007/05/25/migrate-office-2003 -documents-to-office-2007-documents-and-what-are-the-old-doc-properties.aspx for the discussion"What I Can Can难道"?; WSS属性作为自定义XML部件存储在Office 2007(& 2010)OpenXML文件中。 另请参阅"使用带有Open的SharePoint Foundation 2010托管客户端对象模型" XML&NBSP; SDK 2.0,价格为 http://msdn.microft。 com / zh-CN / library / ee956524(office.14).aspx $ 另请参阅"从不同部分检索内容:明确或Open XML SDK 2.0 for Microsoft Office中的隐式关系" b at: http://msdn.microsoft.com/en-us/library/ee413542(office.12).aspx I've done some serious googling to find out how to read properties of a word document and finally I found this post: http://social.msdn.microsoft.com/forums/en-US/vsto/thread/11f1fd8b-0ee0-4f3b-8a7e-f5dc92cf48d5/I modified that piece of code to a property-reader:public static class Word2007CustomPropertyReader { public static string ReadProperty(CustomXMLParts customXmlParts, string field) { CustomXMLPart xmlPart = null; CustomXMLNode xmlNode = null; foreach (CustomXMLPart part in customXmlParts) { if (part.DocumentElement.BaseName == _wssPropertiesRootElementName) { xmlPart = part; break; } } if (xmlPart != null) { xmlNode = xmlPart.SelectSingleNode(_documentManagementXpath); } if (xmlNode != null) { foreach (CustomXMLNode node in xmlNode.ChildNodes) { string key = node.BaseName; if (key == field) { return node.Text; } } } return string.Empty; } private static readonly string _wssPropertiesRootElementName = "properties"; private static readonly string _documentManagementXpath = "//documentManagement"; }To read a property from my winform I have to run an instance of Microsoft.Office.Interop.Word.Application and create an instance of Microsoft.Office.Interop.Word.Document and retrieve CustomXMLParts.To me this feels kind of awkward. Is there some other way to get to CustomXMLParts? Or some other way to read the Sharepoint created properties? 解决方案 Hello EckePecke,Here are some resources to consult about both CustomXMLParts and seeing the individual parts of an OpenXML file.In the following article "Document Information Panel and Document Properties" athttp://msdn.microsoft.com/en-us/library/bb447589.aspxSee this discusion.Custom Document Properties--------------------------------------------------------------------------------User-defined properties are contained in the Custom File Properties part of the Open XML Formats. For documents stored in a SharePoint Foundation library, this part contains a custom property that specifies the content type ID of the content type assigned to the document, as in the following example.Copy <?xml version="1.0" encoding="UTF-8" standalone="yes"?><Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"> <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ContentTypeId"> <vt:lpwstr>0x01010042D2ECEB487FD14A878A8B12B45AD0DF</vt:lpwstr> </property></Properties>The content type ID property is never promoted from the document to the document library in which it resides. Note In Office 2010 documents saved in a binary format, such as .doc, all custom properties, including those that would map to SharePoint Foundation columns, are stored in the same location. You cannot bind SharePoint Foundation columns to document properties in Office 2010 documents saved as binary formats. If you save a binary file in Open XML Formats in SharePoint Foundation, SharePoint Foundation attempts to rationalize the properties present in the document. If the document contains a custom property that has the same name and data type as a column in the SharePoint Foundation document library to which it is being saved, then SharePoint Foundation assumes the two properties are the same and relocates the document property within the Open XML Formats accordingly. However, this rationalization of document properties is not performed on files saved in the Open XML Formats from the start.End of that discussionSee the blog "Microsoft Office SharePoint and related" athttp://blogs.msdn.com/b/joerg_sinemus/archive/2007/05/25/migrate-office-2003-documents-to-office-2007-documents-and-what-is-happen-with-the-old-doc-properties.aspxfor the discussion "What I Can Do?" WSS properties are...stored as custom XML part within an Office 2007 ( & 2010 ) OpenXML file.Also see "Using the SharePoint Foundation 2010 Managed Client Object Model with the Open XML SDK 2.0 athttp://msdn.microft.com/en-us/library/ee956524(office.14).aspx Also see "Retrieving Content from Different Parts: Explicit or Implicit Relationships in the Open XML SDK 2.0 for Microsoft Office"at:http://msdn.microsoft.com/en-us/library/ee413542(office.12).aspx 这篇关于读取在Sharepoint中创建的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 09:35