I'm creating media items as part of an import from a CSV file. Each media item has a number of properties, one of which is "umbracoFile", a property which holds the associated PDF.
I'm using the API to create the media items like so:
Media newItem = Media.MakeNew(lineValues[1], MediaType.GetByAlias("MyMediaType"), User.GetUser(0), 5902);
And then setting the "umbracoFile" property like so:
newItem.getProperty("umbracoFile").Value = "The path to the file";
I'm then using File.Move to move the actual PDF file into the correct folder in the "Media" directory.
This all seems to work, except the file is not shown in the "Link to media item(s)" row when I view the media item, and the document is not being indexed by Examine either.
I've no idea what's going on. I'm guessing I'm missing something when I create the media item, perhaps I'm not saving it properly, or it needs to be published?
I see you're using the ID of the umbracoFile property in the file path instead of the ID of the newly created media item, which is what I was using. I've changed my code to use the umbracoFile ID for the file path but it still doesn't work.
Issue When Creating Media Items Using API
Hi,
I'm creating media items as part of an import from a CSV file. Each media item has a number of properties, one of which is "umbracoFile", a property which holds the associated PDF.
I'm using the API to create the media items like so:
Media newItem = Media.MakeNew(lineValues[1], MediaType.GetByAlias("MyMediaType"), User.GetUser(0), 5902);
And then setting the "umbracoFile" property like so:
newItem.getProperty("umbracoFile").Value = "The path to the file";
I'm then using File.Move to move the actual PDF file into the correct folder in the "Media" directory.
This all seems to work, except the file is not shown in the "Link to media item(s)" row when I view the media item, and the document is not being indexed by Examine either.
I've no idea what's going on. I'm guessing I'm missing something when I create the media item, perhaps I'm not saving it properly, or it needs to be published?
Can anyone help?
Robert
Don't see what could be the problem. Here is the code I always use to create media. Always seems to work:
Jeroen
Thanks Jeroen,
Your example is following roughly the same logic as what I'm doing, but mine doesn't work!
Can you tell me what media.XmlGenerate(newXmlDocument()); does?
Robert
It updates the media in the cmsContentXml table so when you use library.GetMedia(id, false) it will return the newest xml.
Jeroen
I see you're using the ID of the umbracoFile property in the file path instead of the ID of the newly created media item, which is what I was using. I've changed my code to use the umbracoFile ID for the file path but it still doesn't work.
Very strange.
Cracked it!
I wasn't setting the correct path for the umbracoFile property, it's all working fine now!
Thanks for posting your example, it put me on the right track.
Robert
is working on a reply...