How to upload fies to Knowledge center via Content Service

I am working on a plugin creation that uploads a file to Knowledge center from your system
i know that i have to use contentService
but in contentService their is function named "upload(Document doc, java.lang.Integer unique)" which returns ContentOutputStream instance
but i am unable to understand how the document is getting saved into Knowledge center.
Anyone please help struck in this for a while

Thanks in advance

OriginalPostID-235089

  Discussion posts and replies are publicly visible

Parents
  • @shivamg

    I wont be able to attach screenshots but I will surely help you with code.
    Below is the code snippet of how to use create method.

    private Long createDocument(String documentName, Long saveInFolder, double size, ContentService contentService) {
    \ tDocument document = null;
    \ tLong generatedDocument = null;
    \ tint sizeInt = (int) Math.ceil(size);
    \ ttry {
    \ tdocument = new Document(saveInFolder, documentName, "zip");
    \ tdocument.setState(ContentConstants.STATE_ACTIVE_PUBLISHED);
    \ tdocument.setSize(sizeInt);
    \ tdocument.setFileSystemId(ContentConstants.ALLOCATE_FSID);
    \ tgeneratedDocument = contentService.create(document, ContentConstants.UNIQUE_NONE);
    \ t} catch (Exception e) {
    \ te.printStackTrace();
    \ t}

    \ treturn generatedDocument;

    \t}
    First create an InputStream of the original file which you want to upload.
    Then use the above logic to create a document and you get the document id.
    Use the document id to get the InternalFilename i.e the file path and create an OutputStream if the document created.
    Then read from the InputStream and write into the OutputStream.
    The document is uploaded into the KC and you have the document id too.

    Now that I am aware of upload method in the Appian API i would suggest you to go for upload method because it save a lot of coding and it might be more efficient too.
Reply
  • @shivamg

    I wont be able to attach screenshots but I will surely help you with code.
    Below is the code snippet of how to use create method.

    private Long createDocument(String documentName, Long saveInFolder, double size, ContentService contentService) {
    \ tDocument document = null;
    \ tLong generatedDocument = null;
    \ tint sizeInt = (int) Math.ceil(size);
    \ ttry {
    \ tdocument = new Document(saveInFolder, documentName, "zip");
    \ tdocument.setState(ContentConstants.STATE_ACTIVE_PUBLISHED);
    \ tdocument.setSize(sizeInt);
    \ tdocument.setFileSystemId(ContentConstants.ALLOCATE_FSID);
    \ tgeneratedDocument = contentService.create(document, ContentConstants.UNIQUE_NONE);
    \ t} catch (Exception e) {
    \ te.printStackTrace();
    \ t}

    \ treturn generatedDocument;

    \t}
    First create an InputStream of the original file which you want to upload.
    Then use the above logic to create a document and you get the document id.
    Use the document id to get the InternalFilename i.e the file path and create an OutputStream if the document created.
    Then read from the InputStream and write into the OutputStream.
    The document is uploaded into the KC and you have the document id too.

    Now that I am aware of upload method in the Appian API i would suggest you to go for upload method because it save a lot of coding and it might be more efficient too.
Children
No Data