Welcome to the Smartsheet Forum Archives
The posts in this forum are no longer monitored for accuracy and their content may no longer be current. If there's a discussion here that interests you and you'd like to find (or create) a more current version, please Visit the Current Forums.
External form and API 2.0 to insert a row in C#
I am attempting to use an external form (on our website written in C#) and the API 2.0 to insert a row into a sheet. I am reading the API 2.0 Documentation (at http://smartsheet-platform.github.io/api-docs/?csharp#add-row(s)) but cannot figure out where I place the actual values submitted from the form within this code to be added as a new row. Any help would be welcome!
The API 1.x code went something like this:
string smartsheetAPIToken = ConfigurationManager.AppSettings["SWTCSmartsheetToken"];
long sheetID = 5551212345678910; //Smartsheet ID
Token token = new Token();
token.AccessToken = smartsheetAPIToken;
SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();
List<Column> cols = new List<Column>(smartsheet.Sheets().Columns().ListColumns(sheetID));
Cell cell1 = new Cell();
cell1.ColumnId = cols[0].ID;
cell1.Value = DateTime.Now; //Date Time added
Cell cell2 = new Cell();
cell2.ColumnId = cols[1].ID;
cell2.Value = firstName.Text; //First Name
Cell cell3 = new Cell();
cell3.ColumnId = cols[2].ID;
cell3.Value = lastName.Text; //Last Name
Cell cell4 = new Cell();
cell4.ColumnId = cols[3].ID;
cell4.Value = email.Text; //Email
List<Cell> cells = new List<Cell>();
cells.Add(cell1);
cells.Add(cell2);
cells.Add(cell3);
cells.Add(cell4);
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);
Comments
-
Are there examples of how an external form might use the API (2.0) to add a row to a sheet?
-
Hi Michael,
Regarding how column (cell) values are specified for the new Row -- you do this by creating a collection of cells (referred to as"cellsA" in the sample code in the API documentation: http://smartsheet-platform.github.io/api-docs/?csharp#add-row(s)). When creating that collection, you specify "new Cell.AddCellBuilder(column_id, column_value).Build()" for each cell you want to populate within the row. For example, based upon the value mappings shown in the .CS file you attached previously -- your new code would look something like this:// Specify cell values for first row.Cell[] cellsA = new Cell[] {new Cell.AddCellBuilder(cols[0].ID, DateTime.Now).Build(),new Cell.AddCellBuilder(cols[1].ID, firstName.Text).Build(),new Cell.AddCellBuilder(cols[2].ID, lastName.Text).Build(),new Cell.AddCellBuilder(cols[3].ID, maidenName.Text).Build(),new Cell.AddCellBuilder(cols[4].ID, email.Text).Build(),new Cell.AddCellBuilder(cols[5].ID, address.Text).Build(),new Cell.AddCellBuilder(cols[6].ID, city.Text).Build(),new Cell.AddCellBuilder(cols[7].ID, StateDropDown.StateName).Build(),new Cell.AddCellBuilder(cols[8].ID, zip.Text).Build(),new Cell.AddCellBuilder(cols[9].ID, phone.Text).Build(),.....[continue adding "Cell.AddCellBuilder(column_id, column_value).Build()" until each cell is represented in this collection].....};"// Specify contents of first row.
Row rowA = new Row.AddRowBuilder(true, null, null, null, null).SetCells(cellsA).Build();// Add row to sheet.
smartsheet.SheetResources.RowResources.AddRows(sheetId, new Row[] {rowA});(In the API 1.1 code you provided, the example code I've included above would effectively replace all code starting with the line that reads "Cell cell1 = new Cell();" and continuing through the end of that file.)
Categories
- All Categories
- 14 Welcome to the Community
- Smartsheet Customer Resources
- 64.1K Get Help
- 414 Global Discussions
- 221 Industry Talk
- 461 Announcements
- 4.8K Ideas & Feature Requests
- 143 Brandfolder
- 141 Just for fun
- 58 Community Job Board
- 462 Show & Tell
- 32 Member Spotlight
- 1 SmartStories
- 299 Events
- 38 Webinars
- 7.3K Forum Archives