Add/Remove listview web part in publish site via powershell
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的更多相关文章
- 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 ...
- ListView Web 服务器控件概述(MSDN)
1: "折叠"图像"展开"图像"复制"图像"复制悬停"图像 全部折叠全部展开 代码:全部 代码:多个 代码:Visual ...
- 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 ...
- 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 ...
- 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 ...
- 有关集合的foreach循环里的add/remove
转自:Hollis(微信号:hollischuang) 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 1 .foreach循环 ...
- 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 ...
- HashSet——add remove contains方法底层代码分析(hashCode equals 方法的重写)
引言:我们都知道HashSet这个类有add remove contains方法,但是我们要深刻理解到底是怎么判断它是否重复加入了,什么时候才移除,什么时候才算是包括????????? add ...
- SharePoint自动化系列——Add/Remove “Hold” from items
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题1: 1.如果SharePoint item被添加了hold,通过UI界面来对SharePoi ...
随机推荐
- linux下安装node.js
1.下载 wget http://nodejs.org/dist/v0.10.32/node-v0.10.32-linux-x64.tar.gz 2.解压 tar -xvf node-v0.10.32 ...
- blender资源
blender资源 只能发帖不能更改的百度贴吧贴. http://tieba.baidu.com/f?kw=blender blendercn youku视频专辑: http://i.youku.co ...
- 如何debug ruby
how to debug ruby: 1. 第一种方法,直接使用ruby内建的debug在命令行调试,这个个gdb或者pdb的命令差不多. ruby -r debug yourubyfile.rb 2 ...
- Android获取Activity(应用)的执行状态及其它信息
检測某Activity是否在当前Task的栈顶 public static boolean isTopActivy(String cmdName, Context context) { Activit ...
- MFC之窗体改动工具栏编程状态栏编程程序启动画面
1窗体外观的改动 (1)改动在CMainFrame::preCreateWindow(CREATESTRUCT& cs) 改动标题:cs.style&=FWS_ADDTOTITLE; ...
- OCP读书笔记(11) - 使用闪回技术II
闪回归档 1. 什么是闪回数据归档? 闪回归档是用来保存一个或多个表的历史数据的新数据库对象,以及该数据的存储保留和清除策略.归档只是保存数据库中一个或多个表的所有事务处理的变化的一个或多个表空间,数 ...
- [cocos2d-x]屏幕自适应解决的方法
近期在写一个项目,要求pc,ipad,andriod平台上都能够执行,所以选择用cocos2d-x来开发. 我们的资源大小是1024*768的,在pc上和苹果上都是没有问题的,但是到了andriod上 ...
- mysql left join,right join,inner join用法分析
下面是例子分析表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 ...
- 2014 CSDN博文大赛终于获奖名单发布
博文大赛第二阶段(2014年7月15日-2014年8月10日)已经结束,决赛获奖名单已在8月11日出炉. 现将获奖名单发布: 移动开发 NO.1 罗升阳 Luoshengyang S ...
- LeetCode——Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...