简体   繁体   中英

How can I create a new form for a SharePoint list in PowerShell?

I've discovered the hard way that there's no easy way to just upload a file to a list's SPFolder and make it work as a form . (If there is, please correct me, because the guy trying this method seems to have failed as I did... the files uploaded but were never recognized as forms.)

Is there an easier way to create a new form, and add web parts to it, from scratch? I've tried sending over an empty-ish form file and even a blank text file, and after presumably adding Web Parts to both of them, they generate the same empty shell.... in theory, this should have items, but it's displaying nothing, and completely ignoring my attempts to inject items into the form after it's been "added".

The "dummy form" attempt:

Write-Host > DoubleDummy.aspx
$file = Get-ChildItem -LiteralPath "DoubleDummy.aspx"
Add-SPFormFile $list.rootFolder $file $newname

The result (it literally displays nothing in a browser):

<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage..."  %>
<%@ Register Tagprefix="SharePoint"  ... %>
...
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<SharePoint:UIVersionedContent UIVersion="4" runat="server">
    <ContentTemplate>
    <div style="padding-left:5px">
    </ContentTemplate>
</SharePoint:UIVersionedContent>
    <table cellpadding="0" cellspacing="0" id="onetIDListForm" style="width:100%">
     <tr>
     <td>
     <WebPartPages:WebPartZone runat="server" FrameType="None" ID="Main" Title="loc:Main"><ZoneTemplate>
        <WebPartPages:DataFormWebPart... PageType="PAGE_EDITFORM" FrameState="Normal" ...>
....

For a form on list $list and website $web, the following code works. There are a lot of overrides for Add() none of which will take both a default form type and a boolean to allow an override, so there's a "conditional" delete I wrote into this.

$rootFolder = $list.RootFolder
$part0 = $web.ServerRelativeUrl
$part1 = $rootFolder.URL
$newFormUrl = "${part0}/${part1}/${newname}"

# Delete file first.
try {
    $list.RootFolder.Files.Delete($list.RootFolder.Files[$newFormUrl].url)
} catch {
    # It's optional.
    Write-Host "Delete failed."
}
$done = $list.RootFolder.Files.Add($newFormUrl,
        [Microsoft.SharePoint.SPTemplateFileType]::FormPage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM