Attach files to a SmartObject from aspx web forms

  • 19 September 2007
  • 0 replies
  • 5 views

Badge +6

Is this method of uploading documents to a SmartObject going to break when I post the file to clustered environment?  In other words, when the path to the file is passed to the SmartObject does that path need to be accurate on the webserver or on the K2 server? - on my VPC they are the same...


Nonetheless, here is how you do it.

First, you'll need a SmartBox based SmartObject that is setup with a property field of type "File".  (see screenshot at bottom)

Then you'll need an aspx form with a "FileUpload" control and a submit button.

The codebehind will look something like this - although there is no exception handling here for simplicity:


//remember to add a reference for Sourcecode.Data.SmartObjectsClient and a Using SourceCode…


//event code


protected void btnSubmit_Click(object sender, EventArgs e)
{
   If  (FileUpload1.HasFile)
   {
   //


   //set strFilePath = “c:inetpubwwwrootattachmentsyourfile.txt”
   string strFilePath = “c:inetpubwwwrootattachments” + FileUpload1.FileName;


   //literally save the save the file locally on the web/k2 server so that it can be consumed by the SmartObject
   FileUpload1.PostedFile.SaveAs(strFilePath);


   //run the attachmentcreate() method to create a smartobject record and insert this file…
   AttachmentsCreate(strFilePath);


   //be neat and tidy and get rid of the extra file
   System.IO.File.Delete(strFilePath);
   }
                
//method that will do the SmartObject call

   private void AttachmentsCreate(string m_strFileTempStorage)
        //SmartObject Attachments.Create
        {
            //define SmartObject connection string
            string m_strSOConnAttachments = "Server=blackpearl;Port=5555;AutoAlias=False;SmartObjects=Attachments";
           
            //define the SmartObject method to be called
            string strSelectCmd = "Attachments.Create";


            //redefine the connection string information
            SourceCode.Data.SmartObjectsClient.SOConnection oConn = new SourceCode.Data.SmartObjectsClient.SOConnection(m_strSOConnAttachments);



            SourceCode.Data.SmartObjectsClient.SOCommand oCmd = new SourceCode.Data.SmartObjectsClient.SOCommand(strSelectCmd, oConn);



            SourceCode.Data.SmartObjectsClient.SODataReader oRdr = null;


            //define the command type
            oCmd.CommandType = CommandType.StoredProcedure;


            //m_strFileTempStorage
            SourceCode.Data.SmartObjectsClient.SOParameter oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@attFileTempStorage", m_strFileTempStorage);// m_strFileTempStorage);
            oCmd.Parameters.Add(oParam);


            ////Additional Parameters can be added to populate the other fields in the SmartObject
            ////DocumentName
            //oParam = new SourceCode.Data.SmartObjectsClient.SOParameter("@attDocumentName", FileUpload.FileName);
            //oCmd.Parameters.Add(oParam);


            // Write to Attachments Smobject - get ID back - alternatively could be run using "ExecuteNonQuery" instead of "ExcecuteReader"
            oRdr = oCmd.ExecuteReader(CommandBehavior.CloseConnection);



            //could be utilized if any Smobj's relating to reponses are created
            if (oRdr != null)
            {
                while (oRdr.Read())
                {
                    // note the names in the oRdr collection correspond to SmartObject properties
                    // i.e. oRdr["attID"] means there is a SmartObject property called "attID"
                    txtAttachmentID.Text = Convert.ToInt32(oRdr["attID"].ToString());
                 }
            }


            oRdr.Close();
    }


 


 


 


 


****************


Search terms added 10/25/07


attachments sharepoint attach file upload smartobject SmO SO


15892i162CF846699C74A1.png

0 replies

Be the first to reply!

Reply