Check if the currently opened item is a folder or a document in EditForm.aspx
-
08-01-2021 - |
Question
We have added a content editor webpart to EditForm.aspx
for document library. In the CEWP, there's a JS filed. We need to execute different code when a folder is getting edited (like renaming the folder) and when file metadata is edited.
How can we check if the currently opened item is a folder or a document in EditForm.aspx
? We can use the ID field in the URL and check using REST API or CSOM but I would like to avoid server calls for efficiency purpose.
La solution
Use out of the box object WPQ1FormCtx
or WPQ2FormCtx
. I have seen former in SharePoint Online and the later in SP2013 on prem. The property you are interested in is WPQ1FormCtx.ItemAttributes.FsObjType
. If it is 1, the the object being edited is Folder. Attaching the screenshot from my SharePoint tenant.
You can also refere to this link. Here I had used WPQ2FormCtx
in SP 2013
Autres conseils
This is an alternative approach to Nadeem's answer.
When SharePoint opens the EditForm.aspx
it sends the ContentTypeId
in the URL parameters. From this article I was able to find out that ContentTypeId
for folder starts with 0x0120
. With that in hand I was able to check and code according to folder or a document.