2012-03-07 11 views
6

Tridion 2011では、UpdateXmlに相当するコアサービスを使用して、新しいTridionオブジェクトを一般的な方法で作成したいと考えています。私は、新しいコンポーネント、ページ、そしてその後のフォルダと構造グループを作成しようと考えています。 UpdateXmlを使用するとうまくいきますが、RepositoryLocalObject(または別の汎用タイプのオブジェクト)をコアサービスのComponentDataオブジェクトにキャストする際に問題が発生しています。私のコアサービスコードはずっと長く(そして2番目に成長しています)。コアサービスを使用してTridion 2011でアイテムを作成

私は、オブジェクトタイプ固有のプロパティにアクセスしようとすると、エラーメッセージ:

エラー9「Tridion.ContentManager.CoreService.Client.RepositoryLocalObjectDataは」「コンテンツ」となし拡張メソッドの定義が含まれていません

'コンテンツ'は、 'Tridion.ContentManager.CoreService.Client.RepositoryLocalObjectData'タイプの最初の引数を受け取ります

可能な解決策は、拡張メソッドを作成することですか?

TridionのTOMのAPI:

Function CreateNewItemCopy(organizationalItemUri, itemType, title, xml, 
          directory, filename) 
    Dim newItem : set newItem = tdse.GetNewObject(itemType, organizationalItemUri) 
    newItem.UpdateXml(xml) 
    newItem.Title = title 

    if(itemType = 64) then ' page 
     newItem.FileName = filename 
    elseif(itemType = 4) then ' sg 
     newItem.Directory = directory 
    end if 

    newItem.save(true) 
    CreateNewItemCopy = newItem.id 
    set newItem = nothing 
End Function 

Tridionの2011コアサービス

private ItemType GetTridionItemType(RepositoryLocalObjectData source) 
{ 
    string itemType = source.GetType().Name; 
    switch (itemType) 
    { 
     case "ComponentData": 
      return ItemType.Component; 
     case "PageData": 
      return ItemType.Page; 
    } 
    return ItemType.UnknownByClient; 
} 

private string CreateNewItemCopy(string title, RepositoryLocalObjectData source, 
           string filename) 
{ 
    ItemType tridionItemType = GetTridionItemType(source); 
    string orgItemUri = source.LocationInfo.OrganizationalItem.IdRef; 
    var newItem = client.Copy(source.Id, orgItemUri, true, new ReadOptions()); 
    newItem.Title = title; 
    if (tridionItemType == ItemType.Page) 
    { 
     PageData pageData = newItem as PageData; 
     pageData.FileName = filename; 
     client.Update(pageData, new ReadOptions()); 
    } 
    else 
    { 
     client.Update(newItem, new ReadOptions()); 
    } 

    return newItem.Id; 
} 

* オリジナルコードの下に優秀な回答に基づいて

* 更新されたコード

private string CreateNewItemCopy(string title, RepositoryLocalObjectData source, 
           string filename) 
{ 
    string newItemUri = ""; 
    try 
    { 
     ItemType tridionItemType = GetTridionItemType(source.Id); 
     string orgItemUri = source.LocationInfo.OrganizationalItem.IdRef; 

     if (tridionItemType == ItemType.Component) 
     { 
      ComponentData sourceComp = source as ComponentData; 
      ComponentData newComponent = client.GetDefaultData(tridionItemType, 
                orgItemUri) as ComponentData; 
      newComponent.Title = title; 
      newComponent.Metadata = source.Metadata; 

      // ** Only Component has .Content and SchemaRef 
      newComponent.Content = sourceComp.Content; 
      newComponent.Schema.IdRef = sourceComp.Schema.IdRef; 
      client.Create(newComponent, null); 
      newItemUri = newComponent.Id; 
     } 
     else if (tridionItemType == ItemType.Page) 
     { 
      PageData sourcePage = source as PageData; 
      PageData newPage = client.GetDefaultData(tridionItemType, 
                orgItemUri) as PageData; 
      newPage.Title = title; 
      newPage.Metadata = source.Metadata; 

      // ** Only Page has .Filename 
      newPage.FileName = filename; 
      client.Create(newPage, null); 
      newItemUri = newPage.Id; 
     } 
     else // I would really like to handle all things here - but have problems with 
      // item-specific mandatory properties, such as Schema, Filename, and Dir 
     { 
      var newGenericTridionItem = client.GetDefaultData(tridionItemType, 
              orgItemUri) as RepositoryLocalObjectData; 
      newGenericTridionItem.Title = title; 
      newGenericTridionItem.Metadata = source.Metadata; 
      //if(GetTridionItemType(newGenericTridionItem.Id) == ItemType.Page) 
      // newGenericTridionItem.filename; 
      client.Create(newGenericTridionItem, null); 
      newItemUri = newGenericTridionItem.Id; 
     } 
    } 
    catch (Exception ex) 
    { 
     throw; 
    } 

    return newItemUri; 
} 

