2012-08-30 90 views
2

我使用核心服務創建多媒體組件,並且一切工作正常。但是,當我在創建多媒體組件時使用多媒體架構定義元數據架構字段時,我收到以下錯誤: -使用核心服務創建多媒體組件使用核心服務

無法找到http://www.tridion.com/ContentManager/5.0/DefaultMultimediaSchema:Metadata

當我爲多媒體組件提供默認多媒體架構的TCM ID時,會顯示此消息。由於元數據字段保存在Tridion數據庫中,因此我首先必須從代理程序中檢索這些字段,或者爲此最好的解決方案是什麼,請建議。以下是示例代碼。請修改,如果有人有針對metadatafields提供默認值,以及如何檢索他們(有/無查詢經紀人DB)任何想法: -

public static string UploadMultiMediaComponent(string folderUri, string title, string schemaID) 
    { 
     core_service.ServiceReference1.SessionAwareCoreService2010Client client = new SessionAwareCoreService2010Client(); 
     client.ClientCredentials.Windows.ClientCredential.UserName = "myUserName"; 
     client.ClientCredentials.Windows.ClientCredential.Password = "myPassword"; client.Open(); 

     ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(
              ItemType.Component, folderUri); 
     multimediaComponent.Title = title; 

     multimediaComponent.ComponentType = ComponentType.Multimedia; 
     multimediaComponent.Schema.IdRef =schemaID; 

     //multimediaComponent.Metadata = ""; 

     StreamUpload2010Client streamClient = new StreamUpload2010Client(); 

     FileStream objfilestream = new FileStream(@"\My Documents\images.jpg", 
                FileMode.Open, FileAccess.Read); 
     string tempLocation = streamClient.UploadBinaryContent("images.jpg", 
                   objfilestream); 

     BinaryContentData binaryContent = new BinaryContentData(); 
     binaryContent.UploadFromFile = tempLocation; 
     binaryContent.Filename = "images.jpg"; 
     binaryContent.MultimediaType = new LinkToMultimediaTypeData() 
     { 
      // for jpg file 
      IdRef = "tcm:0-2-65544" 
     }; 
     multimediaComponent.BinaryContent = binaryContent; 

     IdentifiableObjectData savedComponent = client.Save(multimediaComponent, 
                  new ReadOptions()); 

     client.CheckIn(savedComponent.Id, null); 
     streamClient.Close(); 
     client.Close(); 
     Console.WriteLine(savedComponent.Id); 
     //} 
    } 
+0

您使用哪種版本的tridion?還有關於元數據解決的問題? –

+0

Huston,我正在使用SDL Tridion 2011 GA。我不明白你的元數據問題意味着什麼,因爲只有關於查詢或提供肉類數據字段的價值是我面臨的問題。 –

回答

2

我不知道爲什麼你的代碼不能正常工作,但下面的代碼是爲我工作

public static ComponentData GenerateMultiMediaComponent(TridionGeneration tridionGeneration, XmlData newsArticle, string componentName) 
     { 
      try 
      { 
       Dictionary<string, object> dicTridion = Common.GetTridionObject(tridionGeneration.client, ItemType.Component, tridionGeneration.Settings.ComponentFolderUri, componentName); 
       int objectCount = (int)dicTridion["count"]; 

       SchemaFieldsData schemaFields = tridionGeneration.client.ReadSchemaFields(tridionGeneration.Settings.SchemaUri, true, new ReadOptions()); 




       ComponentData componentData = (ComponentData)tridionGeneration.client.GetDefaultData(ItemType.Component, tridionGeneration.Settings.ComponentFolderUri); 

       if (schemaFields.Fields != null) 
       { 
        var fields = Fields.ForContentOf(schemaFields); 
        Helper.FillSchemaFields(tridionGeneration, fields); 
        componentData.Content = fields.ToString(); 
       } 

       if (schemaFields.MetadataFields != null) 
       { 
        var metafields = Fields.ForMetadataOf(schemaFields, componentData); 
        Helper.FillSchemaFields(tridionGeneration, metafields); 
        componentData.Metadata = metafields.ToString(); 
       } 

       componentData.Title = (objectCount == 0) ? componentName : componentName + " " + (objectCount + 1).ToString(); 
       componentData.ComponentType = ComponentType.Multimedia; 

       StreamUpload2010Client streamClient = new StreamUpload2010Client(); 

       FileStream objfilestream = new FileStream(@"[IMAGE_PATH]", FileMode.Open, FileAccess.Read); 
       string tempLocation = streamClient.UploadBinaryContent("images.jpg", objfilestream); 
       BinaryContentData binaryContent = new BinaryContentData(); 
       binaryContent.UploadFromFile = tempLocation; 
       binaryContent.Filename = "[IMAGE_NAME]"; 
       componentData.BinaryContent = binaryContent; 
       binaryContent.MultimediaType = new LinkToMultimediaTypeData() 
       { 
        IdRef = "tcm:0-2-65544" 
       }; 

       componentData = (ComponentData)tridionGeneration.client.Create(componentData, new ReadOptions()); 

       return componentData; 
      } 
      catch (Exception ex) 
      { 
       return null; 
      } 
     } 

