Adding Related Items To Dynamic Content Items

Posted by Community Admin on 04-Aug-2018 21:49

Adding Related Items To Dynamic Content Items

All Replies

Posted by Community Admin on 22-Apr-2014 00:00

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.

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

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:

An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code

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
zooMomentItem.AddImage("MomentImage", img);

with
zooMomentItem.CreateRelation(image, "MomentImage");

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!

Posted by Community Admin on 13-Jan-2015 00:00

Brett, thank you for posting this. i was receiving the same error when trying to tie together related products (using the SetContentLink() extension method). the solution you posted to use Telerik.Sitefinity.RelatedData.CreateRelation() worked for my situation as well.

i agree that the documentation for the past few versions of Sitefinity is missing major information necessary to complete basic tasks (or else the info is nearly impossible to locate) - i've also resorted to using a decompiler (dotPeek) on several occasions.

Posted by Community Admin on 06-Oct-2016 00:00

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:

 theApplication.CreateRelation(doc, "CV");   

Any idea how to resolve the problem?

Posted by Community Admin on 06-Oct-2016 00:00

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.

Posted by Community Admin on 07-Oct-2016 00:00

Hello Brett,

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?

Thanks.

Posted by Community Admin on 07-Oct-2016 00:00

I found the problem...

I forgot to save changes of the dynamic module after the modifications

dynamicModuleManager.SaveChanges();

Posted by Community Admin on 07-Oct-2016 00:00

I'm glad I could be your rubber duck

This thread is closed