private ItemType GetTridionItemType(string uri) 
{ 
    const int itemTypeComp = 16; 
    const int itemTypePage = 64; 
    const int itemTypeSG = 4; 
    const int itemTypeFolder = 2; 
    int itemTypeInt = GetTridionItemTypeId(uri); 
    switch (itemTypeInt) 
    { 
     case itemTypeComp: 
      return ItemType.Component; 
      break; 
     case itemTypePage: 
      return ItemType.Page; 
      break; 
     case itemTypeSG: 
      return ItemType.StructureGroup; 
      break; 
     case itemTypeFolder: 
      return ItemType.Folder; 
      break; 
    } 
    return ItemType.UnknownByClient; 
} 

private int GetTridionItemTypeId(string uri) 
{ 
    const int itemTypeComp = 16; 
    string[] uriParts = uri.Split('-'); 

    if (uriParts.Length == 2) // comp, tcm:9-1234 
    { 
     return itemTypeComp; 
    } 
    else // other, tcm:9-456-64 for a page... 
    { 
     int itemTypeId = Int32.Parse(uriParts[2]); 
     return itemTypeId; 
    } 
} 

答えて

6

私は少しあなたのコードを調整してきたし、今では働いている:

private string CreateNewItemCopy(string title, RepositoryLocalObjectData source, string filename) 
    { 
     string newItemUri = ""; 
     try 
     { 
      ItemType tridionItemType = GetTridionItemType(source); 
      string orgItemUri = source.LocationInfo.OrganizationalItem.IdRef; 

      if (tridionItemType == ItemType.Component) 
      { 
       ComponentData sourceComp = source as ComponentData; 
       ComponentData newComponent = client.GetDefaultData(tridionItemType, orgItemUri) as ComponentData; 
       newComponent.Title = title; 
       newComponent.Metadata = source.Metadata; 

       // ** Only Component has .Content and SchemaRef 
       newComponent.Content = sourceComp.Content; 
       newComponent.Schema.IdRef = sourceComp.Schema.IdRef; 
       newItemUri = client.Create(newComponent, new ReadOptions()).Id; 
      } 
      else if (tridionItemType == ItemType.Page) 
      { 
       PageData sourcePage = source as PageData; 
       PageData newPage = client.GetDefaultData(tridionItemType, orgItemUri) as PageData; 
       newPage.Title = title; 
       newPage.Metadata = source.Metadata; 

       // ** Only Page has .Filename 
       newPage.FileName = filename; 
       newItemUri = client.Create(newPage, new ReadOptions()).Id; 
      } 
      else //I would really like to handle all things here - but have problems with item-specific mandatory properties, such as Schema, Filename, and Dir 
      { 
       var newGenericTridionItem = client.GetDefaultData(tridionItemType, orgItemUri) as RepositoryLocalObjectData; 
       newGenericTridionItem.Title = title; 
       newGenericTridionItem.Metadata = source.Metadata; 
       //if(GetTridionItemType(newGenericTridionItem.Id) == ItemType.Page) 
       // newGenericTridionItem.filename; 
       newItemUri = client.Create(newGenericTridionItem, new ReadOptions()).Id; 
      } 
     } 
     catch (Exception ex) 
     { 
      throw; 
     } 

     return newItemUri; 
    } 

    private ItemType GetTridionItemType(RepositoryLocalObjectData source) 
    { 
     string itemType = source.GetType().Name; 
     switch (itemType) 
     { 
      case "ComponentData": 
       return ItemType.Component; 
      case "PageData": 
       return ItemType.Page; 
     } 
     return ItemType.UnknownByClient; 
    } 

しかし、私はまだあなたがこのようにそれを行うにはしたくない理由を理解していないと、単純なコピー方法を使用していませんか?

+0

私はそれをやってみましたが、文字列.NameからTridion ItemTypeプロパティに移動する方法を理解できませんでした。 – robrtc

+0

書き換えられたGetTridionItemTypeメソッドにcopyとthanksを使用することをお勧めします。最後の1つの問題 - ページのコピーに対してファイル名プロパティを設定するにはどうすればよいですか?私はそれを 'Copy_of_pagename'に設定したままにしたくありません。 – robrtc

+0

ページfilenameを処理するためのif条件で上記のコードが更新されました。しかし、良い方法がありますか? – robrtc

関連する問題