Skip to content

Instantly share code, notes, and snippets.

@juntalis
Created September 23, 2016 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juntalis/4133b57bd774f5b0ceb06024d6bdb690 to your computer and use it in GitHub Desktop.
Save juntalis/4133b57bd774f5b0ceb06024d6bdb690 to your computer and use it in GitHub Desktop.
/** Script configuration goes here */
var LIST_TITLE = 'YOUR LIST TITLE HERE';
var FIELD_NAME = 'YOUR FIELD NAME HERE';
/** Actual logic used to list the field names. */
(function (listTitle, readOnlyField) {
// First grab our client context.
var oCtx = SP.ClientContext.get_current(),
// Cache stuff to prevent needlessly recomputing stuff.
oList = oCtx.get_web().get_lists().getByTitle(listTitle),
oField = oList.get_fields().getByInternalNameOrTitle(readOnlyField);
// Entrypoint
deleteReadOnlyField(oField, function () {
alert('Field successfully removed!');
});
/// #region Actual Logic
function deleteReadOnlyField(oField, onSuccess) {
// Set the "ReadOnly" property to false
oField.set_readOnlyField(false);
// Register the changes.
oField.update()
oCtx.load(oField);
// Execute our query. If the property change made to our field was
// successful, we can delete the field and finish up.
executeQuery(this, function() {
oField.deleteObject();
executeQuery(this, onSuccess, 'DeleteField');
}, 'SetReadOnly=False');
}
/// #endregion
/// #region Utility Functions
function isDefined(subject) {
// Self-explanatory
return typeof(subject[member]) !== 'undefined';
}
function hasMember(subject, member) {
// subject has a specific member (value of member can be null, just
// so long as it exists)
return subject &&
Object.prototype.hasOwnProperty.call(subject, member) ||
isDefined(subject[member])
}
/** Show a generic error message */
function showError(description, args) {
// Show error message prefixed with the "description" parameter.
alert('Error occurred ' + prefix + ': '
+ args.get_message() + '\n'
+ args.get_stackTrace()); // lazy
}
/** Have the context execute all pending operations. */
function executeQuery(self, onSuccess, onError) {
// If onError is a string value, generate a call to showError, using
// the string for its description field.
if (typeof(onError) === 'string' || (onError instanceof String)) {
var description = onError;
onError = function (sender, args) {
showError.call(this, description, args);
};
}
return oCtx.executeQueryAsync(
Function.createDelegate(self, onSuccess),
Function.createDelegate(self, onError || showError));
}
/// #endregion
})(LIST_TITLE, FIELD_NAME);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment