1. Here is the code: Add WebPart in Publish Site

Example : AddWebPartPublish http://localhost  "/Pages/Publish.aspx" "Shared Documents" "Header" "0"

###########################################################################

# $siteUrl : The site url -Example: http://localhost                                                                       #

# $pagePath: The page path                                                                                                       #

# $listName: The name of list that should be added. -Example: Birthdays                                 #

# $webpartZone: The zone in page  - Example: "Header"                                                          #

# $index: The index of web part in the zone  -Example: "0"                                                       #

###########################################################################

#  Import the Microsoft.SharePoint.PowerShell

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
Add-PSSnapin System.Reflection -erroraction silentlyContinue function AddWebPartPublish($siteUrl, $pagePath, $listName, $webpartZone, $index)
{
$pageUrl = $siteUrl + $pagePath
$spWeb = Get-SPWeb $siteUrl -ErrorAction Stop [Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb);
$allowunsafeupdates = $spWeb.AllowUnsafeUpdates
$spWeb.AllowUnsafeUpdates = $true $page = $spWeb.GetFile($pageUrl);
if ($page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin)
{
write-host "Page has already been checked out "
}
else
{
$SPWeb.CurrentUser.LoginName
$page.UndoCheckOut()
$page.CheckOut()
write-host "Check out the page override"
} }
else
{
$page.CheckOut()
write-host "Check out the page"
}
try{
#Initialise the Web part manager for the specified profile page.
$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) # Check list is exist or not
foreach($list in $spWeb.Lists)
{
if($list.Tostring() -eq $listName)
{
write-host "The list named $list is existing"
#$ListViewWebPart = New-Object Microsoft.SharePoint.WebPartPages.ListViewWebPart
$ListViewWebPart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart
$listViewWebPart.Title = $listName
$ListViewWebPart.ListName = ($list.ID).ToString("B").ToUpper() #$ListViewWebPart.ChromeType = "none" $ListViewWebPart.ViewGuid = ($list.Views[0].ID).ToString("B").ToUpper() $spWebPartManager.AddWebPart($ListViewWebPart, $webpartZone, $index)
$spWebPartManager.SaveChanges($ListViewWebPart) break; }
} #Check to ensure the page is checked out by you, and if so, check it in
if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin)
{
$page.CheckIn("Page checked in automatically by PowerShell script")
Write-Output "Page has been checked in"
} $page.Publish("Published")
Write-Output "Page has been published success"
$pubWeb.Close()
$spWeb.Update() if($flag -eq 0)
{
write-host "Not exist"
}
else
{
Write-Output "success"
}
} catch
{
write-host "(ERROR : "$_.Exception.Message")"
throw
} finally
{ $spWeb.Dispose() } }

2. Here is the code: Delete WebPart in Publish Site

Example : RemoveWebPart " http://localhost"  "/Pages/Publish.aspx" "Shared Documents"

###########################################################################

# $siteUrl : The site url -Example: http://localhost                                                                       #

# $pagePath: The page path                                                                                                       #

# $listName: The name of list that should be added. -Example: Birthdays                                 #

###########################################################################

#  Import the Microsoft.SharePoint.PowerShell

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
} function RemoveWebPart($siteUrl, $pagePath, $name)
{
#$web = $site.OpenWeb("/Ops")
# Check the parameter is null or not
if($siteUrl.trim() -eq $null -or $name.trim() -eq $null -or $pagePath.trim() -eq $null)
{
write-host "The parameter is null"
return
}
$pageUrl = $siteUrl + $pagePath
$spWeb = Get-SPWeb $siteUrl -ErrorAction Stop [Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb);
$allowunsafeupdates = $spWeb.AllowUnsafeUpdates
$spWeb.AllowUnsafeUpdates = $true $page = $spWeb.GetFile($pageUrl);
if ($page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin)
{
write-host "Page has already been checked out "
}
else
{
$SPWeb.CurrentUser.LoginName
$page.UndoCheckOut()
$page.CheckOut()
write-host "Check out the page override"
} }
else
{
$page.CheckOut()
write-host "Check out the page"
}
try{
#Initialise the Web part manager for the specified profile page.
$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$flag = 0
#Circle all the Webparts in the specified page
foreach ($webpart in $spWebPartManager.WebParts)
{
#write-host $siteUrl +": Existing Web part - " + $webpart.Title + " : " + $webpart.ID
if($webpart.Title -eq $name)
{ $spWebPartManager.DeleteWebPart($spWebPartManager.WebParts[$webpart.ID])
$spWebPartManager.SaveChanges($webpart)
Write-Output "Delete the webpart"
$flag = 1 break;
}
} #Check to ensure the page is checked out by you, and if so, check it in
if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin)
{
$page.CheckIn("Page checked in automatically by PowerShell script")
Write-Output "Page has been checked in"
} $page.Publish("Published")
Write-Output "Page has been published success"
$pubWeb.Close()
$spWeb.Update() if($flag -eq 0)
{
write-host "Not exist"
}
else
{
Write-Output "success"
}
} catch
{
write-host "(ERROR : "$_.Exception.Message")"
throw
} finally
{ $spWeb.Dispose() } }

