Recreating default display, edit and new forms of a list in SharePoint 2010 using Powershell

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)
# # ===================

Comments

  • Anonymous
    January 01, 2003
    Thanks Stephane for pointing out the mistake...

  • Anonymous
    January 01, 2003
    this is not working for me..

  • Anonymous
    January 01, 2003
    Recreating default display, edit and new forms of a list in SharePoint 2010 using Powershell
    thank you

  • Anonymous
    September 10, 2013
    what does $files reference? i tried to run your script but it's throwing an error because $files is not defined.  Thanks.

  • Anonymous
    September 12, 2013
    Getting errors due to a null reference for $files.  Doesn't that variable need to be initialized?

  • Anonymous
    November 06, 2013
    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

  • Anonymous
    January 22, 2014
    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 = $newformurlI am getting this error :unable to find SPForm matching URL .....upload.aspx at abs.ps1+ $list.DefaultNewFormUrl = $newformurlCan you please suggest ?

  • Anonymous
    January 22, 2014
    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 = $newformurlI am getting this error :unable to find SPForm matching URL .....upload.aspx at abs.ps1+ $list.DefaultNewFormUrl = $newformurlCan you please suggest ?

  • Anonymous
    March 12, 2014
    Hi there

    Can this be used on a SharePoint Document Library?

  • Anonymous
    April 17, 2014
    @Bradley : I havent tried it in DL but I believe it should work there as well

  • Anonymous
    April 17, 2014
    @Bradley : I havent tried it in DL but I believe it should work there as well

  • Anonymous
    May 15, 2014
    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.

  • Anonymous
    December 01, 2014
    May I ask why you are doing this in a loop?
    I removed the loop sections and it worked like a charm!

  • Anonymous
    December 15, 2014
    thanks for the work.This is helpful.

  • Anonymous
    May 01, 2015
    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.
    =======

  • Anonymous
    July 23, 2015
    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)

  • Anonymous
    November 08, 2016
    This script works great for me.. Thanks :)

  • Anonymous
    January 06, 2017
    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)