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. MySQL如何修改root密码

    MySQL修改用户密码         因为长期不登录MySQL数据库,登录时经常忘记root权限密码.本文提供一个在数据库服务器上修改root密码的方法,本文撰写基础是在xp操作系统下进行. 第一步 ...

  2. 利用SQL语句实现分页

    1.概述 在网页中如果显示的数据太多就会占据过多的页面,而且显示速度也会很慢.为了控制每次在页面上显示数据的数量,就可以利用分页来显示数据. 2.技术要点 在SQL Server中要实现SQL分页,需 ...

  3. 与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室

    原文:与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...

  4. hdu1171(DP求两份物品的价值相差最小)

    题目信息: 给出一些物品的价值和个数.分成两份,是这两份的价值相差最小(DP方法) http://acm.hdu.edu.cn/showproblem.php? pid=1171 AC代码: /** ...

  5. Dom生成Xml和解析Xml

    xml这样的文件格式在非常多时候都是非常适合我们用来存取数据的,所以利用程序来生成xml文件和解析xml文件就显得比較重要了.在dom中是把每个元素都看做是一个节点Node的,全部页面上的属性.元素等 ...

  6. API - 微云

    API - 微云 1.接口说明 2.数据上传协议说明 1. 接口说明 文件上传申请,成功会返回实际上传的地址. 根据申请上传返回的地址,组织数据上传. 1.1 URL OAuth2.0协议: http ...

  7. windows 2003 自动安全设置

    @echo offecho.echo.echo.echo 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓echo.echo.echo windows 2003 自动安全设置程序 echo. ec ...

  8. Restlet+Fastjson 高速构建轻量级 Java RESTful Webservice

    自己入门Java时做过一个小型RESTful Web Service的项目,这里总结一下. 服务的数据交换格式主要採用JSON,服务为REST风格.连接採用Http协议,数据库使用MySQL,OR M ...

  9. JVM学习03_new对象的内存图讲解,以及引出static方法(转)

    目录 -=-讲解对象创建过程中,-=-堆内存和栈内存的情况 -=-构造函数对类对象的成员变量的初始化过程 -=-构造函数出栈 -=-类的方法在不访问类对象的成员变量时造成的内存资源浪费怎么解决? -= ...

  10. Android Studio使用心得 - 简单介绍与环境配置

    FBI Warning:欢迎转载,但请标明出处:http://blog.csdn.net/codezjx/article/details/38544823,未经本人允许请勿用于商业用途.感谢支持! 关 ...