How Can I give a path to mediapicker using content service
I am new in umbraco
I have a DocType (CreateMediaDT). This doctype has one textstring property (title) and one mediapicker property (media). I want to create a DocType programmaticallay (using content service)
I added a content with only textsring property. It was simple
var basicContent= Services.ContentService.CreateContent("Basic", pageId,"Basic");
basicContent.SetValue("title",model.Title);
I understand that textstring property can take string value. Because model.Title is string.
But I want to add a path for media picker with content service. I want to do this
var mediaContent= Services.ContentService.CreateContent("Media01", pageId,"MediaC");
mediaContent.SetValue("mediaPath" , media);
But It doesnt run.
I have a lot of document in a database. I need to create all document in umbraco. I did all but without image. I plan to add a mediapicker in document and give a path for mediapicker. How can I give a path mediapicker or other media property in a document with programmatically.
Advice me please.
Like Alex writes, you first need to upload the the file to the media archive using the media service.
The examples in the documentation could probably be improved a bit, so I've tried to illustrate creating a new media item (image) with the example below:
@{
// Get a reference to the media service
IMediaService mediaService = ApplicationContext.Services.MediaService;
// Declare a path to the local file on the disk
string path = "D:/photo-1460901419481-3fc27f0cdae9.jpg";
// Declare the media reference here as we need it later
IMedia media;
// Open a new file stream to the file
using (FileStream fs = new FileStream(path, FileMode.Open)) {
// Get the file name (aka not the full path)
string filename = Path.GetFileName(path);
// Create a new media item of the type "Image" (-1 is the root folder)
media = mediaService.CreateMedia(filename, -1, Constants.Conventions.MediaTypes.Image);
// Update the "umbracoFile" property (other properties are then automatically updated as well)
media.SetValue(Constants.Conventions.Media.File, filename, fs);
// Save the image
mediaService.Save(media);
}
}
Using an overload of the SetValue method, we can point Umbraco to a file in a stream, and Umbraco will then take care of the necessary logic of placing the image file in the correct location within the media archive, as well as update the other properties like width, height and file size.
With the media created, we can then continue to creating or updating your content items. In your other thread, it seemed that you already had creating the content, so the example below assumes that.
The exact value that you should save to your media property depends on the data type of the property. For instance, the default media picker in Umbraco 7.7 and newer will save the media's UDI opposed to the numeric ID of the media in earlier versions.
If the property editor used for the data type is called Umbraco.MediaPicker2, it uses UDIs, whereas Umbraco.MediaPicker saves a numeric ID (see image below for one way to find the name).
The example below assumes you're using Umbraco.MediaPicker2, and thus saves an UDI.
@{
// Get a reference to the content service
IContentService contentService = ApplicationContext.Services.ContentService;
// Get a reference to the content item you wish to update
IContent content = contentService.GetById(1103);
// Get the UDI of the media
GuidUdi mediaUdi = media.GetUdi();
// We need to convert the UDI to a string, as "SetValue" value doesn't support GuidUdi
string mediaUdiAsString = mediaUdi.ToString();
// Update the property value
content.SetValue("media", mediaUdiAsString);
// Make sure that we save and publish the change
contentService.SaveAndPublishWithStatus(content);
}
To summarize both examples (creating media and then updating content), it could look something like this partial view:
@using Umbraco.Core.Services
@inherits UmbracoViewPage
@{
// Get a reference to the media service
IMediaService mediaService = ApplicationContext.Services.MediaService;
// Declare a path to the local file on the disk
string path = "D:/photo-1460901419481-3fc27f0cdae9.jpg";
// Declare the media reference here as we need it later
IMedia media = mediaService.GetById(1147);
// Open a new file stream to the file
using (FileStream fs = new FileStream(path, FileMode.Open)) {
// Get the file name (aka not the full path)
string filename = Path.GetFileName(path);
// Create a new media item of the type "Image" (-1 is the root folder)
media = mediaService.CreateMedia(filename, -1, Constants.Conventions.MediaTypes.Image);
// Update the "umbracoFile" property (other properties are then automatically updated as well)
media.SetValue(Constants.Conventions.Media.File, filename, fs);
// Save the image
mediaService.Save(media);
}
// Get a reference to the content service
IContentService contentService = ApplicationContext.Services.ContentService;
// Get a reference to the content item you wish to update
IContent content = contentService.GetById(1103);
// Get the UDI of the media
GuidUdi mediaUdi = media.GetUdi();
// We need to convert the UDI to a string, as "SetValue" value doesn't support GuidUdi
string mediaUdiAsString = mediaUdi.ToString();
// Update the property value
content.SetValue("media", mediaUdiAsString);
// Make sure that we save and publish the change
contentService.SaveAndPublishWithStatus(content);
}
If you're using an API or surface controller instead, the code should look mostly the same.
Yes, when you have a media picker, you can enable a setting that allows you to select multiple media.
If you do this via the backoffice, Umbraco will simply save multiple UDIs which are than each separated by comma.
So if we are to do the same with code, and based on my previous example, it could look something like:
@using Umbraco.Core.Services
@inherits UmbracoViewPage
@{
// Declare a path to the local files on the disk
List<string> pathList = new List<string> {
"D:/photo-1430990480609-2bf7c02a6b1a.jpg",
"D:/photo-1428796741459-69571a6d5157.jpg"
};
// Get a reference to the media service
IMediaService mediaService = ApplicationContext.Services.MediaService;
// Initialize a new list for keeping track of the UDIs of each media
List<string> udiList = new List<string>();
// Iterate through the items in the path list
foreach (string path in pathList) {
// Declare the media reference here as we need it later
IMedia media = mediaService.GetById(1147);
// Open a new file stream to the file
using (FileStream fs = new FileStream(path, FileMode.Open)) {
// Get the file name (aka not the full path)
string filename = Path.GetFileName(path);
// Create a new media item of the type "Image" (-1 is the root folder)
media = mediaService.CreateMedia(filename, -1, Constants.Conventions.MediaTypes.Image);
// Update the "umbracoFile" property (other properties are then automatically updated as well)
media.SetValue(Constants.Conventions.Media.File, filename, fs);
// Save the image
mediaService.Save(media);
}
// Get the UDI of the media
GuidUdi mediaUdi = media.GetUdi();
// We need to convert the UDI to a string, as "SetValue" value doesn't support GuidUdi
string mediaUdiAsString = mediaUdi.ToString();
// Append the UDI to the list
udiList.Add(mediaUdiAsString);
}
// Get a reference to the content service
IContentService contentService = ApplicationContext.Services.ContentService;
// Get a reference to the content item you wish to update
IContent content = contentService.GetById(1103);
// Update the property value (the UDIs are separated by a comma)
content.SetValue("media", String.Join(",", udiList));
// Make sure that we save and publish the change
contentService.SaveAndPublishWithStatus(content);
}
How Can I give a path to mediapicker using content service
I am new in umbraco I have a DocType (CreateMediaDT). This doctype has one textstring property (title) and one mediapicker property (media). I want to create a DocType programmaticallay (using content service)
I added a content with only textsring property. It was simple
I understand that textstring property can take string value. Because model.Title is string.
But I want to add a path for media picker with content service. I want to do this
But It doesnt run. I have a lot of document in a database. I need to create all document in umbraco. I did all but without image. I plan to add a mediapicker in document and give a path for mediapicker. How can I give a path mediapicker or other media property in a document with programmatically. Advice me please.
HI Mehmet
For creating media you have to use MediaService - https://our.umbraco.org/documentation/reference/management/services/mediaservice
Content service for content items, MediaServices for media and files
There is CreateMedia method that you have to use - https://our.umbraco.org/documentation/reference/management/services/mediaservice#createmedia-string-name-int-parentid-string-mediatypealias-int
After creating media you have to save media id to "mediaPath" property of the document, something like that:
Thanks,
Alex
Thank you so much Alex.
Hi Mehmet,
Like Alex writes, you first need to upload the the file to the media archive using the media service.
The examples in the documentation could probably be improved a bit, so I've tried to illustrate creating a new media item (image) with the example below:
Using an overload of the
SetValue
method, we can point Umbraco to a file in a stream, and Umbraco will then take care of the necessary logic of placing the image file in the correct location within the media archive, as well as update the other properties like width, height and file size.With the media created, we can then continue to creating or updating your content items. In your other thread, it seemed that you already had creating the content, so the example below assumes that.
The exact value that you should save to your
media
property depends on the data type of the property. For instance, the default media picker in Umbraco 7.7 and newer will save the media's UDI opposed to the numeric ID of the media in earlier versions.If the property editor used for the data type is called Umbraco.MediaPicker2, it uses UDIs, whereas Umbraco.MediaPicker saves a numeric ID (see image below for one way to find the name).
The example below assumes you're using Umbraco.MediaPicker2, and thus saves an UDI.
To summarize both examples (creating media and then updating content), it could look something like this partial view:
If you're using an API or surface controller instead, the code should look mostly the same.
Hope that helps ;)
Hi Anders
Where were you that long? :)
incredible and awesome example. Thank you very much!
Again a question Anders
I did as you wrote. And it run. Thank you so much.
But I have a question
I have a path list which include ".jpg" files.
and I want to loop in list and add all photo in list to my content.
Just I could add only one photo.
Is there any way adding two or more photos to multiple medi pickers ?
Yes, when you have a media picker, you can enable a setting that allows you to select multiple media.
If you do this via the backoffice, Umbraco will simply save multiple UDIs which are than each separated by comma.
So if we are to do the same with code, and based on my previous example, it could look something like:
I did it. Thank you Sir
is working on a reply...