Since then, I have made some ground on retrieving the image from a folder based upon the supplied path and creating a new image using System.Drawing.Image however I'm now stuck as to which property of the media object I pass this into.
Thank you Jason, I'm trying to do something similar and your example is very helpful. But I'm running into a glitch - I cannot find a method to check if a media item with the same name exists before I call mediaService.CreateMedia - the only media services methods I've found are GetById and GetMediaByPath, and all I have is the name (like "myTestImage") and the ancestor folder id.
And, if one traverses from an ancestor folder, what property does one check for a name match?
I'm doing this in a .cs file, making me wish I could use uQuery...but, no.
If I understand your question correctly, you are looking to check that a media item with a given name does not exist before creating a new media item in Umbraco. To do this, you can traverse from an ancestor folder and use a bit of Linq to solve your problem.
int ancestorFolderId = 2039;
IEnumerable<Imedia> images = ms.GetChildren(ancestorFolderId).Where(x => x.Name == "TheImageName");
// Or use .GetDescendants
This essentially asks Umbraco to give you all of the children of your ancestor folder that have the name "TheImageName". Typically you will only have one image named this so the IEnumerable
if(images.Count() > 0){
// Image already exists
// Access the image and its properties by using images.First()
}else{
//Image does not exist. Create a new one.
}
Always ensure that you check you IEnumerable has content before trying to use it though as it will error if you don't.
Required properties for creating an image with the Media service
Hi all,
I posted an original question here:
http://our.umbraco.org/forum/umbraco-7/using-umbraco-7/52982-Using-the-Umbraco-media-services-to-create-a-media-item-from-an-image-uploaded-to-a-specified-directory
Since then, I have made some ground on retrieving the image from a folder based upon the supplied path and creating a new image using System.Drawing.Image however I'm now stuck as to which property of the media object I pass this into.
My code can be found below:
As you can see I have tried to pass the image into umbracoFile but this doesn't seem to have worked and returns the following error:
Any help would be greatly appreciated.
Regards,
Jason
Hi,
I think you can set newImage.Path = "path to your file". And then saving should do the trick.
dave
Hi Dawoe,
Unfortunately, it's not as simple as that and that code doesn't actually do anything. Eventually I managed to crack it using this code:
This worked for me, thanks Jason.
Chris
Thank you Jason, I'm trying to do something similar and your example is very helpful. But I'm running into a glitch - I cannot find a method to check if a media item with the same name exists before I call mediaService.CreateMedia - the only media services methods I've found are GetById and GetMediaByPath, and all I have is the name (like "myTestImage") and the ancestor folder id.
And, if one traverses from an ancestor folder, what property does one check for a name match?
I'm doing this in a .cs file, making me wish I could use uQuery...but, no.
Hi Helen,
If I understand your question correctly, you are looking to check that a media item with a given name does not exist before creating a new media item in Umbraco. To do this, you can traverse from an ancestor folder and use a bit of Linq to solve your problem.
This essentially asks Umbraco to give you all of the children of your ancestor folder that have the name "TheImageName". Typically you will only have one image named this so the IEnumerable
Always ensure that you check you IEnumerable has content before trying to use it though as it will error if you don't.
Excellent! exactly what I needed. thanks!
Thanks for sharing this Jason - you just helped me avoid a few hours of head scratching tonight! :)
Maff
Thanks for sharing, Jason, this is excellent.
My question is how do you use this if you have created your own Media Type, with Image as a component.
[EDIT]
I found it - I must use CreateMedia but specify my own Media Type instead of "Image"
e.g.
is working on a reply...