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 ...
随机推荐
- HttpClient使用详解
http://itindex.net/detail/52566-httpclient HttpClient使用详解 标签: httpclient | 发表时间:2015-01-22 12:07 | 作 ...
- DMP文件的生成和使用
1.生成dmp的程序 #include <dbghelp.h> #pragma comment(lib, "dbghelp.lib") //设置异常处理回调函数Se ...
- hdu 4034 Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第 ...
- ezw证件照芯片压缩算法
相关网站:http://m.blog.csdn.net/blog/kimwu/12654517 http://blog.sina.com.cn/s/blog_4be751690100bsgb.html ...
- hdu 1007 最近点对问题(Splay解法)
为什么要写这个题..经典啊,当然,别以为我用分治做的,不过主要思想还是那神奇的六个点共存(一个h*2h的矩形中最多能放下多少个点使得两两距离不超过h) 其实我是在这里看到的 http://commun ...
- 关于Oralce数据库优化的几点总结
个人理解,数据库性能最关键的因素在于IO,因为操作内存是快速的,但是读写磁盘是速度很慢的,优化数据库最关键的问题在于减少磁盘的IO,就个人理解应该分为物理的和逻辑的优化, 物理的是指oracle产品本 ...
- WinMM.dll 函数汇总
#include "MMSystem.h" auxGetDevCaps 查询指定的辅助输出设备以确定其性能 auxGetNumDevs ...
- 【Java&Android开源库代码剖析】のAndroid-Universal-Image-Loader-part1
做Android app开发的同学应该都听说过或者用过nostra13的Android-Universal-Image-Loader开源库,它在图片异步加载.缓存和显示等方面提供了强大灵活的框架.之前 ...
- 深入理解Tomcat系列之一:系统架构(转)
前言 Tomcat是Apache基金组织下的开源项目,性质是一个Web服务器.下面这种情况很普遍:在eclipse床架一个web项目并部署到Tomcat中,启动tomcat,在浏览器中输入一个类似ht ...
- Java LinkedBlockingQueue和ArrayBlockingQueue分析
LinkedBlockingQueue是一个链表实现的堵塞队列,在链表一头增加元素,假设队列满.就会堵塞.还有一头取出元素.假设队列为空.就会堵塞. LinkedBlockingQueue内部使用Re ...