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. html5实现拖拽文件上传

    以下是自学it网--中级班上课笔记 网址:www.zixue.it html文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict ...

  2. 开启cocos2dx 3.0的Console功能

    下面内容用于自己知识的备忘,想看具体内容,请參照例如以下地址. 原英文文地址: http://discuss.cocos2d-x.org/t/cocos3-0-tutorial-console-tut ...

  3. What is the difference between JRE,JVM and JDK?

    If you are a Java developer, it is very often that you think about understanding the JRE,JVM and JDK ...

  4. android 在你的UI中显示Bitmap - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Displaying Bitmaps in Your UI 在你的UI中显示Bitmap ...

  5. linux根据部署jenkins

    1. Jenkins 下载 Jenkins 下载网址:http://jenkins-ci.org/ 2. Jenkins 安装 (1) 安装JDK JDK下载:http://www.oracle.co ...

  6. Ctrl-A全选

    Ctrl-A全选这点事(C#,WinForm)   所有的文本框,不管单行多行都Ctrl-A全选就好了吧?是啊,很方便.Windows的软件基本都是这样.可为什么我们自己制作的WinForm就默认不是 ...

  7. poj3642 Charm Bracelet(0-1背包)

    题目意思: 给出N,M,N表示有N个物品,M表示背包的容量.接着给出每一个物品的体积和价值,求背包可以装在的最大价值. http://poj.org/problem? id=3624 题目分析: o- ...

  8. 阿里巴巴2015研究project普通笔试题,与答案

    欢迎您对这篇文章的其他建议.我可以留言在以下平台. 个人博客网站:www.anycodex.com/blog/ Csdn博客网站:http://my.csdn.net/?ref=toolbar 微博: ...

  9. linux的tr命令

    tr(translate缩写)主要用于删除文件中的控制字符,或进行字符转换. 语法 tr [ -c | -cds | -cs | -C | -Cds | -Cs | -ds | -s] [ -A] S ...

  10. 怎样在Linux下通过ldapsearch查询活动文件夹的内容

    从Win2000開始.微软抛弃NT域而採用活动文件夹来管理Windows域.而活动文件夹就是微软基于遵守LDAP协议的文件夹服务.假设用扫描器扫描的话能够发现活动文件夹的389port是打开的.并且微 ...