Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tridion: Errors when setting a page's metadata schema in the event system

This is on Tridion 2011 SP1.

I'm using the component save event in the Tridion event system to create a corresponding page and associate some metadata with that page. When specifying a schema, I am getting event logs that indicate that the UUID of the schema cannot be found.

The UUID that is in the error is the for the metadata schema I want to use, and I'm also using a local TcmUri for the metadata schema. I'm at a bit of a loss at the moment.

The .NET and resulting error are below:

Code

public static TcmUri CreatePage(TcmUri parentSgId, Component component, TcmUri componentTemplateUri, TcmUri metaDataSchemaUri = null)
    {
        Logging.Debug("Attempting to create page in " + parentSgId.ToString());
        Page page = new Page(component.Session, parentSgId);
        page.Title = component.Title;
        page.FileName = component.Title;
        // Add a metadata schema
        if (metaDataSchemaUri != null)
        {
            TcmUri localMetaDataSchemaUri = TransformTcmUri(metaDataSchemaUri, parentSgId);
            page.MetadataSchema = (Schema)page.Session.GetObject(localMetaDataSchemaUri);
        }
        // Add the CP
        TcmUri localComponentUri = Helpers.TransformTcmUri(component.Id, parentSgId);
        TcmUri localComponentTemplateUri = Helpers.TransformTcmUri(componentTemplateUri, parentSgId);
        page.ComponentPresentations.Add(new ComponentPresentation(new Component(localComponentUri, component.Session), new ComponentTemplate(localComponentTemplateUri, component.Session)));
        try
        {
            page.Save(true);
            Logging.Debug("Created page successfully " + page.Id.ToString());
            return page.Id;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

    }

Error

Unable to find uuid:C42EE4FC-D2A2-49F5-92C7-BF6DCB014343:Metadata.
Component: Tridion.ContentManager Errorcode: 0 User: EMAKINA\MTSUser
StackTrace Information Details: at
Bair.Tridion.Events.Utilities.Helpers.CreatePage(TcmUri parentSgId,
Component component, TcmUri componentTemplateUri, TcmUri
metaDataSchemaUri) [...]
like image 733
Mark Williams Avatar asked Feb 29 '12 15:02

Mark Williams


2 Answers

The problem, as pointed out by Quirijin was that I wasn't setting the metadata for the page before attempting to save it. The working code is below.

Setting the page metadata

if (metaDataSchemaUri != null)
            {
                Helpers.SetPageMetaData(metaDataSchemaUri, parentSgId, component, ref page);
            }

The SetPageMetaData method

 protected static void SetPageMetaData(TcmUri metaDataSchemaUri, TcmUri parentSgId, Component component, ref Page page)
        {
        TcmUri localMetaDataSchemaUri = TransformTcmUri(metaDataSchemaUri, parentSgId);
        page.MetadataSchema = (Schema)page.Session.GetObject(localMetaDataSchemaUri);
        ItemFields metaFields = new ItemFields((Schema)page.Session.GetObject(localMetaDataSchemaUri));
        Logging.Debug("Schema title: " + page.MetadataSchema.Title);
        // Set the page metadata

            TextField pageTitle = (TextField)metaFields["pagetitle"];
            pageTitle.Value = "The page title";
[...]
            KeywordField showbreadcrumb = (KeywordField)metaFields["showbreadcrumb"];
            showbreadcrumb.Value = new Keyword(TransformTcmUri(new TcmUri("tcm:134-12018-1024"), parentSgId), page.Session);
[...]
        }
        page.Metadata = metaFields.ToXml();
        Logging.Debug("Page metadata set");
    }
like image 155
Mark Williams Avatar answered Oct 31 '22 03:10

Mark Williams


I was able to replicate your error in my own image. After some investigation I was able to figure out what the issue. The error "Unable to find uuid:C42EE4FC-D2A2-49F5-92C7-BF6DCB014343:Metadata" means that setting the Schema is ok, but it can't find that element in the Page's XML. You have to explicitly set .Metadata as well! Here's what I did to successfully save a Page and add the metadata schema:

    private void SetMetadata(Page page, SaveEventArgs eventArgs, EventPhases phases)
    {
        try
        {
            Schema schema = (Schema)page.Session.GetObject("tcm:3-5806-8");

            if (page.MetadataSchema == null)
            {
                page.MetadataSchema = schema;
                ItemFields metadata = new ItemFields(schema);
                TextField showInNav = (TextField)metadata["showinmenu"];
                showInNav.Value = "No";
                page.Metadata = metadata.ToXml();
                SetPageMetadata.LogMessage("Set Page Metadata");
            }
        }
        catch (Exception e)
        {
            SetPageMetadata.LogMessage("An error occurred while rsetting the Page metadata:\n" + e.Message);
        }
    }
like image 33
Jeremy Grand-Scrutton Avatar answered Oct 31 '22 01:10

Jeremy Grand-Scrutton