I've spent the day trying get an image added as a ContentLink to a Dynamic Module I've built in a Multi-Site Sitefinity 7.0 instance. I've searched the forums and Google and everything I've found doesn't work and gives me the following error at runtime.
An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code
Additional information: Object must implement IConvertible. Here's the code:
var providerName = "dynamicProvider2";
var cultureName = "en";
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type zooMomentType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ZooMoments.ZooMoment");
DynamicContent zooMomentItem = dynamicModuleManager.CreateDataItem(zooMomentType);
var splitDate = this.txtVisitationDate.Text.Split('/');
int.TryParse(splitDate, out months);
int.TryParse(splitDate, out years);
zooMomentItem.SetValue("VisitationDate", new DateTime(years, months, 1));
zooMomentItem.UrlName = new Lstring(Guid.NewGuid().ToString());
var image = UploadImage(uplZooMomentPhoto);
if (image != null)
//Throws Object must implement IConvertible.
//Doesn't throw error and inserts into the Database but the related item does not show up on the Add/Edit Screen of the module.
//var contentLinkManager = ContentLinksManager.GetManager();
//var link = contentLinkManager.GetContentLink(image.Parent.Id, typeof(Telerik.Sitefinity.Libraries.Model.Image), "MomentImage");
//if (link == null)
// link = contentLinkManager.CreateContentLink("MomentImage", zooMomentItem, image);
// link.ChildItemAdditionalInfo = image.ThumbnailUrl;
// link.ParentItemProviderName = providerName;
// link.ChildItemId = image.Id;
zooMomentItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft", new CultureInfo(cultureName));
So I left the 3 things I tried in the code above commented out. as changed with I am wondering if this has changed with Sitefinity 7 or if it is a difference with Multi-Site. The img variable is a Sitefinity Image so the type should be okay.
How can I add this related data programmatically without getting this error.
So I found the solution to my problem browsing through the depths JustDecompile on Sitefinity's codebase. I believe that the new related data items that were introduced in Sitefinity 7 have now broken the previous way of creating content links on dynamic modules. The methods in question are AddImage(), AddFile(), AddVideo() found in Telerik.Sitefinity.Model.
Using these methods results in the following exception at runtime:
Additional information: Object must implement IConvertible
.The solution to the problem resides with the extension methods found in Telerik.Sitefinity.RelatedData and the method to use is CreateRelation().
To fix my code I had to replace the following code
The new related data concept in Sitefinity 7 is a great new feature but I wish that some of this stuff would have been spelled out better. I burned through 2 days trying to find this solution. Hope this helps someone else!
Thanks for sharing these information impossible to find elsewhere...
However, I am trying to do the same with a file (doc is of type Document in my code below) and the files do not appear in the dynamic module:
Any idea how to resolve the problem?
Hello Sophie! It's been several years since I have worked on this but I will try to help you as best I can. Can you tell me what version of Sitefinity you are working on? Things might have changed between version 7.0 and 9.0.
Do you have reference to the document you are trying to relate to? In my example, I was also uploading an image. I would first insert and SaveChanges() that image in the Images modules. In your case, you would do this a document in the document & files module.
After the image was published, I then created my dynamic module item, and used the CreateRelation() code mentioned above with reference to the document in it.
Thanks a lot for your fast answer! Indeed I am working on Sitefinity 9.1.
And yes I uploaded the file in a sub-library of Document & Files library and used SaveChanges (this code has been already tested and works fine).
Then I used CreateRelation(doc,"CV") where "doc" has a type Document.
Do you think the problem is due to the fact that my CV is in a sub-library or due to the type of "doc" used in the method CreateRelation?
I found the problem...
I forgot to save changes of the dynamic module after the modifications