9

I'm attempting to call CoreServiceSession's ReadSchemaFields and pass the result to Frank's wrapper for content & metadata found here.

However, when I call ReadSchemaFields with the ID of a metadata schema used on a page I get an "Object reference not set to an instance..." error. I've quadruple-checked that the reference error is indeed from within the internal CoreService's call to ReadSchemaFields and I guess I'm wondering if I'm just missing something that states metadata schemas cannot be used with this method, that it's solely intended for components (which Frank's has an example of using it to get a folder's metadata schema fields, so I feel it's possible). With TOM.NET you could create an ItemFields object using either a component's data/schema, page's metadata/schema, keyword's metadata/schema, etc. I was expecting the ReadSchemaFields method to be similarly generic.

My code (simplified) is:

VersionedItemData item = client.Read(CIUtility.GetDynamicUri(CurrentWorkItem.Subject.IdRef)) as VersionedItemData;
if (item != null)
{
    log.Debug("Metadata " + item.Metadata);
    // Prints: Work Metadata - <Metadata xmlns="uuid:776EC2D6-37AD-4E56-86C2-26D1967D92D9">
    //                           <isSecure>Yes</isSecure>
    //                           <sessionTimeout>20</sessionTimeout>
    //                         </Metadata>

    log.Debug("Metadata Schema - " + item.MetadataSchema.IdRef);
    // Prints: Metadata Schema - tcm:5-35-8

    if (item.MetadataSchema != null && !String.IsNullOrWhiteSpace(item.MetadataSchema.IdRef) && !TcmUri.IsNullOrUriNull(new TcmUri(item.MetadataSchema.IdRef)))
    {
        log.Debug("Get schema fields");
        // Prints

        SchemaFieldsData schemaFields = client.ReadSchemaFields(item.MetadataSchema.IdRef, true, new ReadOptions());
        // Error occurs

        if (schemaFields == null)
            log.Debug("No schema fields");
            // Doesn't print

        log.Debug("Hello world.");
        // Doesn't print
        ...
    }
    ...
}

Am I just stuck with parsing the XML by hand if I'm dealing with Metadata? Or have I missed something terribly obvious (I am working on very little sleep right now)?

5

Your:

client.ReadSchemaFields(item.MetadataSchema.IdRef, true);

misses the ReadOptions parameter, so I'm wondering id your code compiles? Further, did you run to a debugger to check if the IdRef parameter is not null?

Other than that, I can read Metadata schemas just fine with the ReadSchemaFields method, but I see that the fields actually are stored in the MetadataFields property:

SchemaFieldsData s = coreService.ReadSchemaFields("tcm:3-1553-8", true, new ReadOptions());
s.Fields; // null
s.MetadataFields; // Holds definitions
| improve this answer | |
  • I have a wrapper class for the CoreServiceSession and it handles the ReadOptions - so yeah, it's present (I missed removing that obfuscation). The problem is that I'm not even getting a chance to try to read out the data (.Fields vs. .MetadataFields), the call itself to ReadSchemaFields errors with the null reference exception... Good to know it's possible, I guess I'll have to dig further at another time as I can't spend too much more time digging into why this one service call exceptions right now. – J Stuart Jun 10 '13 at 21:49
  • You also asked if I'd checked whether IdRef was not null and I have (I included the debug log output which included the schema ID). – J Stuart Jun 10 '13 at 21:51
  • When I get the chance, I'm going to play with different metadata schemas to see if there's something particular in the schema I was attempting to use and potentially identify what might be causing the error. I won't be able to do this test for a few days as I'm at SDL Innovate right now and my company is based in Colorado Springs (read wildfires) where we've shut down our lab environment in anticipation of possibly losing power... – J Stuart Jun 13 '13 at 16:16
4

I finally found my problem and I'm sad to say it was entirely a coding error on my part. To quote Alexander Klock: "It's easy to make one of those simple and stupid mistakes ... that, after realizing what you did wrong, you are extremely happy you found the issue, but at the same time still just a bit embarrassed that you missed it in the first place."

I actually did try to play with different metadata schemas and other permutations to see if I could find something that might have been causing an issue within the ReadSchemaFields call itself, but nothing seemed to work. Eventually I had to just work with the Metadata XML itself to accomplish what I needed.

However, a different area of my workflow implementation brought me back to this problem and resulted in head-banging and much gnashing of teeth. I could have sworn that I had simplified my code to essentially what I put in my original question (I had to remove some of the utility method references and such to make it clearer). I was virtually certain that I'd tried to log errors just after the call to ReadSchemaFields and those logged messages were never rendered - but I must have been wrong.

My problem ended up being in my code after making the ReadSchemaFields call and passing the result to Frank's Fields wrapper. I was checking to make sure my fields object wasn't null and then was checking to make sure the the value of a particular field wasn't null or whitespace. I was missing a check to make sure that field actually existed before getting it's value...

Pretty silly. I still feel like I must have just messed something else up when I was attempting to log debug messages that sent me down a very long rabbit hole...

Moral of the story: Make certain you remove unnecessary complexity when attempting to debug a problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.