How to send Document to MS Sharepoint programmatically using Export Scripts

  • 1K Views
  • Last Post 18 October 2018
ScanAddicted posted this 11 February 2014

As I understand that ABBYY Flexicapture 10 has powerful MS Sharepoint Export that is easy to configure, in several cases I need to export document with autocreating of subfolder and contenttype.
In my case I have an invoice document that must be stored in MS Sharepoint based on path : RootFolder\Year\Month\Date\InvoiceNumber\Documents with content type invoices.

In this scenario : Rootfolder and contenttype and also contenttypeid already created and known and I cannot use SharepointServices because we are not in the sharepoint developer environment and only depend on webservices.

Is there any clue how to do it?

Order By: Standard | Newest | Votes
Andrey Zyuzin posted this 12 February 2014

Hi

This is reasonable request. Unfortunately for now FlexiCapture 10 doesn't provide such standard functionallity as SharePoint's folders creation.
Do you have an access to SharePoint API in your environment?

ScanAddicted posted this 14 February 2014



Hi Andrey,

Thanks for the reply.

I have already solved the problem. I dont have Sharepoint API Access (sharepoint.dll) but Sharepoint provide this webservices to be access using network credentials and this credential has permission to create folder and create documents in ms sharepoint within the library.

I create DLL from this library named DWSWebservices.DWS (using add webservices reference and compile)


Webservices To Create Folder : http:\\[sharepointserver]\_vti_bin\dws.asmx (just plain folder in ms sharepoint)

Sample Code :

dim path as string = "MyAccountPayable/2014"
'(the folder I want to create)
Dim myclientdws as new DWSWebservices.DWS
myclientdws.Credential = new Net.NetworkCredential(myuser,mypassword)\
myclientdws.url = http:\\[sharepointserver]\_vti_bin\dws.asmx
'(just incase I want to use this DLL for other server, I just update the url address of this webservice)
Dim strresult as string = ""
strresult = myclientdws.CreateFolder(path)

If I want to create folder _> subfolder , I just iterate those code.









Andrey Zyuzin posted this 14 February 2014

Thank you for sharing your expirience. And how are you going to put the document to the specific folder? I guess that you need to export documents to some "temp" folder (that has permanent path) at SharePoint Library using standard FlexiCapture export to SharePoint, and then, for example using FlexiCapture after export script event, move exported document to the specific folder.

ScanAddicted posted this 14 February 2014

Yes I use after export script but for different purpose.


First save image in PDF using export script of ABBYY in mylocaltemp directory using :

document.saveas(LocalFIlename,imageoptions)




