---
title: "Word文書のフィールドにアクセスする"
slug: "access-word-document-fields"
abstract: "Sitecoreを使ってWordの文書フィールドにアクセスする方法。Data.Fields.WordDocumentFieldクラス。"
---
# Word文書のフィールドにアクセスする

Sitecore.Data.Fields.WordDocumentFieldクラスを使ってWordのドキュメントフィールドの値にアクセスできます。Sitecore.Data.Fields.WordDocumentField.Htmlプロパティを使ってフィールド値のHTML表現にアクセスできます。Sitecore.Data.Fields.WordDocumentField.PlainTextプロパティを使ってフィールド値のプレーンテキスト表現にアクセスできます。

Sitecore.Data.Fields.WordDocumentField.Stylesプロパティを使って、そのHTMLに関連付けられたカスケードスタイルシートコードにアクセスできます。例えば、以下の例に基づくWebコントロールを実装し、WordドキュメントフィールドのスタイルとHTMLを出力できます。

namespace Sitecore.Sharedsource.Web.UI.WebControls
{

 using System;
 using System.Text.RegularExpressions;

 public class RenderWordDocumentFieldRenderer : Sitecore.Web.UI.WebControl
 {

 public string FieldName { get; set; }

 protected override void DoRender(System.Web.UI.HtmlTextWriter output)
 {
 Sitecore.Data.Items.Item item = this.GetItem();

 if (this.FieldName == null || item == null)
 {
 return;
 }

 Sitecore.Data.Fields.WordDocumentField wordField = item.Fields"WordField";

 if (wordField == null || wordField.BlobId == Sitecore.Data.ID.Null || Regex.IsMatch(wordField.PlainText, "^\\\\s\*$"))
 {
 return;
 }

 output.WriteLine(String.Format("<style type=\\"text/css\\">{0}</style>", wordField.Styles));

 output.WriteLine(Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, wordField.InnerField.Name));
 }
 }
}

Sitecore.WordOCX.WordOCXUrlManager.GetDownloadLinkメソッドを使って、Wordドキュメントフィールドの値にアクセスするURLを取得することができます。これはWordドキュメント(.docxファイル)です。

例えば、コンテキスト項目内のWordドキュメントフィールドに*WordDocument*名が付いているWordドキュメントファイルにアクセスするURLを取得する場合:

Sitecore.Data.Items.Item item = Sitecore.Context.Item;

Sitecore.Data.Fields.WordDocumentField wordField = item.Fields"WordDocument";

if (wordField != null && wordField.BlobId != Sitecore.Data.ID.Null && !Regex.IsMatch(wordField.PlainText, "^\\\\s\*$"))
{
 Dictionary<string, string> parameters = new Dictionary<string, string>();

 parameters"db" = item.Database.Name;

 parameters"blobId" = wordField.BlobId.ToString();

 Sitecore.WordOCX.WordOCXUrlManager wordManager =

 new Sitecore.WordOCX.WordOCXUrlManager(parameters);

 string url = wordManager.GetDownloadLink();
}