Ad
Hide/Remove ContentType From A SP List With JavaScript
I am creating a SP List via REST API, and after the list has been created I'm adding a custom ContentType to that list. So, my custom ContentType should be a default CT on that List.
I've already tried this but it doesn't work, probably my contentType has to be an object..
let dfd = new $.Deferred();
let targetUrl = config.App + subsiteTitle;
let clientContext = new SP.ClientContext(targetUrl);
if (clientContext !== undefined && clientContext !== null) {
let web = clientContext.get_web();
let listCollection = web.get_lists();
let list = listCollection.getByTitle(listTitle);
let contentType = list.get_contentTypes().getById('0x01');
contentType.deleteObject();
clientContext.executeQueryAsync(
Function.createDelegate(this, function() {
dfd.resolve(this);
}),
Function.createDelegate(this, function(sender, args) {
console.log('Failed to remove content type ' + args.get_message());
})
)
}
return dfd.promise();
Does anyone know how to hide or remove 'Item' ContentType from that List with JavaScript, or to set my custom ContentType as default, doesn't matter if it is via REST API or JSOM? Thanks a lot!
Ad
Answer
In your example two actions are missing:
- adding content type into list,
SP.ContentTypeCollection.addExistingContentType
method could be utilized for that purpose - in order to set a specific content as a default one, in needs to be
specified as a first using
SP.Folder.uniqueContentTypeOrder
property
Example
let ctx = new SP.ClientContext(targetUrl);
let ct = ctx.get_site().get_rootWeb().get_contentTypes().getById(contentTypeId);
let targetList = ctx.get_web().get_lists().getByTitle(listTitle);
let listContentTypes = targetList.get_contentTypes();
listContentTypes.addExistingContentType(ct); //1. add content type into list
var folder = targetList.get_rootFolder();
ctx.load(folder, 'ContentTypeOrder');
ctx.load(ct);
executeQuery(ctx)
.then(() => {
//2. set added content type as a default one
let ctIdList = folder.get_contentTypeOrder().sort((x,y) =>{
return x.get_stringValue().indexOf(contentTypeId) != -1 ? -1 : y.get_stringValue().indexOf(contentTypeId) != -1 ? 1 : 0;
});
folder.set_uniqueContentTypeOrder(ctIdList);
folder.update();
return executeQuery(ctx);
})
.then(() => {
console.log("Done!");
})
where
function executeQuery(context) {
return new Promise((resolve, reject) => {
context.executeQueryAsync(function () {
resolve();
}, function (sender, args) {
reject(args);
});
});
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad