Add Calendar View Web Part PowerShell Script


/ Published in: Windows PowerShell
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function AddCalendarWP($web)
  2. {
  3. $wpPage = $web.GetFile("Pages/home.aspx")
  4. if($wpPage -ne $null)
  5. {
  6. if($wpPage.CheckedOutByUser -ne $null){$wpPage.UndoCheckOut()}
  7. $wpPage.CheckOut()
  8. $list = $web.Lists["mhcCalendar"]
  9. $wpManager = $wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  10.  
  11. $wpView = New-Object "Microsoft.SharePoint.WebPartPages.ListViewWebPart"
  12. $wpView.ListName = $list.ID.ToString("B").ToUpper()
  13. $wpView.ViewType = "Calendar"
  14. $wpView.ViewGuid = $list.DefaultView.ID.ToString("B").ToUpper()
  15. $wpView.Title = "Calendar"
  16. $wpManager.AddWebPart($wpView,"Center",6)
  17. $wpManager.SaveChanges($wpView)
  18. $wpManager.Dispose()
  19. $wpPage.CheckIn("")
  20. $wpPage.Publish("from Powershell")
  21. }
  22. else
  23. {
  24. Write-Host -ForegroundColor Red "Home Page not found. Calendar web part not added"
  25. }
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.