To send the document to the folder i am using other webservices provided by Sharepoint. (http://[SharepointServer]/_vti_bin/copy.asmx )

I copy the webservices into DLL named WSCopy.dll

Dim mycopy as new wscopy.wscopy.copy
mycopy.credentials = new net.networkcredential(myuser,mypassport)
mycopy.url = http://[Sharepointserver]/_vti_bin/copy.asmx
Dim contenttype as new FieldInformation
Contenttype.DisplayName = "Content Type ID"
Contenttype.InternalName = "ContentTypeID"
Contenttype.Type = FieldType.Text
Contenttype.value = "0x01......96DC66" ' (please see sharepoint guide how to find content id

Dim information1 as new FieldInformation
information1.DisplayName = "Invoice No"
Information1.InternalName = "InvoiceNo"
information1.Type = FieldType.Text
Information1.Value = "252636376373"
'To export document to sharepoint please do not create datefield without value or else error can be happen
' If you had null value its better not to write fieldinformation array

Dim information2 as new FieldInformation
Information2.DisplayName = "Invoice Date"
Information2.InternalName = "InvoiceDate"
Information2.Type = FieldType.DateTime
Dim invoicedate as new datetime(2013,2,28,0,0,0)
DIm format as string = "MM/dd/yyyy"
Information2.Value = invoicedate.tostring(format)

Dim pathofsubfolder as string = "http://[SharepointServer]/2013/02/28/"
Dim filename as string = "Invoice_mycode_20130228.pdf"
Dim destinationurl() as string = {pathofsubfolder & filename}

'convert image produce by ABBYY into BLOB/byte

DIm localFilename as string = "..\mytemp\Invoice_mycode_20130228.pdf"
Dim fs as system.io.filestream
fs = system.io.file.openread(localfilename)
Dim content(fs.length) as byte
fs.read(content,0,system.convert.toint32(fs.length))
fs.close

Dim fileinfoArray() as FieldInformation = {contenttype, information1,information2}

Dim mycopyresult1 as new wscopy.wscopy.copyresult()
Dim mycopyresult2 as new wscopy.wscopy.copyresult()
Dim mycopyresultarray as wscopy.wscopy.copyresult() = {mycopyresult1, mycopyresult2}

Dim result as UInteger = mycopy.copyIntoItems(filename, destinationurl, fileinfoarray, content, mycopyresultarray)

and the after export script used for deleting the files in local after export.


Hope can be helpful.

Andrey Zyuzin posted this 17 February 2014

Thank you, I caught your point

richfish posted this 18 October 2018

Just in case anyone comes across this when searching the forum, this will create year and month folders and upload the image, no columns filled in though

Add Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll to your .net references.

Create a global export script:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;

using Microsoft.SharePoint.Client;

using System.Security;

using System.IO;

 

public class Sharepoint

{

    //this method saves the invoice image to the correct folder

    public static void SaveFileToSharePoint(string fileName, byte[] filebytes)

        {

            using (var context = new ClientContext("https://whatever.sharepoint.com/teams/testflexicapture/"))

            {

                //get date fields that we will use to understand which folder to export document image to

                DateTime now = DateTime.Now;

                string month = DateTime.Now.ToString("MMMM");

                string year = now.Year.ToString();

 

                //setup the connection to the sharepoint

                var passWord = new SecureString();

                foreach (var c in "yourpassword") passWord.AppendChar(c);

                context.Credentials = new SharePointOnlineCredentials("youremail", passWord);

                var web = context.Web;

                

                //pull in the file bytes and file name from parameters                

                var newFile = new FileCreationInformation { Content = filebytes, Url = fileName };

                //set the file to be overwritten if it exists which it shouldn't due the GUID based naming convention

                newFile.Overwrite = true;

                //navigate to the documents folder of the library

                var docs = web.Lists.GetByTitle("Documents");

                //navigate to the year sub folder

                var docsFolder = docs.RootFolder.Folders.GetByUrl(year);

                //write the file into the month subfolder

                docsFolder.Folders.GetByUrl(month).Files.Add(newFile);

                context.ExecuteQuery();

                

            }

        }

    

    //this method creates the folders and subfolders in sharepoint

    public static void CreateFolder()

        {

            using (var context = new ClientContext("https://whatever.sharepoint.com/teams/testflexicapture/"))

            {

                //get date fields that we will use to understand which folders to create

                DateTime now = DateTime.Now;

                string month = DateTime.Now.ToString("MMMM");

                string year = now.Year.ToString();

 

                //setup the connection to the sharepoint

                var passWord = new SecureString();

                foreach (var c in "yourpassword") passWord.AppendChar(c);

                context.Credentials = new SharePointOnlineCredentials("yourusername", passWord);

                var web = context.Web;

                

                //navigate to the documents folder of the library

                var query = context.LoadQuery(web.Lists.Where(p => p.Title == "Documents"));

                context.ExecuteQuery();

                List list = query.FirstOrDefault();

                var folders = list.RootFolder.Folders;

                context.Load(folders);

                context.ExecuteQuery();

                //add the year folder

                var newFolder = folders.Add(year);

                //add the month folder

                newFolder.Folders.Add(month);

                context.ExecuteQuery();

 

              }

 

        }

 

}

Then use export script:

using System;

using System.Text.RegularExpressions;

using System.IO;

 

//encoding document to base 64 code

IExportImageSavingOptions options = FCTools.NewImageSavingOptions();

options.Format = "pdf-a-s";

options.UseMRC = true;

options.ColorType = "BlackAndWhite";

options.Resolution = 300;

options.AddProperFileExt = true;

options.ShouldOverwrite = true;

 

byte[] imageBytes = Document.SaveAsStream(options);

 

//first create the current year and month folders

Sharepoint.CreateFolder();

 

//then upload the documents using GUID as filename

var guid = Guid.NewGuid().ToString();

Sharepoint.SaveFileToSharePoint(guid + ".pdf",imageBytes);

Close