Fauzi88
Fauzi88

Reputation: 713

How to send file attachment using smartsheet api sdk

I want to send file attachment from fileupload into smartsheet. I was using sdk, I found a sample code for attachment. This my code for attachment:

if (fileUpload.HasFile)
    {
        string fileName = fileUpload.PostedFile.FileName;
        string sourceFile = Server.MapPath("~/")+fileName;
        fileUpload.PostedFile.SaveAs(sourceFile);
        string type = fileUpload.PostedFile.ContentType;
        smartsheet.Sheets().Attachments().AttachFile(sheetId, sourceFile, type);

    }

I read about AttachFile() method, must using ObjectId, but I do not understand how to get ObjectId, so I use sheetId.

Edit: That code was correct, when I running my code, I found file attachment in tab attachment on my sheet, but I want to attach that file into new row I added.

Can you help me to solve this? I still new to use smartsheet, and still learning how to use smartsheet api sdk c#, but I not found many example or sample code to use smartsheet api.

Here my full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Smartsheet.Api;
using Smartsheet.Api.Models;
using Smartsheet.Api.OAuth;

namespace smartsheet
{
    public partial class TestInput : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string strToken = "649qjpt7pmq8i0xze5550hr12x";
                long sheetId = 4204618734430084;

                Token token = new Token();
                token.AccessToken = strToken;

                SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();
                Smartsheet.Api.Models.Home home = smartsheet.Home().GetHome(new ObjectInclusion[] { ObjectInclusion.TEMPLATES });
                List<Column> cols = new List<Column>(smartsheet.Sheets().Columns().ListColumns(sheetId));

                Cell cell1 = new Cell();
                cell1.ColumnId = cols[0].ID;
                cell1.Value = txFirstname.Text;
                Cell cell2 = new Cell();
                cell2.ColumnId = cols[1].ID;
                cell2.Value = txLastname.Text;

                List<Cell> cells = new List<Cell>();
                cells.Add(cell1);
                cells.Add(cell2);

                Row row = new Row();
                row.Cells=cells;

                List<Row> rows = new List<Row>();
                rows.Add(row);

                RowWrapper rowWrapper = new RowWrapper.InsertRowsBuilder().SetRows(rows).SetToBottom(true).Build();

                smartsheet.Sheets().Rows().InsertRows(sheetId, rowWrapper);

                if (fileUpload.HasFile)
                {
                    string fileName = fileUpload.PostedFile.FileName;
                    string sourceFile = Server.MapPath("~/")+fileName;
                    fileUpload.PostedFile.SaveAs(sourceFile);
                    string type = fileUpload.PostedFile.ContentType;
                    smartsheet.Sheets().Attachments().AttachFile(sheetId, sourceFile, type);

                }

            }
            catch (Exception ex)
            {
                LableMsg.Text = ex.Message.ToString();
            }
        }
    }
}

Upvotes: 4

Views: 1575

Answers (1)

kyanskeem
kyanskeem

Reputation: 106

I'm not a C# developer, but I am very familiar with Smartsheet's Java SDK, which C# SDK was modeled after, so there may be some slight syntax problems with the answer below, but it should give you the gist of things.

You are almost there - to attach a file to a new row, you'll just need to get the id of the new row you just created and then attach to it instead of the sheet. Change your line:

smartsheet.Sheets().Rows().InsertRows(sheetId, rowWrapper);

to

IList<Row> insertedRow = smartsheet.Sheets().Rows().InsertRows(sheetId, rowWrapper);
long newRowId = insertedRow.get(0).ID;

Now you can attach directly to that row:

smartsheet.Rows().Attachments().AttachFile(newRowId, sourceFile, type);

Upvotes: 2

Related Questions