// Grabs a document from a Sharepoint list and turns it into a "provided document",
        // i.e. a document which is to provided to Signicat in a web service request
        // and used as the original in a signing process
        private provideddocument GetDocumentForSigningFromSPList(SharePointContext spContext, string SPListId, string SPListItemId)
        {
            // Create a provided document, prepared for signing with basic properties
            var document = new provideddocument
            {
                externalreference = SPListItemId,
                id            = "doc_1",
                mimetype      = "application/pdf",
                signtextentry = "I have read and understood the document which has been presented to me, and I agree to its contents with my signature."
            };

            // Now, let's get the name and data of the document from the Sharepoint list
            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    // Get the list item representing the document
                    List     list     = clientContext.Web.Lists.GetById(Guid.Parse(SPListId));
                    ListItem listItem = list.GetItemById(SPListItemId);
                    clientContext.Load(listItem, li => li.DisplayName, li => li.File);
                    clientContext.ExecuteQuery();

                    // Set the name of the document
                    document.description = listItem.DisplayName;

                    // Now get the binary contents
                    Microsoft.SharePoint.Client.File itemFile   = listItem.File;
                    ClientResult <Stream>            fileStream = listItem.File.OpenBinaryStream();
                    clientContext.Load(itemFile);
                    clientContext.ExecuteQuery();
                    using (var memoryStream = new MemoryStream())
                    {
                        fileStream.Value.CopyTo(memoryStream);
                        document.data = memoryStream.ToArray();
                    }
                }
            }
            return(document);
        }
        // Creates a "document order" request, i.e. a mapping to the object model provided
        // by the DocumentService, a Signicat web service for administering signature processes.
        private createrequestrequest GetRequest(provideddocument document, string method, string spListId, string spSource, string spListUrlDir)
        {
            string queryString =
                "?rid=${requestId}&" +
                string.Format("SPListId={0}&documentName={1}&SPSource={2}&SPListURLDir={3}",
                Url.Encode(spListId), Url.Encode(document.description), Url.Encode(spSource), Url.Encode(spListUrlDir));

            var request = new createrequestrequest
            {
                password = PASSWORD,
                service = SERVICE,
                request = new request[]
                {
                    new request
                    {
                        profile = "slim",
                        language = "en",
                        document = new document[]
                        {
                            document
                        },
                        task = new task[]
                        {
                            new task
                            {
                                bundle = false,
                                bundleSpecified = true,
                                id = "task_1",
                                ontaskcomplete = Url.Action("Return", "Home", null, Request.Url.Scheme) + queryString,
                                ontaskcancel = Url.Action("Return", "Home", null, Request.Url.Scheme) + queryString,
                                ontaskpostpone = Url.Action("Return", "Home", null, Request.Url.Scheme) + queryString,
                                documentaction = new documentaction[]
                                {
                                    new documentaction
                                    {
                                        type = documentactiontype.sign,
                                        documentref = document.id,
                                        sendresulttoarchive = true,
                                        sendresulttoarchiveSpecified = true
                                    }
                                },
                                signature = new signature[]
                                {
                                    new signature
                                    {
                                        method = new string[]
                                        {
                                            method
                                        }
                                    }
                                },
                                authentication = new authentication { artifact = true, artifactSpecified = true }
                            }
                        }
                    }
                }
            };
            return request;
        }
        // Grabs a document from a Sharepoint list and turns it into a "provided document",
        // i.e. a document which is to provided to Signicat in a web service request
        // and used as the original in a signing process
        private provideddocument GetDocumentForSigningFromSPList(SharePointContext spContext, string SPListId, string SPListItemId)
        {
            // Create a provided document, prepared for signing with basic properties
            var document = new provideddocument
            {
                externalreference = SPListItemId,
                id = "doc_1",
                mimetype = "application/pdf",
                signtextentry = "I have read and understood the document which has been presented to me, and I agree to its contents with my signature."
            };

            // Now, let's get the name and data of the document from the Sharepoint list
            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    // Get the list item representing the document
                    List list = clientContext.Web.Lists.GetById(Guid.Parse(SPListId));
                    ListItem listItem = list.GetItemById(SPListItemId);
                    clientContext.Load(listItem, li => li.DisplayName, li => li.File);
                    clientContext.ExecuteQuery();

                    // Set the name of the document
                    document.description = listItem.DisplayName;

                    // Now get the binary contents
                    Microsoft.SharePoint.Client.File itemFile = listItem.File;
                    ClientResult<Stream> fileStream = listItem.File.OpenBinaryStream();
                    clientContext.Load(itemFile);
                    clientContext.ExecuteQuery();
                    using (var memoryStream = new MemoryStream())
                    {
                        fileStream.Value.CopyTo(memoryStream);
                        document.data = memoryStream.ToArray();
                    }
                }
            }
            return document;
        }
        // Creates a "document order" request, i.e. a mapping to the object model provided
        // by the DocumentService, a Signicat web service for administering signature processes.
        private createrequestrequest GetRequest(provideddocument document, string method, string spListId, string spSource, string spListUrlDir)
        {
            string queryString =
                "?rid=${requestId}&" +
                string.Format("SPListId={0}&documentName={1}&SPSource={2}&SPListURLDir={3}",
                              Url.Encode(spListId), Url.Encode(document.description), Url.Encode(spSource), Url.Encode(spListUrlDir));

            var request = new createrequestrequest
            {
                password = PASSWORD,
                service  = SERVICE,
                request  = new request[]
                {
                    new request
                    {
                        profile  = "slim",
                        language = "en",
                        document = new document[]
                        {
                            document
                        },
                        task = new task[]
                        {
                            new task
                            {
                                bundle          = false,
                                bundleSpecified = true,
                                id             = "task_1",
                                ontaskcomplete = Url.Action("Return", "Home", null, Request.Url.Scheme) + queryString,
                                ontaskcancel   = Url.Action("Return", "Home", null, Request.Url.Scheme) + queryString,
                                ontaskpostpone = Url.Action("Return", "Home", null, Request.Url.Scheme) + queryString,
                                documentaction = new documentaction[]
                                {
                                    new documentaction
                                    {
                                        type                         = documentactiontype.sign,
                                        documentref                  = document.id,
                                        sendresulttoarchive          = true,
                                        sendresulttoarchiveSpecified = true
                                    }
                                },
                                signature = new signature[]
                                {
                                    new signature
                                    {
                                        method = new string[]
                                        {
                                            method
                                        }
                                    }
                                },
                                authentication = new authentication {
                                    artifact = true, artifactSpecified = true
                                }
                            }
                        }
                    }
                }
            };

            return(request);
        }