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);
zooMomentItem.SetValue("EmailAddress", txtEmailAddress.Text);
zooMomentItem.SetValue("FirstName", this.txtFirstName.Text);
zooMomentItem.SetValue("LastName", this.txtLastName.Text);
zooMomentItem.SetValue("HomeCity", this.txtHomeCity.Text);
zooMomentItem.SetValue("HomeState", this.ddlHomeState.SelectedValue);
zooMomentItem.SetValue("ZooLocation", this.ddlZooLocation.SelectedValue);
zooMomentItem.SetValue("MomentDescription", this.txtZooMomentDescription.Text);
var splitDate = this.txtVisitationDate.Text.Split('/');
int months;
int years;
int.TryParse(splitDate[0], out months);
int.TryParse(splitDate[1], out years);
zooMomentItem.SetValue("VisitationDate", new DateTime(years, months, 1));
zooMomentItem.UrlName = new Lstring(Guid.NewGuid().ToString());
zooMomentItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
zooMomentItem.SetValue("PublicationDate", DateTime.Now);
var image = UploadImage(uplZooMomentPhoto);
if (image != null)
{
//Throws Object must implement IConvertible.
zooMomentItem.SetValue("MomentImage", image);
//Throws Object must implement IConvertible.
//zooMomentItem.AddImage("MomentImage", img);
//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;
//}
//else
//{
// link.ChildItemId = image.Id;
//}
//contentLinkManager.SaveChanges();
}
zooMomentItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft", new CultureInfo(cultureName));
dynamicModuleManager.SaveChanges();
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.