這正是我們在我們的項目中所做的:)幸運的你。
首先您需要爲該文檔庫創建事件接收器。你需要實現ItemUpdated和ItemAdded。見 http://www.dotnetcurry.com/ShowArticle.aspx?ID=649 http://blogs.msdn.com/b/brianwilson/archive/2007/03/05/part-1-event-handlers-everything-you-need-to-know-about-microsoft-office-與SharePoint門戶服務器苔事件handlers.aspx
//事件接收器代碼。這會給你的內容控制的名稱和它的值
Dictionary<string, string> results = new Dictionary<string, string>();
using (Stream stream = file.OpenBinaryStream(SPOpenBinaryOptions.SkipVirusScan)) {
using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true)) {
var contentControls = doc.MainDocumentPart
.GetXDocument()
.Descendants(w + "sdt");
foreach (var contentControl in contentControls)
{
string key = (string)contentControl.Descendants(w + "sdtPr").Elements(w + "alias").Attributes(w + "val").FirstOrDefault();
string val = GetTextFromContentControl(contentControl);
results[key] = val;
}
}
static string GetTextFromContentControl(XElement contentControlNode) {
return contentControlNode.Descendants(w + "p")
.Select
(
p => p.Elements()
.Where(z => z.Name == r || z.Name == ins || z.Name == br)
.Descendants()
.Where(z => z.Name == w + "t" || z.Name == w + "br")
.StringConcatenate(element => (string)element + (element.Name == w + "br" ? Environment.NewLine : "")) + Environment.NewLine
).StringConcatenate();
}