Following script recreates dispform.aspx, editform.aspx and newform.aspx of a list using PowerShell, useful in scenarios where we need to recreate these forms to fix issues induced due to upgrades
$url = Read-Host "Enter URL"
do{
$listname = Read-Host "Enter List Name"
$web = get-spweb $url
$list = $web.lists[$listname]
$files = $list.rootfolder.files
#---------------------------- Delete Forms ----------------------
$form1 = $list.RootFolder.files | ?{$_.url -match "dispform.aspx"}
$form2 = $list.RootFolder.files | ?{$_.url -match "editform.aspx"}
$form3 = $list.RootFolder.files | ?{$_.url -match "newform.aspx"}
$form2.delete()
$form1.delete()
$form3.delete()
$list.update()
# --------------------------recreating --------------------------------
$editformurl = $list.RootFolder.ServerRelativeUrl + "/editform.aspx"
$dispformurl = $list.RootFolder.ServerRelativeUrl + "/Dispform.aspx"
$newformurl = $list.RootFolder.ServerRelativeUrl + "/NewForm.aspx"
$dispform = $files.add($dispformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage)
$editform = $files.add($editformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage)
$newform = $files.add($newformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage)
$wpm = $editform.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wpm2 = $dispform.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wpm3 = $newform.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$lfw1 = new-object ([Microsoft.SharePoint.WebPartPages.ListFormwebpart])
$lfw2 = new-object ([Microsoft.SharePoint.WebPartPages.ListFormwebpart])
$lfw3 = new-object ([Microsoft.SharePoint.WebPartPages.ListFormwebpart])
$ilist1 = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw1)
$ilist2 = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw2)
$ilist3 = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw3)
$ilist1.ListId = $list.id
$ilist1.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_EDITFORM;
$ilist2.ListId = $list.id
$ilist2.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_DISPLAYFORM;
$ilist3.ListId = $list.id
$ilist3.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_NEWFORM;
$wpm.AddWebPart($lfw1, "Main", 1)
$wpm2.AddWebPart($lfw2, "Main", 1)
$wpm3.AddWebPart($lfw3, "Main", 1)
$list.DefaultDisplayFormUrl = $dispformurl
$list.DefaultEditFormUrl = $editformurl
$list.DefaultNewFormUrl = $newformurl
$list.update()
}
while ($TRUE)
# # ===================
Thanks Stephane for pointing out the mistake…
this is not working for me..
Recreating default display, edit and new forms of a list in SharePoint 2010 using Powershell
thank you
what does $files reference? i tried to run your script but it's throwing an error because $files is not defined. Thanks.
Getting errors due to a null reference for $files. Doesn't that variable need to be initialized?
Thanks a lot for this time saving script. I was completely stuck with SharePoint Designer, and the script worked like a charm.
I had a small fix to do, as the previous comments stated: $files was not set.
So. declare $files as $list.RootFolder.files right after the $list declaration and it will work perfectly
Thanks for your code.
It was for edit,disp and new form. Does recreation for upload.aspx will be similar to NewForm??
using upload.aspx instead of newform.aspx.
$ilist3.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_NEWFORM;
$list.DefaultNewFormUrl = $newformurl
I am getting this error :
unable to find SPForm matching URL …..upload.aspx at abs.ps1
+ $list.DefaultNewFormUrl = $newformurl
Can you please suggest ?
Thanks for your code.
It was for edit,disp and new form. Does recreation for upload.aspx will be similar to NewForm??
using upload.aspx instead of newform.aspx.
$ilist3.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_NEWFORM;
$list.DefaultNewFormUrl = $newformurl
I am getting this error :
unable to find SPForm matching URL …..upload.aspx at abs.ps1
+ $list.DefaultNewFormUrl = $newformurl
Can you please suggest ?
Hi there
Can this be used on a SharePoint Document Library?
@Bradley : I havent tried it in DL but I believe it should work there as well
@Bradley : I havent tried it in DL but I believe it should work there as well
Hi, thanks for the post. It’s helpful. I am just wondering the reason for using IListWebPart, as I was able to by pass that and just set the ListID and PageType properties to the ListFormWebpart objects directly.
May I ask why you are doing this in a loop?
I removed the loop sections and it worked like a charm!
thanks for the work.This is helpful.
I’m fairly new to PowerShell. Do I need to do some form of #include to get the necessary functions?
Error:
=======
get-spweb : The term ‘get-spweb’ is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
=======
I want to upload new custom list from, but below code taking only server relative path only, is their any way to upload custom form from local drive.
#$dispform = $files.add($dispformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage)
This script works great for me.. Thanks 🙂
Thank you so much for taking the time to publish this. It would take a million years to figure this out by myself (years that were not included in the initial project estimates of course)
(Y)