Actually, you can add a dummy field not present on the list by adding to the ctx.ListSchema.Field
collection in OnPreRender
. Then, when the actual rendering starts, SharePoint will just add another column on the view automatically. Then you can add something into that cell in OnPostRender
.
Here's an example of the code to add the field definition into the schema to make the dummy column:
function addTemporaryField(ctx) {
// make sure we haven't added the field already,
// in case OnPreRender fires twice, which it does sometimes
if (ctx.ListSchema.Field.filter(function (fld) {
return fld.Name == 'FakeField';
}).length == 0) {
// create the field schema object to insert
var FakeFieldObj = {
AllowGridEditing: 'FALSE',
DisplayName: 'Fake Field',
RealFieldName: 'FakeField',
Name: 'FakeField',
FieldType: 'Text',
Type: 'Text',
Filterable: 'FALSE',
Sortable: 'FALSE',
ReadOnly: 'TRUE',
};
// find the index of the field to insert next to,
// based on that field's DISPLAY name.
var insertIdx = ctx.ListSchema.Field.map(function (fld) {
return fld.DisplayName;
}).indexOf('Some Field Name');
// if you want to insert *before* the field do not add anything
// but, if you want to insert *after* the field, add 1
insertIdx++;
// insert your fake field schema object into the list schema
ctx.ListSchema.Field.splice(insertIdx, 0, FakeFieldObj);
}
}
I wrote a blog post about it where I also go into inserting data into the dummy column, and you could certainly render a button in there.
var context = SP.ClientContext.get_current();
but then I first need to load sp.js (via executeOrDelayUntilScriptLoaded), otherwise SP.ClientContext.get_current() is undefined. The issue is, if I delay (via executeOrDelayUntilScriptLoaded), it seems i'm "too late" in the sequence to still be able to add columns in