5

We are creating a powershell script to create lists, groups and to install wsp packages.

There are 5 wsp each containing 1 webpart. Is there any way via powershell to create 1 site page and embed 1 webpart on each page?

We have to create new sites pages for every deployment/testing, so feel that it would save lot of time if this was possible via powershell.

I am following link1 and link2 but looks like they are referring to publishing pages, I want to make webpart pages..

2
  • Do you want a yes or no answer? If so: Yes ;) Feb 27, 2014 at 11:02
  • Hi, I am looking for code for webpart page and them to embed webpart on it. We have worked with publishing pages. But not with webpart pages
    – variable
    Feb 27, 2014 at 11:44

3 Answers 3

6

Here is the PowerShell code to create a webpart page:

Add-PsSnapin Microsoft.SharePoint.PowerShell 

$pageTitle = "Page Name"
$layoutTemplate = 4  # Template code
$web = Get-SPWeb http://server/site  
$list = $web.GetList("http://server/site/LibraryName/")
$xml = "<?xml version=""1.0"" encoding=""UTF-8""?><Method ID=""0,NewWebPage""><SetList Scope=""Request"">" + $list.ID + "</SetList><SetVar Name=""Cmd"">NewWebPage</SetVar><SetVar Name=""ID"">New</SetVar><SetVar Name=""Type"">WebPartPage</SetVar><SetVar Name=""WebPartPageTemplate"">" + $layoutTemplate + "</SetVar><SetVar Name=""Overwrite"">true</SetVar><SetVar Name=""Title"">" + $pageTitle + "</SetVar></Method>"
$result = $web.ProcessBatchData($xml)
$web.Dispose()

# Possible LayoutTemplate values:
# 1 - Full Page, Vertical
# 2 - Header, Footer, 3 Columns
# 3 - Header, Left Column, Body
# 4 - Header, Right Column, Body
# 5 - Header, Footer, 2 Columns, 4 Rows
# 6 - Header, Footer, 4 Columns, Top Row
# 7 - Left Column, Header, Footer, Top Row, 3 Columns
# 8 - Right Column, Header, Footer, Top Row, 3 Columns

Source:http://social.technet.microsoft.com/Forums/sharepoint/en-US/39c98f27-248c-40b7-b859-088c7dcdeedc/cannot-create-web-part-page-with-powershell?forum=sharepointadminprevious

You can then add the webpart to the webpart page by using the code prsent in one of the posts you mentioned in the question.

2
 $wiki = $web.GetList($web.Url.ToString() + "/SitePages")
  $rootFolder = $wiki.RootFolder;
  $wikipage = $rootFolder.Files.Add($rootFolder.ServerRelativeUrl + 
  "/default.aspx", [Microsoft.SharePoint.SPTemplateFileType]::WikiPage)
  $wikiItem = $wikipage.Item;
  $wikiItem[[Microsoft.SharePoint.SPBuiltInFieldId]::WikiField] = "";
  $wikiItem.UpdateOverwriteVersion();
  Write-Host 'Wiki page added'
 $defaultPage= $web.GetFile($web.Url +"SitePages/default.aspx") 
 if($defaultPage.Exists -and $defaultPage.ListItem.File.CheckedOutByUser -eq $null)
 {    
     $webpartmanager=$defaultPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) 
     $wpGuid = [System.Guid]::NewGuid().ToString()  
     $wpKey = "g_" + $wpGuid.Replace("-","_")   
     $wp =New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart            
     $wp.ID = $wpKey 
     $code = "$"   
     $XmlDoc = New-Object System.Xml.XmlDocument
     $contentXml=$xmlDoc.CreateElement("content")
     $contentXml.InnerText= "<p> Lorem ipsum dolor sit amet, consectetur 
                             adipiscing elit </p>"
     $wp.Content =  $contentXml    
     $wp.Title = "Title"  
     $wp.Visible = $webPartProperty_Visible  
     $wp.ChromeType = "Default" 
     $wp.HorizontalAlign = "Center"   
     $webpartmanager.AddWebPart($wp, "Main", 0)   
     $web.Update()
     write-host "webpart added on" $web.Url + $view
   } 
0

I've written a simple script to add a webpart to a page using PowerShell which is tested and working fine.

Please note that to add a webpart to a page export webpart to your local drive and .webpart/.dwp file will be in xml format.

cls

asnp "*sh*"

$web=Get-SPweb -Identity "http://SP2013dev.com/sites/addwebpart/"

[xml]$webpartxml= Get-Content -Path "C:\Manju\WPRequest.xml"

$SR = New-Object System.IO.StringReader($webpartxml.OuterXml)

$XTR = New-Object System.Xml.XmlTextReader($SR)

$err=$null

$WebPartZoneID = "Topzone"

$WebPartZoneIndex = 0

 try
   {

  $page=$web.GetFile("Pages/default.aspx");

  $bool=$page.CheckedOutBy

       if($bool)
        {
            Write-Host "Page is already Checkout to " $page.CheckedOutBy.UserLogin

            $page.UndoCheckOut()

            Write-Host "Page is Over ridded by " $web.CurrentUser.DisplayName + " to Add Webpart"
        }

    $page.CheckOut();

    $wmgr=$web.GetLimitedWebPartManager($page,    [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);

    $webpart=$wmgr.ImportWebPart($XTR,[ref]$err);

    $wmgr.AddWebPart($webpart,$WebPartZoneID,$WebPartZoneIndex);

    $page.CheckIn('dude');

    $page.Publish('Adding request Site Webpart') 

    "Request Site WebPart SucessfullAdded" + (Get-Date -DisplayHint Date) | Out-File -Append "C:\OutPutLog.txt" 

     $SR.Close();
    $XTR.Close();
     $web.Dispose()

    }
    catch
     {
        $ErrorMessage = $_.Exception.Message 

        "Request Site WebPart Failure" + $ErrorMessage  +  (Get-Date -DisplayHint Date) | Out-File -Append "C:\ErrorLog.txt"
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.