這裏是輔助類:

public static class Helper 
    { 
     public static void FillSchemaFields(TridionGeneration tridionGeneration, Fields fields) 
     { 
      List<XmlData> data = XmlHelper.xmlData; 

      var ofield = fields.GetEnumerator(); 
      while (ofield.MoveNext()) 
      { 
       Field f = ofield.Current; 
       FillFieldValue(tridionGeneration, fields, f, data[0]); 
      } 
     } 

     private static void FillFieldValue(TridionGeneration tridionGeneration, Fields fields, Field f, XmlData data) 
     { 
      if (f.Type == typeof(MultimediaLinkFieldDefinitionData)) 
      { 
       fields[f.Name].Value = tridionGeneration.Settings.DefaultImageUri; 
      } 
      else if (f.Type != typeof(EmbeddedSchemaFieldDefinitionData)) 
      { 
       foreach (XmlData fieldvalue in data.Attributes) 
       { 
        if (f.Type == typeof(DateFieldDefinitionData)) 
        { 
         if (fieldvalue.text.ToLower() == f.Name.ToLower()) 
         { 
          fields[f.Name].Value = Convert.ToDateTime(fieldvalue.value).ToString("yyyy-MM-ddTHH:mm:ss"); 
         } 
         else 
         { 
          string val = FindSchemaValue(tridionGeneration, fieldvalue.Attributes, f.Name); 
          if (!string.IsNullOrEmpty(val)) 
          { 
           fields[f.Name].Value = Convert.ToDateTime(val).ToString("yyyy-MM-ddTHH:mm:ss"); 
          } 
         } 
        } 
        else 
        { 
         if (fieldvalue.text.ToLower() == f.Name.ToLower()) 
         { 
          fields[f.Name].Value = System.Net.WebUtility.HtmlEncode(fieldvalue.value); 
         } 
         else 
         { 
          string val = FindSchemaValue(tridionGeneration, fieldvalue.Attributes, f.Name); 
          if (!string.IsNullOrEmpty(val)) 
          { 
           fields[f.Name].Value = System.Net.WebUtility.HtmlEncode(val); 
          } 
         } 
        } 
       } 
      } 
      else 
      { 
       Fields fs = f.GetSubFields(); 
       var ofield = fs.GetEnumerator(); 
       while (ofield.MoveNext()) 
       { 
        Field ff = ofield.Current; 
        FillFieldValue(tridionGeneration, fs, ff, data); 
       } 
      } 
     } 

     private static string FindSchemaValue(TridionGeneration tridionGeneration, List<XmlData> data, string fieldname) 
     { 
      foreach (XmlData fieldvalue in data) 
      { 
       if (fieldvalue.text.ToLower() == fieldname.ToLower()) 
       { 
        return fieldvalue.value; 
       } 
       else 
       { 
        FindSchemaValue(tridionGeneration, fieldvalue.Attributes, fieldname); 
       } 
      } 
      return ""; 
     } 
    } 

和字段類:

