1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Uploader and Document Link in Custom module

Uploader and Document Link in Custom module

6 posts, 0 answered
  1. Jonathan
    Jonathan avatar
    26 posts
    Registered:
    23 Jul 2012
    21 Nov 2012
    Link to this post
    I have a custom module wherein the backend users can create and edit individual "Business Opportunities". Each "Business Opportunity", along with various metadata (date of opportunity, tender type, etc), can have one or more associated documents.

    How will I go about adding document capability to the module? Presumably I will need to create a custom library to store the documents on module install; I will need to put an uploader / picker on the backend form; and I will need some kind of metafield or other to store the various documents associated with a given opportunity. Is this correct?

    And what widgets / controls / parts of the API will I need to work with to make this happen? If anyone has examples (in the SDK or otherwise) that illustrate parts of this, I'm all ears.

    Thanks in advance

    Jonathan
  2. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    23 Nov 2012
    Link to this post
    Hello Jonathan,

    I suppose your custom module is implemented in code and not created through the Sitefinity module builder. Otherwise you'd have probably seen the Media field that you can add to each item.

    If the module is not a dynamic one, you can have a field in your Business Opportunity type which represents a content link. To make an actual link between two items (a business opportunity and a document), you need to create a ContentLink object and set its properties. You can use the ContentLinkManager API to do that.

    var contentLinkMan = ContentLinkManager.GetManager();
    var newLink = contentLinkMan.CreateContentLink("LinkPropertyName", businessOpportunity, attachedDoc);
    contentLinkMan.SaveChanges();

    In the above snippet, "LinkPropertyName" is the name of a dynamic field in your business opportunity class, "businessOpportunity" is an object that you want to add a document to, while "attachedDoc" is the actual document you want to attach.

    In terms of UI, you should implement a field control in your create/edit screen, which can be used to pick a document each time you are creating a business opportunity. One such selector is implemented for Images here:
    http://www.sitefinity.com/blogs/slavoingilizov/posts/slavo-ingilizovs-blog/2011/02/11/creating_a_thumbnail_selector_for_news_items

    You will need to modify it to work with documents.

    Regards,
    Slavo
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. Jonathan
    Jonathan avatar
    26 posts
    Registered:
    23 Jul 2012
    23 Nov 2012 in reply to Slavo
    Link to this post
    Hi Slavo,

    Thanks for the pointers. I'm glad to see that (it appears) I will not have to handle the uploading manually - that control has the upload functionality included. I also saw that there is a blog post by Sletoslav, taking your image picker and extending it for documents:

    http://www.sitefinity.com/blogs/svetoslavpetsov/posts/svetoslav-petsovs-blog/2012/02/08/selecting_documents_in_field_controls_with_editorcontentmanagerdialog

    so I will start to look into that one.

    How about handling multiple documents? The requirement is that the user can associate one *or more* documents to the opportunity. How is this accomplished?

    Thanks!
  4. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    28 Nov 2012
    Link to this post
    Hello Jonathan,

    In order to handle multiple documents, you can create multiple ContentLink objects. You can call the CreateContentLink method from my previous post multiple times, giving it a different document each time. The only condition is that the custom field you've created has to be an array of ContentLink objects (ContentLink[]). Let me know if you need anything else.

    Kind regards,
    Slavo
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  5. Jonathan
    Jonathan avatar
    26 posts
    Registered:
    23 Jul 2012
    28 Nov 2012
    Link to this post
    Slavo,

    I have now brought the document picker / image picker custom field over into my dynamic module, and it is working. I want to change it in the following ways:

    1. allow selection of more than 1 file
    2. display selected documents by their name, not the guid of the doc
    3. allow user to remove arbitrary documents after they have been selected. I see this working similar to the tags field, where individual docs would be presented in a list with little "x"s to the right of the document title.

    So the first part! I sort of understand your comment about requiring ContentLink[] to store the multiple docs -- that makes sense in theory. In my data class, I currently have:

    private string slavoAssociatedDocs;
     
    [UserFriendlyDataType(UserFriendlyDataType.ShortText)]
    [DataMember]
    public string SlavoAssociatedDocs
    {
        get { return slavoAssociatedDocs; }
        set { slavoAssociatedDocs = value; }
    }

    And then in my definition file, I reference the field control like this:

    var documentSelector = new DocumentSelectorFieldControlDefinitionElement(mainSection.Fields)
    {
        ID = "associatedDocumentsFieldControl",
        DataFieldName = "SlavoAssociatedDocs",
        DisplayMode = displayMode,
        FieldType = typeof(DocumentSelectorFieldControl),
        Title = "Associated Documents",
        Description = "Documents associated with opportunity",
        CssClass = "sfTitleField",
        WrapperTag = HtmlTextWriterTag.Li
    };
    mainSection.Fields.Add(documentSelector);

    I've renamed the field control to "DocumentSelectorFieldControl"

    So now I'm thinking I need to change my data class to:

    private SitefinityHyperLink[] slavoAssociatedDocs2;
     
    [DataMember]
    public SitefinityHyperLink[] SlavoAssociatedDocs2
    {
        get { return slavoAssociatedDocs2; }
        set { slavoAssociatedDocs2 = value; }
    }

    Correct? And then what about the definition file? Is it enough to simply do like this:

    var documentSelector = new DocumentSelectorFieldControlDefinitionElement(mainSection.Fields)
    {
        ID = "associatedDocumentsFieldControl",
        DataFieldName = "SlavoAssociatedDocs2",
        DisplayMode = displayMode,
        FieldType = typeof(DocumentSelectorFieldControl),
        Title = "Associated Documents",
        Description = "Documents associated with opportunity",
        CssClass = "sfTitleField",
        WrapperTag = HtmlTextWriterTag.Li
    };
    mainSection.Fields.Add(documentSelector);

    And finally (for now), how do I need to change the code behind on the field control? It inherits from "TextField", which I'm thinking isn't going to work with array of hyperlink... Maybe I need to persist in DB as string, like originally?

  6. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    28 Aug 2017
    04 Dec 2012
    Link to this post
    Hello Jonathan,

    Thank you for getting back to us.

    It is indeed true that we do not have a sample demonstrating multiple items field using ContentLinks, thank you for your feedback on that area, we would definitely include this in our plans for the upcoming Sitefinity samples.

    In the meantime Slavo has a blog demonstrating how you can actually extend his thumbnail selector to support multiple items selection. Persisting the data happens as comma delimited string of the item IDs, which allows for easier splicing these back into an array on the client and server. The blog post is available here, hope you find it useful.

    Regards,
    Boyan Barnev
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
6 posts, 0 answered