Add Related Media Item to Dynamic Content
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.
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();
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.
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);
LibrariesManager libMan = LibrariesManager.GetManager();
var image = libMan.GetImages().Where(i => i.Title ==
"MyImage"
&& i.Status == ContentLifecycleStatus.Master).FirstOrDefault();
myItem.CreateRelation(image,
"MyRelatedMediaFieldName"
);
myItem.CreateRelation(image,
"RelatedImages"
);
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.
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();