Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Created August 31, 2021 22:04
Show Gist options
  • Save fluxdigital/6bfc695e00a3f3096d44b9e833c1f518 to your computer and use it in GitHub Desktop.
Save fluxdigital/6bfc695e00a3f3096d44b9e833c1f518 to your computer and use it in GitHub Desktop.
class AddTableRows : WebEdit
{
private string RowsToAdd { get; set; }
public override void Execute(CommandContext context)
{
Assert.ArgumentNotNull((object)context, nameof(context));
ItemUri queryString = ItemUri.ParseQueryString();
if (queryString != (ItemUri)null)
{
Item obj = Database.GetItem(queryString);
if (obj != null && !Sitecore.Web.WebEditUtil.CanDesignItem(obj))
{
SheerResponse.Alert("The action cannot be executed because of security restrictions.");
return;
}
}
NameValueCollection parameters = new NameValueCollection();
string id = context.Parameters["id"];
parameters["id"] = id;
if (string.IsNullOrEmpty(id)) //using button via exp editor so validate rendering
{
string parameter1 = context.Parameters["referenceId"];
Assert.IsNotNullOrEmpty(parameter1, "uniqueid must not be empty");
parameters["uniqueId"] = parameter1;
string parameter2 = context.Parameters["renderingId"];
Assert.IsNotNullOrEmpty(parameter2, "rendering item id must not be empty");
parameters["renderingItemId"] = parameter2;
}
else //using button via content editor so validate item clicked
{
Item tableItem = Client.ContentDatabase.GetItem(id);
Assert.IsTrue(tableItem.TemplateID == ID.Parse("{408F8C86-59EC-475E-8BF5-5C24D3A3D1F4}"), "This item is not a Table so Rows can not be inserted.");
}
Context.ClientPage.Start((object)this, "Run", parameters);
}
[UsedImplicitly]
protected void Run(ClientPipelineArgs args)
{
Assert.ArgumentNotNull((object)args, nameof(args));
if (!args.IsPostBack)
{
//ask how many rows (maximum of 50 at a time)
SheerResponse.Input("Enter the number of rows you would like (max 50):", "1", @"\b(0*([1-9]|[1-4][0-9]|50))\b", Translate.Text("'$Input' is not a valid number."), 50);
args.WaitForPostBack();
}
else if (args.HasResult) //user has entered rows or columns
{
if (string.IsNullOrEmpty(RowsToAdd)) //rows entered
{
//set default cols to add based on prev rows - if there are some
var colsToAdd = "1";
Item tableItem = Client.ContentDatabase.GetItem(args.Parameters["Id"]);
if (tableItem!=null && tableItem.HasChildren)
{
colsToAdd = tableItem?.Children[0]?.Children.Count.ToString();
}
//ask how many columns (maximum of 30 at a time)
RowsToAdd = args.Result;
SheerResponse.Input("Enter the number of columns you would like (max 30):", colsToAdd, @"\b(0*([1-9]|[12][0-9]|30))\b", Translate.Text("'$Input' is not a valid number."), 30);
args.WaitForPostBack();
}
else
{
Item tableItem = Client.ContentDatabase.GetItem(args.Parameters["Id"]);
AddRows(RowsToAdd, args.Result, tableItem);
RowsToAdd = string.Empty;
}
}
}
private static void AddRows(string rows, string columns, Item dataSourceItem)
{
int rowCount = dataSourceItem.Children.Count + 1;
int rowsToAdd = int.Parse(rows);
int columnsToAdd = int.Parse(columns);
var rowTemplateId = new TemplateID(new ID("{36257DFF-0E30-472C-B269-636569FAAF25}"));
var cellTemplateId = new TemplateID(new ID("{2FBBDAF7-6B2C-4A7E-B1A3-E270273EB603}"));
using (new Sitecore.SecurityModel.SecurityDisabler()){
//add rows (depending on how many rows we have already we may need to start this count from more than 1)
for (int i = 0; i < rowsToAdd; i++)
{
var row = dataSourceItem.Add($"Row {rowCount++}", rowTemplateId);
//set sort order
row.Editing.BeginEdit();
row.Fields[Sitecore.FieldIDs.Sortorder].Value = (rowCount-1).ToString();
row.Editing.EndEdit();
//add cells to rows
for (int c = 1; c <= columnsToAdd; c++)
{
var cell = row.Add($"Cell {c}", cellTemplateId);
//set sort order
cell.Editing.BeginEdit();
cell.Fields[Sitecore.FieldIDs.Sortorder].Value = c.ToString();
cell.Editing.EndEdit();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment