Add Related Media Item to Dynamic Content

Posted by Community Admin on 04-Aug-2018 06:57

Add Related Media Item to Dynamic Content

All Replies

Posted by Community Admin on 09-Sep-2014 00:00

Hi All, 

Today, I get the error " Object must implement IConvertible." when I try to Add Related Media item to Dynamic Content, I have Ajax to Upload Image to Sitefinity Administrator. this is the code.

var doctorType = TypeResolutionService.ResolveType(ConstUtil.DoctorType);
var liveItem = dynamicModuleManager.GetDataItem(doctorType, Authentication.Id);

dynamicModuleManager.Provider.SuppressSecurityChecks = true;

var masterItem = dynamicModuleManager.Lifecycle.GetMaster(liveItem) as DynamicContent;
var cultureName = CurrentCulture;

Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);

var draftItem = dynamicModuleManager.Lifecycle.CheckOut(masterItem) as DynamicContent;

if (draftItem == null)

                    return new HttpResponseMessage
         
                Content = new JsonContent(new Result = false )
          ;
 

var newImage = UploadImage(model);

if (newImage != null)

           //var contentLink = new ContentLink(newImage.Parent.Id, newImage.Id);
            //draftItem.SetValue("photo" , NewImage);

// Now we need to check in, so the changes apply
var checkInDoctorItem = dynamicModuleManager.Lifecycle.CheckIn(draftItem);

dynamicModuleManager.Lifecycle.Publish(checkInDoctorItem);
dynamicModuleManager.SaveChanges();

 

I Try to fix this error by two ways

1) draftItem.AddImage("photo", newImage); It still get same Error

2) draftItem.CreateRelation(newImage, CustomFieldName.DoctorProfilePhoto); 

It not get an error, but it cannot save the new Image to the target.

 So I Use Sitefinity 7.0, please help me to solve it.

Posted by Community Admin on 11-Sep-2014 00:00

Hi David,

I see several reasons for your code to behave in this matter. First off, provided you have a related media field, it expects an object of type Telerik.Sitefinity.Libraries.Model.Image and I am not certain yours is of this type. Secondly, the CreateRelation method is the correct one to go with. For it to work though you need not check out a draft of the dynamic item. Simply get the master version of both the item and the image, create the relation, publish both of the master versions and save changes for both managers. Here is a code sample for further reference:

LibrariesManager libMan = LibrariesManager.GetManager();
               DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
               Type myType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyModule.MyType");
               DynamicContent myItem = dynamicModuleManager.GetDataItems(myType).Where(a => a.Status == ContentLifecycleStatus.Master).FirstOrDefault();
               var image = libMan.GetImages().Where(i => i.Title == "MyImage" && i.Status == ContentLifecycleStatus.Master).FirstOrDefault();
               myItem.CreateRelation(image, "MyRelatedMediaFieldName");
             
               libMan.Lifecycle.Publish(image);
               dynamicModuleManager.Lifecycle.Publish(myItem);
               libMan.SaveChanges();
               dynamicModuleManager.SaveChanges();



Regards,
Ivan D. Dimitrov
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 15-Sep-2014 00:00

Hello
Ivan D. Dimitrov,

Thanks for your reply, in my code I used "Telerik.Sitefinity.Libraries.Model.Image ", and I had tried your solution, but it not working (not show exception). So I don't understand what happen with my code. If you have any ideas please helps me know why it not work.

Thanks and Best Regards.

Posted by Community Admin on 18-Sep-2014 00:00

Hi David,

As far as I can see from your sample code, you first upload the image to Sitefinity and then try to add the uploaded image to a dynamic content item as a related image. Can you please try to comment out this line from your code:

var newImage = UploadImage(model);

and try to get an already uploaded image using the sample code below, as Ivan has suggested:

LibrariesManager libMan = LibrariesManager.GetManager();
var image = libMan.GetImages().Where(i => i.Title == "MyImage" && i.Status == ContentLifecycleStatus.Master).FirstOrDefault();

After you get the already uploaded image you can try to relate it with your dynamic module using the CreateRelation() method as shown below:

myItem.CreateRelation(image, "MyRelatedMediaFieldName");

This will help us to narrow down the issue and check if it is caused while uploading the image.

It is also important to double check and make sure that you use the correct field name for the related data image field. In order to check the field name you can go to Administration -> Module Builder and click on your dynamic module and check the custom field names. Here is a sample screenshot. As shown in the screenshot, the related data image field name for my test module is RelatedImages and in this case the code for creating the relation should look like the following:

myItem.CreateRelation(image, "RelatedImages");

If the issue still persists, can you please try to run the code in debug mode and let me know the exact line where the issue occurs.

Regards,
Sabrie Nedzhip
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 19-Sep-2014 00:00

Hi Sabrie Nedzhip ,

 Thanks for your post, I had tried your solution.

Firstly, I get the master image, Create Relation and  then publish it with the following code: 

var image = libMan.GetImages().Where(i => i.Title =="MyImage" && i.Status == ContentLifecycleStatus.Master).FirstOrDefault(); 

 myItem.CreateRelation(image, "MyRelatedMediaFieldName");                            

libMan.Lifecycle.Publish(image);

and then I Publish the dynamicModule

dynamicModuleManager.Lifecycle.Publish(myItem);

libMan.SaveChanges();               

dynamicModuleManager.SaveChanges();

it not working, so I try to debug my code to find the problems, but not any exception show. 

Posted by Community Admin on 23-Sep-2014 00:00

Hello David,

You should be able to create relation using the sample code I have provided. Here is a short video demonstrating the result on my side when I executed the code. Here is the sample code from the video I have recorded:

var providerName = String.Empty;
            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
 
            LibrariesManager libMan = LibrariesManager.GetManager();
            Type myType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Mymodules.MyModule");
 
            DynamicContent myItem = dynamicModuleManager.GetDataItems(myType)
                .Where(a => a.Status == ContentLifecycleStatus.Master)
                .FirstOrDefault();
 
            var image = libMan.GetImages().Where(i => i.Title == "Sofia" && i.Status == ContentLifecycleStatus.Master).FirstOrDefault();
 
            myItem.CreateRelation(image, "RelatedImages");
            dynamicModuleManager.SaveChanges();
 
            libMan.Lifecycle.Publish(image);
            dynamicModuleManager.Lifecycle.Publish(myItem);
            libMan.SaveChanges();
            dynamicModuleManager.SaveChanges();

Can you please make sure that you properly get the image on your side by title. And then please make sure that you use the correct value for the related data field name when creating the relation. In my example the related data field name for the images is RelatedImages. Please note to replace this value with the name of the field on your side.

You may also take a look at the following article for more details about creating relation between an item and a related item.

Regards,
Sabrie Nedzhip
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed