Please specify what version of the library you are using: [ @pnp npm package 1.2.6 ]
Please specify what version(s) of SharePoint you are targeting: [ SharePoint Online ]
In the world of JSOM we had an ability to set the UniqueContentTypeOrder of a root folder in list/library. Something like below code
var rootFolder = list.get_rootFolder();
rootFolder.set_uniqueContentTypeOrder(newCTO);
rootFolder.update();
list.update();
I am developing SPFx web parts, where while creating a new library/list I would like to also set the Content Type Order. I have observed that list.rootFolder.uniqueContentTypeOrder is GET only property. Can we have a method which allows us to actually set the unique order in future? is it possible or is there and I am not aware? Something like random sample below.
sp.web.lists.getByTitle("Some").rootFolder.uniqueContentTypeOrder.set("0011","0111")
Note: I went through the source of folder.ts but I could not find a method which can set the UniqueContentTypeOrder. Also the update method consists TypedHash of string, number and bool only.
@gautamdsheth the sp package pro :)
Any thoughts?
This is already possible:
interface OrderData {
ContentTypeOrder: { StringValue: string }[];
UniqueContentTypeOrder?: { StringValue: string }[];
}
const folder = sp.web.lists.getById("60F3927A-A763-4473-B2BE-10ACEE3A799A").rootFolder;
// here you need to see if there are unique content type orders already, or just the normal one
const existingOrders = await folder.select("ContentTypeOrder", "UniqueContentTypeOrder").get<OrderData>();
const activeOrder = existingOrders.UniqueContentTypeOrder ? existingOrders.UniqueContentTypeOrder : existingOrders.ContentTypeOrder;
// manipulate the order here however you want (I am just reversing the array as an example)
const newOrder = activeOrder.reverse();
// update the content type order thusly:
await folder.update({
UniqueContentTypeOrder: {
__metadata: { type: "Collection(SP.ContentTypeId)" },
results: newOrder,
},
});
I will add this to the docs page for content types. Right now you will have to type the update object as any due to the typing on the argument there. I am removing that restriction when I update the doc.
Many thanks @patrick-rodgers for sharing the code. Sorry for my ignorance, But I need to get clear on few things :)
I tried your code, but awaitis giving an error expression is only allowed within an async function.
Since you have updated the code in your Repository, this means in the next release of the package we will get this change? (_Change in the Typing argument_)
Now if I want to apply this code without waiting/getting the next release package. I will have to override the update method of the Folder, right? If so how? (_Because with current package folder.update one will get error of Type_)
Best Regards
Asad
@asadrefai - Patrick used the async/await pattern. But if you are not using that, then you can use the then pattern of promises.
Modifying from @patrick-rodgers 's code, you can use it as mentioned below in the existing package :
interface OrderData {
ContentTypeOrder: { StringValue: string }[];
UniqueContentTypeOrder?: { StringValue: string }[];
}
const folder = sp.web.lists.getByTitle("Demo List").rootFolder;
// here you need to see if there are unique content type orders already, or just the normal one
const existingOrders = folder.select("ContentTypeOrder", "UniqueContentTypeOrder").get().then((d: OrderData) => {
const activeOrder = d.UniqueContentTypeOrder ? d.UniqueContentTypeOrder : d.ContentTypeOrder;
// manipulate the order here however you want (I am just reversing the array as an example)
const newOrder = activeOrder.reverse();
var payLoad = {
__metadata: { type: "Collection(SP.ContentTypeId)" },
results: newOrder,
};
// update the content type order thusly:
folder.update({
UniqueContentTypeOrder : payLoad as any
}).then((d:FolderUpdateResult) =>{
console.log(d);
}).catch(d =>{
console.log(d);
});
});
@gautamdsheth This is perfect!! Thanks for sharing.
Most helpful comment
This is already possible:
I will add this to the docs page for content types. Right now you will have to type the update object as any due to the typing on the argument there. I am removing that restriction when I update the doc.