Uploader and Document Link in Custom module
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
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();
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:
www.sitefinity.com/.../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!
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,
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;
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);
private SitefinityHyperLink[] slavoAssociatedDocs2;
[DataMember]
public SitefinityHyperLink[] SlavoAssociatedDocs2
get return slavoAssociatedDocs2;
set slavoAssociatedDocs2 = value;
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);
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