public class Fields 
    { 
     private ItemFieldDefinitionData[] definitions; 
     private XmlNamespaceManager namespaceManager; 

     private XmlElement root; // the root element under which these fields live 

     // at any point EITHER data OR parent has a value 
     private SchemaFieldsData data; // the schema fields data as retrieved from the core service 
     private Fields parent; // the parent fields (so we're an embedded schema), where we can find the data 

     public Fields(SchemaFieldsData _data, ItemFieldDefinitionData[] _definitions, string _content = null, string _rootElementName = null) 
     { 
      data = _data; 
      definitions = _definitions; 
      var content = new XmlDocument(); 
      if (!string.IsNullOrEmpty(_content)) 
      { 
       content.LoadXml(_content); 
      } 
      else 
      { 
       content.AppendChild(content.CreateElement(string.IsNullOrEmpty(_rootElementName) ? _data.RootElementName : _rootElementName, _data.NamespaceUri)); 
      } 
      root = content.DocumentElement; 
      namespaceManager = new XmlNamespaceManager(content.NameTable); 
      namespaceManager.AddNamespace("custom", _data.NamespaceUri); 
     } 
     public Fields(Fields _parent, ItemFieldDefinitionData[] _definitions, XmlElement _root) 
     { 
      definitions = _definitions; 
      parent = _parent; 
      root = _root; 
     } 

     public static Fields ForContentOf(SchemaFieldsData _data) 
     { 
      return new Fields(_data, _data.Fields); 
     } 
     public static Fields ForContentOf(SchemaFieldsData _data, ComponentData _component) 
     { 
      return new Fields(_data, _data.Fields, _component.Content); 
     } 
     public static Fields ForMetadataOf(SchemaFieldsData _data, RepositoryLocalObjectData _item) 
     { 
      return new Fields(_data, _data.MetadataFields, _item.Metadata, "Metadata"); 
     } 

     public string NamespaceUri 
     { 
      get { return data != null ? data.NamespaceUri : parent.NamespaceUri; } 
     } 
     public XmlNamespaceManager NamespaceManager 
     { 
      get { return parent != null ? parent.namespaceManager : namespaceManager; } 
     } 

     internal IEnumerable<XmlElement> GetFieldElements(ItemFieldDefinitionData definition) 
     { 
      return root.SelectNodes("custom:" + definition.Name, NamespaceManager).OfType<XmlElement>(); 
     } 
     internal XmlElement AddFieldElement(ItemFieldDefinitionData definition) 
     { 
      var newElement = root.OwnerDocument.CreateElement(definition.Name, NamespaceUri); 

      XmlNodeList nodes = root.SelectNodes("custom:" + definition.Name, NamespaceManager); 
      XmlElement referenceElement = null; 
      if (nodes.Count > 0) 
      { 
       referenceElement = (XmlElement)nodes[nodes.Count - 1]; 
      } 
      else 
      { 
       // this is the first value for this field, find its position in the XML based on the field order in the schema 
       bool foundUs = false; 
       for (int i = definitions.Length - 1; i >= 0; i--) 
       { 
        if (!foundUs) 
        { 
         if (definitions[i].Name == definition.Name) 
         { 
          foundUs = true; 
         } 
        } 
        else 
        { 
         var values = GetFieldElements(definitions[i]); 
         if (values.Count() > 0) 
         { 
          referenceElement = values.Last(); 
          break; // from for loop 
         } 
        } 
       } // for every definition in reverse order 
      } // no existing values found 
      root.InsertAfter(newElement, referenceElement); // if referenceElement is null, will insert as first child 
      return newElement; 
     } 

     public IEnumerator<Field> GetEnumerator() 
     { 
      return (IEnumerator<Field>)new FieldEnumerator(this, definitions); 
     } 
     public Field this[string _name] 
     { 
      get 
      { 
       var definition = definitions.First<ItemFieldDefinitionData>(ifdd => ifdd.Name == _name); 
       if (definition == null) throw new ArgumentOutOfRangeException("Unknown field '" + _name + "'"); 
       return new Field(this, definition); 
      } 
     } 

     public override string ToString() 
     { 
      return root.OuterXml; 
     } 

    } 

    public class FieldEnumerator : IEnumerator<Field> 
    { 
     private Fields fields; 
     private ItemFieldDefinitionData[] definitions; 

     // Enumerators are positioned before the first element until the first MoveNext() call 
     int position = -1; 

     public FieldEnumerator(Fields _fields, ItemFieldDefinitionData[] _definitions) 
     { 
      fields = _fields; 
      definitions = _definitions; 
     } 

     public bool MoveNext() 
     { 
      position++; 
      return (position < definitions.Length); 
     } 

     public void Reset() 
     { 
      position = -1; 
     } 

     object IEnumerator.Current 
     { 
      get 
      { 
       return Current; 
      } 
     } 

     public Field Current 
     { 
      get 
      { 
       try 
       { 
        return new Field(fields, definitions[position]); 
       } 
       catch (IndexOutOfRangeException) 
       { 
        throw new InvalidOperationException(); 
       } 
      } 
     } 

     public void Dispose() 
     { 
     } 
    } 

    public class Field 
    { 
     private Fields fields; 
     private ItemFieldDefinitionData definition; 

     public Field(Fields _fields, ItemFieldDefinitionData _definition) 
     { 
      fields = _fields; 
      definition = _definition; 
     } 

     public string Name 
     { 
      get { return definition.Name; } 
     } 
     public Type Type 
     { 
      get { return definition.GetType(); } 
     } 
     public string Value 
     { 
      get 
      { 
       return Values.Count > 0 ? Values[0] : null; 
      } 
      set 
      { 
       if (Values.Count == 0) fields.AddFieldElement(definition); 
       Values[0] = value; 
      } 
     } 
     public ValueCollection Values 
     { 
      get 
      { 
       return new ValueCollection(fields, definition); 
      } 
     } 

     public void AddValue(string value = null) 
     { 
      XmlElement newElement = fields.AddFieldElement(definition); 
      if (value != null) newElement.InnerText = value; 
     } 

     public void RemoveValue(string value) 
     { 
      var elements = fields.GetFieldElements(definition); 
      foreach (var element in elements) 
      { 
       if (element.InnerText == value) 
       { 
        element.ParentNode.RemoveChild(element); 
       } 
      } 
     } 

     public void RemoveValue(int i) 
     { 
      var elements = fields.GetFieldElements(definition).ToArray(); 
      elements[i].ParentNode.RemoveChild(elements[i]); 
     } 

     public IEnumerable<Fields> SubFields 
     { 
      get 
      { 
       var embeddedFieldDefinition = definition as EmbeddedSchemaFieldDefinitionData; 
       if (embeddedFieldDefinition != null) 
       { 
        var elements = fields.GetFieldElements(definition); 
        foreach (var element in elements) 
        { 
         yield return new Fields(fields, embeddedFieldDefinition.EmbeddedFields, (XmlElement)element); 
        } 
       } 
      } 
     } 

     public Fields GetSubFields(int i = 0) 
     { 
      var embeddedFieldDefinition = definition as EmbeddedSchemaFieldDefinitionData; 
      if (embeddedFieldDefinition != null) 
      { 
       var elements = fields.GetFieldElements(definition); 
       if (i == 0 && !elements.Any()) 
       { 
        // you can always set the first value of any field without calling AddValue, so same applies to embedded fields 
        AddValue(); 
        elements = fields.GetFieldElements(definition); 
       } 
       return new Fields(fields, embeddedFieldDefinition.EmbeddedFields, elements.ToArray()[i]); 
      } 
      else 
      { 
       throw new InvalidOperationException("You can only GetSubField on an EmbeddedSchemaField"); 
      } 
     } 
     // The subfield with the given name of this field 
     public Field this[string name] 
     { 
      get { return GetSubFields()[name]; } 
     } 
     // The subfields of the given value of this field 
     public Fields this[int i] 
     { 
      get { return GetSubFields(i); } 
     } 

    } 
+0

這裏是什麼字段,這是弗蘭克寫的類。你也可以提供你在代碼中提到的Helper類的代碼嗎? –

+0

我不知道這門課是由坦率或不寫的,但我已經在網上找到了這門課。請參閱下面的文件內容。 –

+0

也請回復關於FillSchemaFields()的助手類是這個類,或者你從網上找到它。請提供助手類的代碼。 –

0

你可以試試嗎?

multimediaComponent.Metadata = "<Metadata/>";