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 (18)

  1. Yash Goel says:

    Thanks Stephane for pointing out the mistake...

  2. Anonymous says:

    this is not working for me..

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

  4. Richard says:

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

  5. Garry Trinder says:

    Getting errors due to a null reference for $files.  Doesn't that variable need to be initialized?

  6. Stephane Bryois says:

    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

  7. 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 ?

  8. 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 ?

  9. Bradley says:

    Hi there

    Can this be used on a SharePoint Document Library?

  10. yash says:

    @Bradley : I havent tried it in DL but I believe it should work there as well

  11. yash says:

    @Bradley : I havent tried it in DL but I believe it should work there as well

  12. Conax says:

    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.

  13. Johan says:

    May I ask why you are doing this in a loop?
    I removed the loop sections and it worked like a charm!

  14. com says:

    thanks for the work.This is helpful.

  15. Leo Jacob says:

    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.
    =======

  16. RK says:

    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)

  17. Subhadip Datta says:

    This script works great for me.. Thanks 🙂

  18. GB says:

    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)

Skip to main content