I want to send the file attachment from the upload file to smartsheet. I used sdk, I found sample code for the attachment. This is my embed code:
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 the AttachFile () method, should use ObjectId, but I don’t understand how to get ObjectId, so I use sheetId.
Edit:
This code was right when I run my code, I found the file attachment in the tabs attachment on my worksheet, but I want to add this file to the new line that I added.
Can you help me solve this problem? I'm still new to using smartsheet and still learning to use the smartsheet api sdk C #, but I have not found many examples or code examples for using smartsheet api.
Here is 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();
}
}
}
}