More in this section

Forums / Developing with Sitefinity / How to programmatically associate document upload to form response entry

How to programmatically associate document upload to form response entry

2 posts, 0 answered
  1. Joe
    Joe avatar
    21 posts
    01 Dec 2010
    27 May 2014
    Link to this post

    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. 

    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);


  2. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    17 Aug 2017
    30 May 2014
    Link to this post
    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.

    Sabrie Nedzhip
    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
2 posts, 0 answered