How to programmatically associate document upload to form re

Posted by Community Admin on 04-Aug-2018 14:22

How to programmatically associate document upload to form response entry

All Replies

Posted by Community Admin on 27-May-2014 00:00

Hey everyone, I'm trying to programmatically create a form response with an associated document that will be uploaded.  I found the forum post that shows the basics on how to create the form entry, but I'm not sure how I would go about associating a document after uploading it.

www.sitefinity.com/.../create-form-responses-programmatically- 

I'm creating a document in a library under Documents, but I just don't see in the API reference how I would associate the given document with the form response.

I've created the Form in the backend already with my fields and also an upload field.  Do I just put the Guid of the uploaded document in the field name like this, or is there a method on form entry to do this?

entry.SetValue("FormFileUpload_C006", uploadedGuid);

 

Posted by Community Admin on 30-May-2014 00:00

Hello Joe,

Can you please try to set the document file as a ContentLink[]. Here is a sample code for your reference:

LibrariesManager libraryManager = LibrariesManager.GetManager();
var contentLinksManager = ContentLinksManager.GetManager();
   
var document = libraryManager.GetDocuments().Where(d => d.Title == "DocumentName").FirstOrDefault();

ContentLink contentLink = new ContentLink();
contentLink.Id = Guid.NewGuid();
contentLink.ParentItemId = entry.Id;
contentLink.ParentItemType = entry.GetType().ToString();
contentLink.ChildItemId = document.Id;
contentLink.ComponentPropertyName = entry.Id.ToString();
contentLink.ChildItemAdditionalInfo = "additional info";
contentLink.ChildItemProviderName = (((IDataItem)document).Provider as DataProviderBase).Name;
contentLink.ChildItemType = document.GetType().FullName;
contentLink.ApplicationName = contentLinksManager.Provider.ApplicationName;
   
ContentLink[] contentLinks = new ContentLink[0];
if (contentLinks == null)
    contentLinks = new ContentLink[0];


var assetsFieldList = contentLinks.ToList();
assetsFieldList.Insert(0, contentLink);
ContentLink[] resultArray = assetsFieldList.ToArray();

Then please set the value of the upload field to the resultArray as follows:

entry.SetValue("FormFileUpload_C006", resultArray);

I hope this information helps. Please let me know if you need any further assistance on implementing this.

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