TempTracer
TempTracer

Reputation: 137

Creating a multimedia component in Tridion Templating class

I am trying to create a multimedia component in Tridion templating classs with the below code.

MemoryStream stringInMemoryStream = new MemoryStream(ASCIIEncoding.Default.GetBytes("
<test>testing</test>"));                            
Component xmlMultimediaComponent = new Component(engine.GetSession(), new 
  TcmUri("tcm:21-2008-2"));                
xmlMultimediaComponent.Title = "New MM component";
xmlMultimediaComponent.Schema = new Schema(new TcmUri("tcm:10-6532-8"), 
 engine.GetSession());                
xmlMultimediaComponent.BinaryContent.MultimediaType = new MultimediaType(new 
   TcmUri("tcm:0-36-65544"), engine.GetSession());                                         
xmlMultimediaComponent.BinaryContent.MultimediaType.MimeType = "application/xml";
xmlMultimediaComponent.BinaryContent.MultimediaType.FileExtensions = new List<string> 
   {"xml", "XML"};
xmlMultimediaComponent.BinaryContent.UploadFromStream = stringInMemoryStream;
xmlMultimediaComponent.BinaryContent.Filename = "testing.xml".ToLower();                                    
xmlMultimediaComponent.Save();                
xmlMultimediaComponent.CheckIn(true);

Note that "tcm:21-2008-2" is folder TCM ID in which the component has to be created. The "tcm:10-6532-8" is the Multimedia schema ID and "tcm:0-36-65544" is the multimedia type ID.

However there seems to some issue and it is failing with below error:

Link to Schema has invalid value. at Tridion.ContentManager.Utilities.ThrowInvalidLinkException(Link link, KernelException innerException) at Tridion.ContentManager.IdentifiableObject.AssertValidLink(Link link) at Tridion.ContentManager.IdentifiableObject.AssertValidLinks(IEnumerable`1 links) at Tridion.ContentManager.IdentifiableObject.AssertValidLinks() at Tridion.ContentManager.IdentifiableObject.OnSaving(SaveEventArgs eventArgs) at Tridion.ContentManager.ContentManagement.RepositoryLocalObject.OnSaving(SaveEventArgs eventArgs) at Tridion.ContentManager.ContentManagement.VersionedItem.OnSaving(SaveEventArgs eventArgs) at Tridion.ContentManager.ContentManagement.Component.OnSaving(SaveEventArgs eventArgs) at Tridion.ContentManager.IdentifiableObject.Save(SaveEventArgs eventArgs) at Tridion.ContentManager.ContentManagement.VersionedItem.Save(Boolean checkInAfterSave) at Tridion.ContentManager.ContentManagement.VersionedItem.Save()

The above error is occuring at below statement xmlMultimediaComponent.Save().

Any idea how can we pass LinkToMultimedia type schema id to Multimedia component?

Upvotes: 5

Views: 649

Answers (1)

johnwinter
johnwinter

Reputation: 3624

I think the schema ID must contain the context publication ID, so in this case 10-6532-8 should be 21-6532-8.

Thanks

Upvotes: 11

Related Questions