Question

I am using the following link's code to process metadata fields:-

https://code.google.com/p/tridion-practice/wiki/ChangeContentOrMetadata

But when I try to assign value to metadata field of date type then I am getting the following xml validation error:-

" System.ServiceModel.FaultException`1 [myPorject.serviceReference.CoreServiceFault]: XML validation error. Reason: The 'uuid:myUUID:dateField' element is invalid - The value '10/4/2012 03:04:00 AM' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:dateTime' - The string '10/4/2012 03:04:00 AM' is not a valid DateTime value.. (Fault Detail is equal to myPorject.serviceReference.CoreServiceFault)"

No matter whether I assign the value as string or DateTime data type I am getting the same error, I have also tried to overload the AddValue method with a Datetime data type but nothing has worked so far.

AddValue method's code snippet:-

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

Please suggest solution for both Date and Number data type. Thanks in advance

Was it helpful?

Solution 5

After some trouble shooting I finally resolve the issue and along with format of date. For this I need to match the time zone of my local machine with the server's Time zone.

And below is the code of overloaded method:-

      public void AddValue(DateTime value1)
    {
        DateTime value = value1 == null ? DateTime.MinValue : value1;

        XmlElement newElement = fields.AddFieldElement(definition);
        if (value != null) newElement.InnerText = value.ToString("yyyy-MM-ddThh:mm:ss");
    }

OTHER TIPS

The error message is quite explicit about the fact that your date format is wrong: The string '10/4/2012 03:04:00 AM' is not a valid DateTime value.

The format Tridion expects is YYYY-MM-DDThh:mm:ss, so no space, no time-zone and no AM/PM marker.

In official Tridion APIs you are typically shielded from having to do the conversion yourself, since they accept DateTime objects and convert it to the correct format for you. But since the helper class you use is not an official API, it takes a few shortcuts and makes you set the value as a string.

I typically use datetime.ToString("u").Replace(" ", "T").Replace("Z", "") to get the dates in the right format.

The date String should have a format like:

  • YYYY-MM-DDThh:mm:ss

Example:

  • "2012-10-03T09:39:43"

In addition to that, the way you are setting the date seems a bit complex to me, given that you are using the Content and Metadata wrapper, why not something like this:

string myStringDate = "2012-10-03T09:39:43";
fields["DateFieldName"].Value = myStringDate;
component.Metadata = fields.ToString();

As per my C# experience such error comes due to datetime format issue . you can try below code

DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd");

I have not tested it with your code but hope it will work in your case.

You are missing the 'T' separator between the date and time in your format. You did mention that the date that's output is in a format that you're trying to reinsert, but whatever is output can be formatted in anyway, and not necessarily the way you have to specify the date.

Have a close read to the XMLSchema dateTime specification here: http://www.w3.org/TR/xmlschema-2/#dateTime

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top