【问题标题】:Updating Components using the Core Service in SDL Tridion 2011使用 SDL Tridion 2011 中的核心服务更新组件
【发布时间】:2012-04-21 14:53:30
【问题描述】:

我正在使用 Tridion 2011 中的核心服务更新组件。

示例代码如下,

string COMPONENT_URI = "tcm:8-674";
string SCHEMA_URI = "tcm:8-426-8";

ComponentData component = client.TryCheckOut(COMPONENT_URI, null) as ComponentData;

try
{
    Response.Write("<BR>" + component.Content);
    XDocument xdoc = XDocument.Parse(component.Content);
    var element = xdoc.Elements("first").Single();
    element.Value = "updated";
    xdoc.Save(component.Content);
    client.Save(component, null);
    Response.Write("<BR"+"SAVED");
}
catch (Exception ex)
{
    Response.Write("Unable to save comp" + ex.Message);
}

client.CheckIn(COMPONENT_URI, null);

我收到以下异常:

 Unable to save compSequence contains no elements 

详情:

first - 组件中的字段名称

有人可以帮忙吗?

谢谢

【问题讨论】:

    标签: tridion


    【解决方案1】:

    您的代码尝试将 XML DOM 保存到文件中。

    XDocument.Save(string fileName) - 将此XDocument 序列化为文件,覆盖现有文件(如果存在)。

    你应该使用这样的东西:

    using (var client = new SessionAwareCoreServiceClient(netTcpBinding, remoteAddress))
    {
        ReadOptions readOptions = new ReadOptions();
        ComponentData component = client.Read(compTcmUri, readOptions) as ComponentData;
        XDocument dom = XDocument.Parse(component.Content);
        // do your modifications to dom
        component.Content = dom.ToString();
        component = client.Update(component, readOptions) as ComponentData;
    
        Console.WriteLine("Component updated: " + component.LocationInfo.WebDavUrl);
    }
    

    【讨论】:

    • 我已经尝试了上面的代码,但是我无法更新组件。
    【解决方案2】:

    Tridion 中组件的 XML 格式如下:

    <Content xmlns="uuid:2607D20D-1B22-4994-98C1-66D9ACF85C20">
      <first>The value of my first field</first>
      <second>The value of my second field</second>
    </Content>
    

    你的相关代码sn-p是:

    var element = xdoc.Elements("first").Single();
    

    这是选择元素失败,因为它是:

    1. 不为选择提供命名空间
    2. 只选择文档根目录的直接子节点

    如果您不指定命名空间,您似乎希望默认命名空间会被自动选择,但事实并非如此。一旦有了处理名称空间的 XML,每个查询都必须指定正确的名称空间。

    如果你修改代码来处理这两个问题,它应该看起来像这样:

    XNamespace ns = xdoc.Root.GetDefaultNamespace();
    var element = xdoc.Descendants(ns+"first").Single();
    

    您可能需要考虑阅读 .NET 处理 XML 中的命名空间以及一般的 XML 命名空间,因为这是一个非常常见的错误,您只需要快速退出系统即可。

    在您发现here 提供的帮助器类有用之前,希望通过核心服务更新组件 XML 的人。

    此外,正如 Mihai 指出的那样,您调用 XDocument.Save 的方式是错误的。它需要一个文件名作为它的参数,而你正在向它传递你的组件的 XML。

    【讨论】:

      【解决方案3】:

      有时,特别是如果您要从充当主存储库的不同存储库同步内容,您可以从头开始重建组件。 在这种情况下,下面是一个基本示例:

          public static void updateComponent()
          {
              string componentWdUrl = "/webdav/020%20Content/Building%20Blocks/Content/wstest/testComponent.xml";
              CoreServicesUtil coreServicesUtil = new CoreServicesUtil();
              coreServicesUtil.coreServiceClient.CheckOut(componentWdUrl, true, coreServicesUtil.readOptions);
              ComponentData componentData = coreServicesUtil.getComponentData(componentWdUrl);
              SchemaData schemaData = coreServicesUtil.getSchemaData(componentData.Schema.IdRef);
              componentData.Content = xmlUtil.GetNewXmlNode("Content", schemaData.NamespaceUri);
              componentData.Metadata = xmlUtil.GetNewXmlNode("Metadata", schemaData.NamespaceUri);
              componentData.AddSingleField("singlefield", "singlefield sample", schemaData.NamespaceUri);
              componentData = (ComponentData)coreServicesUtil.coreServiceClient.Save(componentData, coreServicesUtil.readOptions);
              coreServicesUtil.coreServiceClient.CheckIn(componentData.Id, coreServicesUtil.readOptions);
              coreServicesUtil.coreServiceClient.Close();
          }
      

      文章中解释了有关此示例中使用的方法的更多描述:

      Faulted State error while creating component with Core Service

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-13
        • 2012-04-11
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多