Wednesday 11 March 2015

Powershell for adding Custom list form of a list

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$Url = "http://sp2013:5559" #Please remove trailing slash if any

$listname ="TestList"
$web = get-spweb $Url
$list = $web.lists[$listname]
$files = $list.rootfolder.files






$editformurl = $list.RootFolder.ServerRelativeUrl + "/CustEditForm.aspx"
$dispformurl = $list.RootFolder.ServerRelativeUrl + "/CustDispForm.aspx"
$newformurl = $list.RootFolder.ServerRelativeUrl + "/CustNewForm.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;


$lfw1.Hidden ="True"
$lfw2.Hidden ="True"
$lfw3.Hidden ="True"
$lfw1.ChromeType ="None"
$lfw2.ChromeType ="None"
$lfw3.ChromeType ="None"

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



$SiteURL =$Url
#---------------Test Page----------------------------
$PageName="Rooms/CustNewForm.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName}
 



$PageName="TestList/CustDispForm.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName}
 


$PageName="TestList/CustEditForm.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName}
 

No comments:

Post a Comment