Add/Remove listview web part in publish site via powershell的更多相关文章

  1. Add custom and listview web part to a page in wiki page using powershell

    As we know, Adding list view web part is different from custom web part using powershell, what's mor ...

  2. ListView Web 服务器控件概述(MSDN)

    1: "折叠"图像"展开"图像"复制"图像"复制悬停"图像 全部折叠全部展开 代码:全部 代码:多个 代码:Visual ...

  3. WIX: Hide installed program from the Add/Remove Programs window.

    Reference article : How to hide an entry in the Add/Remove Programs applet? In Wix source files, set ...

  4. How to hide an entry in the Add/Remove Programs applet?

    Original link: http://www.winhelponline.com/articles/15/1/How-to-hide-an-entry-in-the-AddRemove-Prog ...

  5. Maste Note for OCR / Vote disk Maintenance Operations (ADD/REMOVE/REPLACE/MOVE)

    Doc ID 428681.1 Applies to: Oracle Database - Enterprise Edition - Version 10.2.0.1 to 11.2.0.1.0 [R ...

  6. 有关集合的foreach循环里的add/remove

    转自:Hollis(微信号:hollischuang) 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 1 .foreach循环 ...

  7. System.ArgumentException: 已添加了具有相同键的项。(An item with the same key has already been added) 在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 在 System.Web.Mvc.Js

    最近将一个项目从ASP.NET MVC 3升级至刚刚发布的ASP.NET MVC 5.1,升级后发现一个ajax请求出现了500错误,日志中记录的详细异常信息如下: System.ArgumentEx ...

  8. HashSet——add remove contains方法底层代码分析(hashCode equals 方法的重写)

    引言:我们都知道HashSet这个类有add   remove   contains方法,但是我们要深刻理解到底是怎么判断它是否重复加入了,什么时候才移除,什么时候才算是包括????????? add ...

  9. SharePoint自动化系列——Add/Remove “Hold” from items

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题1: 1.如果SharePoint item被添加了hold,通过UI界面来对SharePoi ...

随机推荐

  1. “ASP.default_aspx”并不包括“DropDownList1_SelectedIndexChanged”的定义,其解决方法。

    "ASP.default_aspx"并不包括"DropDownList1_SelectedIndexChanged"的定义,其解决方法. 在使用DropDown ...

  2. 设置 Linux 的 LD_LIBRARY_PATH 变量

    源地址:http://www.cnblogs.com/trying/archive/2013/06/07/3123577.html 我们在ubuntu图形界面下用eclipse写了一个动态库,到cen ...

  3. hdu2647解题报告

    题意:有个工厂的老板给工人发奖金,每人基础都是888,工人们有自己的想法,如:a 工人想要比 b 工人的奖金高,老板想要使花的钱最少 那么就可以 给b 888,给a 889 ,但是如果在此基础上,b也 ...

  4. 用Feed43为随意站点定制RSS feed教程~

     用Feed43为随意站点定制RSS feed教程- Feed43--自己定义RSS种子的免费工具中笔者的最爱,确切来讲Feed43不适合心脏衰弱者.通过它的服务,我能够很好地控制种子的终于样式,当然 ...

  5. OpenRisc-50-or1200的freeze模块分析

    引言 之前,我们分析or1200的控制通路中的sprs模块和except模块,本小节,我们就分析一下or1200控制通路的最后一个模块,就是freeze模块. 1,整体分析 freeze模块,顾名思义 ...

  6. ZOJ3805:Machine

    In a typical assembly line, machines are connected one by one. The first machine's output product wi ...

  7. 正确理解HTML,XHTML页面的头部doctype定义

    摘自http://www.west263.com/info/html/wangyezhizuo/css/20080225/42390.html 当我们制作页面的时候,总会在它的源代码头部看到一串声明, ...

  8. CoreJava_Collection接口add有一个返回值!

        今天讨论与朋友小知识,我发现很多人不知道collection接口定义add方法返回的值,我们所有的工作很多年Java职工.不熟悉的公共接口,使自己成为在细节上的麻烦.一个小采访这让我想起,当我 ...

  9. 跨平台网络通信与server编程框架库(acl库)介绍

    一.描写叙述 acl project是一个跨平台(支持LINUX,WIN32,Solaris,MacOS,FreeBSD)的网络通信库及server编程框架,同一时候提供很多其它的有用功能库.通过该库 ...

  10. UltraEdit破解方法最强收录

    作为一个能够满足你一切编辑需求的强大文本编辑器.ultraedit在IT届有着非常高的人气.只是它正版的价钱也是不廉价滴,没记错的话是要好几十刀. 那么对于我们来说,破解UltraEdit就是一项必备 ...