2012-03-07 14 views
6

In Tridion 2011, desidero utilizzare l'equivalente del servizio principale di UpdateXml per creare nuovi oggetti Tridion in un modo generico. Intendo creare nuovi componenti, pagine e in seguito cartelle e gruppi di strutture. Funziona abbastanza bene con UpdateXml ma sto riscontrando un problema con il cast dello RepositoryLocalObject (o di un altro oggetto di tipo generico) su un oggetto ComponentData con il servizio principale. Il codice del mio servizio principale è molto più lungo (e cresce al secondo).Creazione di un elemento in Tridion 2011 tramite Core Service

messaggio

errore quando provo ad accedere su un oggetto di tipo proprietà specifica:

Errore 9 'Tridion.ContentManager.CoreService.Client.RepositoryLocalObjectData' non contiene una definizione per 'Contenuto' e nessun metodo di estensione 'contenuto' accettare un primo argomento di tipo 'Tridion.ContentManager.CoreService.Client.RepositoryLocalObjectData'

sarebbe una possibile soluzione quella di creare un metodo di estensione?

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 Servizio principale

* Aggiornato Codice Sulla base eccellente risposta qui sotto

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; 
} 

* codice originale

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; 
    } 
} 

risposta

6

ho un po 'aggiustato il codice e ora si sta lavorando:

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; 
    } 

Ma io continuo a non capire perché si vuole fare in questo modo e non usare semplice metodo Copy?

+0

L'ho provato in origine, ma non ho capito come passare dalla stringa .Name a una proprietà Tridion ItemType. – robrtc

+0

Eccellente suggerimento per utilizzare la copia e grazie per il metodo GetTridionItemType riscritto. Un ultimo problema: come impostiamo la proprietà filename per una copia di una pagina? Non voglio lasciarlo impostato su "Copia_del_pagina". – robrtc

+0

Codice aggiornato sopra con se condizione per gestire il nome file della pagina. Ma c'è un modo migliore? – robrtc

Problemi correlati