CSOM を使用して SharePoint のコンテンツ タイプを作成する

Core.SPD サンプルを使用すると、プログラムによってサイト列とコンテンツ タイプを作成し、それらを相互にリンク付けることができます。

また、SharePoint CSOM API (SharePoint Server 2013 クライアント コンポーネント SDK で使用できます) を使用すると、特定のコンテンツ タイプ ID を追加したり、コンテンツ タイプ、リスト、サイト タイトルをローカライズしたりすることができます。

はじめに

まず、Core.SPD サンプルを、GitHub の Office 365 Developer のパターンおよびプラクティス プロジェクトからダウンロードします。

注:

この記事で提供されるコードは、明示または黙示のいかなる種類の保証なしに現状のまま提供されるものであり、特定目的への適合性、商品性、権利侵害の不存在についての暗黙的な保証は一切ありません。

コンテンツ タイプとサイト列の作成

次のコード例は、ContentTypeCreationInformation クラスを使用してコンテンツ タイプを作成し、ID を設定する方法について示しています。

ContentTypeCollection contentTypes = web.ContentTypes;
cc.Load(contentTypes);
cc.ExecuteQuery();

foreach (var item in contentTypes)
{
  if (item.StringId == "0x0101009189AB5D3D2647B580F011DA2F356FB2")
    return;
}

// Create a Content Type Information object.
ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();
// Set the name for the content type.
newCt.Name = "Contoso Document";
// Inherit from oob document - 0x0101 and assign. 
newCt.Id = "0x0101009189AB5D3D2647B580F011DA2F356FB2";
// Set content type to be available from specific group.
newCt.Group = "Contoso Content Types";
// Create the content type.
ContentType myContentType = contentTypes.Add(newCt);
cc.ExecuteQuery();

Using AddFieldAsXml you can add fields to the FieldCollection of a site collection:
FieldCollection fields = web.Fields;
cc.Load(fields);
cc.ExecuteQuery();

string FieldAsXML = @"<Field ID='{4F34B2ED-9CFF-4900-B091-4C0033F89944}' Name='ContosoString' DisplayName='Contoso String' Type='Text' Hidden='False' Group='Contoso Site Columns' Description='Contoso Text Field' />";
Field fld = fields.AddFieldAsXml(FieldAsXML, true, AddFieldOptions.DefaultValue);
cc.Load(fields);
cc.Load(fld);
cc.ExecuteQuery();


FieldLinkCollection クラスと FieldLinkCreationInformation クラスを使用して、フィールドをコンテンツ タイプにリンクさせます。

FieldCollection fields = web.Fields;
Field fld = fields.GetByInternalNameOrTitle("ContosoString");
cc.Load(fields);
cc.Load(fld);
cc.ExecuteQuery();

FieldLinkCollection refFields = myContentType.FieldLinks;
cc.Load(refFields);
cc.ExecuteQuery();

foreach (var item in refFields)
{
  if (item.Name == "ContosoString")
    return;
  }

FieldLinkCreationInformation link = new FieldLinkCreationInformation();
link.Field = fld;
myContentType.FieldLinks.Add(link);
myContentType.Update(true);
cc.ExecuteQuery();

コンテンツ タイプ、リスト、サイト タイトルのローカライズ

次のコードを使用して、サイト タイトルとサイト説明をローカライズします。

web.TitleResource.SetValueForUICulture("fi-FI", "Kielikäännä minut");
web.DescriptionResource.SetValueForUICulture("fi-FI", "Kielikäännetty saitti");


リストの場合、サイトと同じ方法を使用します。

list.TitleResource.SetValueForUICulture("fi-FI", "Kielikäännä minut");
list.DescriptionResource.SetValueForUICulture("fi-FI", "Tämä esimerkki näyttää miten voit kielikääntää listoja.");


コンテンツ タイプの場合は、名前と説明をローカライズするためのオプションがあります。 フィールドの場合は、タイトルと説明の値をローカライズできます。

myContentType.NameResource.SetValueForUICulture("fi-FI", "Contoso Dokumentti");
myContentType.DescriptionResource.SetValueForUICulture("fi-FI", "Tämä on geneerinen Contoso dokumentti.");

fld.TitleResource.SetValueForUICulture("fi-FI", "Contoso Teksti");
fld.DescriptionResource.SetValueForUICulture("fi-FI", "Tää on niiku Contoso metadatalle.");

ドキュメント コンテンツ タイプとサイト列の作成

次の例は、ドキュメント コンテンツ タイプを作成し、ドキュメント テンプレートをコンテンツ タイプに関連付ける方法を示しています。

この例では、Contoso Document という新しいコンテンツ タイプをサイト コレクションに追加します。 このコンテンツ タイプには、新しいドキュメントが作成されたときに、カスタム テンプレートが関連付けられます。

ContentType ct = web.ContentTypes.GetById("0x0101009189AB5D3D2647B580F011DA2F356FB2");
            cc.Load(ct); cc.ExecuteQuery();
            string ctFolderServerRelativeURL = "_cts/" + ct.Name;
            Folder ctFolder = web.GetFolderByServerRelativeUrl(ctFolderServerRelativeURL);
            cc.Load(ctFolder);
            cc.ExecuteQuery();

            string path = @"C:\Data\Test Documents\Doc CT File.docx";
            string fileName = System.IO.Path.GetFileName(path);
            byte[] filecontent = System.IO.File.ReadAllBytes(path);

            using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
            {
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = filecontent;
                newFile.Url = ctFolderServerRelativeURL + "/" + fileName;

                Microsoft.SharePoint.Client.File uploadedFile = ctFolder.Files.Add(newFile);
                cc.Load(uploadedFile);
                cc.ExecuteQuery();
                //Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, ctFolderServerRelativeURL + "/" + fileName, fs, true);

                
                cc.Load(ct); cc.ExecuteQuery();
                ct.DocumentTemplate = fileName;
                ct.Update(false);
                cc.Load(ct); cc.ExecuteQuery();
                Console.WriteLine("Content type updates");
            }

関連項目