How to programmatically associate document upload to form response entry
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);
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();
entry.SetValue(
"FormFileUpload_C006"
, resultArray);