As we know, Adding list view web part is different from custom web part using powershell, what's more, there are also difference between adding web part to web part zone page and wiki pag. here is the method.

1. Add custom web part to wiki page:

Note: because of custom web part, we couldn't new the web part via new-object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, had to get the web part from the folder of the web part catalog. first, we should get the custom web part via name in the web part folder, second, read the custom via OpenBinaryStream() method and import the file to web part object.

And, because of add web part to wiki page, the wiki page didn't have web part zone, but it has the hide zone named "WPZ", after adding web part to the wiki page, we still couldn't see it, the reason is that the wiki page is reloadey by the html code, wo had to re-write the html:

<div class="ms-rtestate-read ms-rte-wpbox" contenteditable="false" style="float:left;width:30&%;min-width:300px;">
<div class="ms-rtestate-notify ms-rtestate-read $($lvwpGuid)" id="div_$($lvwpGuid)" unselectable="on"></div>
<div id="$($lvwpGuid) " unselectable="on" style="display: none"></div>
</div>

so we just change the id of div, then we will get the result. 
Here is the function:

AddCustomWebPart http://localhost "SitePages/Home.aspx" "TrendingTagsWebPart_TrendingTags.webpart" "Trending Tags"
function AddCustomWebPart($siteCollectionUrl, $pageUrl, $webPartName, $title){

$site = new-object Microsoft.SharePoint.SPSite($siteCollectionUrl);
$web = $site.OpenWeb()
$defaultPage = $web.GetFile($pageUrl)
$item = $defaultPage.Item #Create fancy GUID
$lvwpGuid = [System.Guid]::NewGuid().ToString()
$lvwpKey = "g_" + $lvwpGuid.Replace("-","_")
$errorMsg = "" [Microsoft.SharePoint.SPList]$wpList = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog)
[Microsoft.SharePoint.SPFolder]$wpFolder = $wpList.RootFolder
[Microsoft.SharePoint.SPFile]$wpFile = $wpFolder.Files[$webPartName]
[System.Xml.XmlReader]$xmlReader = [System.Xml.XmlReader]::Create($wpFile.OpenBinaryStream())
[Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager]$wpManager = $defaultPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $myCustomWP = $wpManager.ImportWebPart($xmlReader,[ref]$errorMsg)
$myCustomWP.ID = $lvwpKey
$myCustomWP.Title = $title
$wpManager.AddWebPart($myCustomWP, "WPZ", 0); # Add the HTML content and web part containers to the page.
$wikiContent = @" <div class="ms-rtestate-read ms-rte-wpbox" contenteditable="false" style="float:left;width:30&%;min-width:300px;">
<div class="ms-rtestate-notify ms-rtestate-read $($lvwpGuid)" id="div_$($lvwpGuid)" unselectable="on"></div>
<div id="$($lvwpGuid) " unselectable="on" style="display: none"></div>
</div> "@ #wiki content is stored in the field “Wiki Content”
$item["WikiField"] = $wikicontent
$item.Update()
$xmlReader.Close()
$web.Dispose()
$site.Dispose()
write-host "Done"
}

1. Add list view  web part to wiki page:

 
AddWebPartToWiki http://localhost "SitePages/Home.aspx" "Links" "LinkOne"
function AddWebPartToWiki($siteCollectionUrl, $pageUrl, $listName, $viewName){
$web = get-spweb $siteCollectionUrl
$list = $web.Lists[$listName]
$wpPage = $web.GetFile($pageUrl)
$item = $wpPage.Item # Get the LimitedWebPartManager
$webpartmanager=$wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) #Create fancy GUID
$lvwpGuid = [System.Guid]::NewGuid().ToString()
$lvwpKey = "g_" + $lvwpGuid.Replace("-","_") # Instantiate wp
$lvwp = new-object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart
$lvwp.ID = $lvwpKey
$lvwp.WebID = $web.ID;
$lvwp.ChromeType = "TitleOnly";
$lvwp.Title = "Your Title";
#$lvwp.TitleUrl = "http://dev-sp";
$lvwp.Toolbar = "No Toolbar";
$lvwp.ListID = $list.ID;
$lvwp.ListName = $list.ID.ToString(); # Set the view
$lvwp.ViewGuid = $list.Views[$viewName].ID.ToString(); # Add the web part
$webpartmanager.AddWebPart($lvwp, "WPZ", 0); # Add the HTML content and web part containers to the page.
$wikiContent = @" <div class="ms-rtestate-read ms-rte-wpbox" contenteditable="false" style="float:left;width:30%;min-width:300px;">
<div class="ms-rtestate-notify ms-rtestate-read $($lvwpGuid)" id="div_$($lvwpGuid)" unselectable="on"></div>
<div id="$($lvwpGuid) " unselectable="on" style="display: none"></div>
</div> "@ #wiki content is stored in the field “Wiki Content”
$item["WikiField"] += $wikicontent
$item.Update() # Update the web
$web.Update();
$web.Dispose(); write-host "success"
}

