Malin Åhlin
Malin Åhlin

Reputation: 3

Signing documents located in SharePoint with CoSign

I'm trying to use the CoSign API to get sign fields and sign a docx document located in SharePoint.

I can get the memorystream or byte array from the SharePoint file, using the byte array I've created a FileHandler object but it returns sign fields.

I've posted the helper methods I'm using below, The FileHandle returns an object but the SAPI.SignatureFieldEnumInitEx method returns 0 fields, checking the document with the SharePoint Add-in and the example code returns 2 fields. Problem seem to lie in the byteArray I'm sending in, the item.File.OpenBinary() method to open the document doesn't seem to be correct.

using (SPSite site = new SPSite("http://sharepointweburl"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList documents = web.Lists["Documents"];
            SPListItem item = documents.GetItemById(8);
            Console.WriteLine("Item: {0}", item.Name);
            var binaryFile = item.File.OpenBinary();

            var handle = DocuSign.GetFileHandleFromStream(binaryFile);
            var fields = DocuSign.GetSignatureFieldsFromHandle(handle);
        }
    }

public static FileHandle GetFileHandleFromStream(byte[] byteArray)
{
    FileHandle fileHandle = null;
    var sapiByteArray = new SAPIByteArray();
    sapiByteArray.FromArray(byteArray);
    SAPIContext ctxSigField = new SAPIContextClass();
    SAPI.CreateFileHandleByMem(out fileHandle, SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_OFFICE_XML_PACKAGE, 0, sapiByteArray);
    return fileHandle;
}

public static DOCXField[] GetSignatureFieldsFromHandle(FileHandle fileHandle)
{
    SAPIContext ctxSigField = new SAPIContextClass();
    int rc;
    int NumOfFields = 0;
    rc = SAPI.SignatureFieldEnumInitEx(
        hSession,
        ctxSigField,
        SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_OFFICE_XML_PACKAGE,
        "",
        fileHandle,
        0,
        ref NumOfFields
        );
    DOCXField[] fields = new DOCXField[NumOfFields];
    Console.WriteLine("Found {0} signature fields", NumOfFields);
    for (int i = 0; i < NumOfFields; i++)
    {
        fields[i] = new DOCXField();
        //Get Signature Field Handle
        rc = SAPI.SignatureFieldEnumCont(hSession, ctxSigField, out fields[i].hSigField);
        if (rc != 0) throw new Exception("Failed in SignatureFieldEnumCont (" + rc.ToString("X") + ")");

        //Get Signature Field Details
        rc = SAPI.SignatureFieldInfoGet(
            hSession,
            fields[i].hSigField,
            fields[i].sSettings,
            fields[i].sInfo);

        if (rc != 0) throw new Exception("Failed in SignatureFieldInfoGet (" + rc.ToString("X") + ")");
    }
    SAPI.ContextRelease(ctxSigField);
    return fields;
}

Upvotes: 0

Views: 65

Answers (1)

Alon
Alon

Reputation: 56

The CoSign API doesn't support using memory stream for DOCX (only PDF). The only option is to save the memory stream as a temporary file and then call SAPI methods with the file path.

Upvotes: 0

Related Questions