//add attachments samples public static void AddAttachmentsSamples() { using (Connection K2Conn = new Connection()) { //dummy values for process instance ID and activity instance ID int processInstanceId = 1; int activityInstanceDestinationID = 10; string fileName = "Report1.pdf"; //Add an Attachment using the Connection class and the Process Instance's ID IWorkflowAttachment _attachment = K2Conn.AddAttachment(processInstanceId, fileName, GetFile("Report1.pdf")); //Add an Attachment using the Connection class and the Worklist Item's SerialNumber IWorkflowAttachment _attachment1 = K2Conn.AddAttachment("[_serialNo]", fileName, GetFile("Report1.pdf")); //Add an Attachment using the Connection class and the Process Instance's ID and Activity Instance Destination's ID IWorkflowAttachment _attachment2 = K2Conn.AddAttachment(processInstanceId, activityInstanceDestinationID, fileName, GetFile("Report1.pdf")); //Add an Attachment using the Process Instance ProcessInstance _processInstance = K2Conn.OpenProcessInstance(processInstanceId); IWorkflowAttachment _attachment3 = _processInstance.AddAttachment(fileName, GetFile("Report1.pdf")); //Add an Attachment using the WorklistItem WorklistItem _worklistItem = K2Conn.OpenWorklistItem("[_serialNo]"); IWorkflowAttachment _attachment4 = _worklistItem.AddAttachment(fileName, GetFile("Report1.pdf")); //Async exampe, where you create the attachment first and then upload the file //Add an 'empty' attachment IWorkflowAttachment _attachment5 = K2Conn.AddAttachment(processInstanceId, fileName, null); //now upload the attachment's file. //Note. You can only upload the file once and only for an 'empty' attachment. //Note. This can be used for async purposes, to create the metadata first and secondly upload the file. IAttachment attachmentWithContent = K2Conn.UploadAttachmentContent(_attachment.Id, GetFile("Report1.pdf")); } }
//retrieve attachments samples public static void GetAttachmentsSamples() { using (Connection K2Conn = new Connection()) { //dummy values for process instance ID and activity instance ID int processInstanceId = 1; int activityInstanceDestinationID = 10; //Get all the Attachments using the Process Instance's ID //By default this call will always return the attachment's files. IEnumerable <IWorkflowAttachment> attachments = K2Conn.GetAttachments(processInstanceId); foreach (IWorkflowAttachment attachment1 in attachments) { //retrieve the attachment file into an IO stream System.IO.Stream fileContents = attachment1.GetFile(); } //Get all the Attachments using the SerialNumber //By default this call will always return the attachment's files. IEnumerable <IWorkflowAttachment> attachments2 = K2Conn.GetAttachments("[_serialNo]"); //Get all the Attachments using the Process Instance's ID and Activity Instance Destination's ID //By default this call will always return the attachment's files. IEnumerable <IWorkflowAttachment> attachments3 = K2Conn.GetAttachments(processInstanceId, activityInstanceDestinationID); //Get all the Attachments from the Process Instance's 'Attachments' property ProcessInstance _processInstance = K2Conn.OpenProcessInstance(processInstanceId); IEnumerable <IWorkflowAttachment> procInstAttachments = _processInstance.Attachments; //Get all the Attachments from the Worklist Item's 'Attachments' property WorklistItem _worklistItem = K2Conn.OpenWorklistItem("[_serialNo]"); IEnumerable <IWorkflowAttachment> WLItemAttachments = _worklistItem.Attachments; //Get the Attachment by passing the Attachment's ID //By default this call will always return the attachment's file. int attachmentId = 1; IWorkflowAttachment attachment = K2Conn.GetAttachment(attachmentId); //save the attachment. SaveFile(attachment.FileName, attachment.GetFile()); //Get the Attachment by passing the Attachment's ID //pass 'false' for the includeFile parameter to only load the file on demand. IWorkflowAttachment attachmentNoFile = K2Conn.GetAttachment(attachmentId, false); //Get all the Attachments using the Process Instance's ID and Activity Instance Destination's ID //pass 'false' for the includeFile parameter to only load the file on demand. IEnumerable <IWorkflowAttachment> attachments4 = K2Conn.GetAttachments(processInstanceId, activityInstanceDestinationID, false); //Iterate through the attachments foreach (IAttachment attachment4 in attachments4) { //only get specific user's attachments if (attachment4.FQN.Equals(@"K2:Denallix\Bob")) { //use the attachment.GetFile() method to load the attachment's file and cache it. using (System.IO.Stream downloadStream = attachment.GetFile()) { SaveFile(attachment.FileName, downloadStream); } } } } }