More to Link: http://soufiane-benyoussef.blogspot.in/2011/09/add-custom-webpart-to-page-using-power.html

Add custom and listview web part to a page in wiki page using powershell的更多相关文章

  1. Add/Remove listview web part in publish site via powershell

    1. Here is the code: Add WebPart in Publish Site Example : AddWebPartPublish http://localhost  " ...

  2. Add Columns to the Web Sessions List

    To add custom columns to the Web Sessions List, add rules using FiddlerScript. The BindUIColumn Attr ...

  3. [webgrid] – header - (How to Add custom html to Header in WebGrid)

    How to Add custom html to Header in WebGrid MyEvernote Link Posted on March 30, 2013by mtryambake Ho ...

  4. Visual Studio发布Web项目报错:Unable to add 'xxx' to the Web site. Unable to add file 'xxx'. The specified file could not be encrypted.

    背景 Visual Studio下的Web项目 现象 发布时遇到Unable to add 'xxx' to the Web site.  Unable to add file 'xxx'. The ...

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

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

  6. How To Add Custom Build Steps and Commands To setup.py

    转自:https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/ A setup.py scrip ...

  7. Add custom daemon on Linux System

    Ubuntu add custom service(daemon) Task 需要在系统启动的时候自动启动一个服务(后台程序),在系统关闭的时候关闭服务. 比如在部署某个应用之前,需要将某个任务设置成 ...

  8. [原创]java WEB学习笔记16:JSP指令(page,include),JSP标签(forwar,include,param)

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. How to: Add SharePoint 2010 Search Web Parts to Web Part Gallery for Upgraded Site Collections

    When you upgrade to Microsoft SharePoint Server 2010, some of the new SharePoint Enterprise Search W ...

随机推荐

  1. android四大组件详解--摘

    Android四大基本组件介绍与生命周期 Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器 ...

  2. [转]SAP中找表的方法

    http://blog.chinaunix.net/uid-24063584-id-2642334.html 分类: 18种根据屏幕字段查找数据库表数据的技巧 帮助   18种根据屏幕字段查找潜在数据 ...

  3. centos5.5 安装git

    查看centos版本 # cat /etc/redhat-release CentOS release 5.5 (Final) 安装git 下载: 如果有老版本的git: #git clone git ...

  4. [Swust OJ 412]--医院设置(floyd算法)

    题目链接:http://acm.swust.edu.cn/problem/412/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  5. EAN 通用商品条形码

    商品条形码是指由一组规则排列的条.空及其对应字符组成的标识,用以表示一定的商品信息的符号.其中条为深色.空为纳色,用于条形码识读设备的扫描识读.其对应字符由一组阿拉伯数字组成,供人们直接识读或通过键盘 ...

  6. 基于visual Studio2013解决C语言竞赛题之0307函数求值

      题目 解决代码及点评 这又是个条件函数,但是这个函数无法用switch来解决,因为switch只能用于和某条件相等情况下,而这个函数的范围是无穷的 遇到这种问题,我们还是需要用复合的if语 ...

  7. How many integers can you find(容斥+dfs容斥)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. Effective C++:条款37:绝不又一次定义继承而来的缺省參数值

    因为又一次定义继承而来的non-virtual函数是不对的(见上一个条款),所以这个条款就将问题局限于:绝不又一次定义继承一个带有缺省參数值的virtual函数. (一) virtual函数是动态绑定 ...

  9. Win7安装IIS

    非常明显的,我们做系统是用来给人用的,所以这就涉及到对系统的公布问题.仅仅有公布之后.别人才干通过訪问你的IP和port号来訪问你的程序. 而系统公布一般都是在IIS上面进行系统公布,所以我们就必需要 ...

  10. 初步swift语言学习笔记2(可选类型?和隐式可选类型!)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/28904115 转载请注明出处 假设认为文章对你有所帮助.请通过留言 ...