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. C - N皇后问题(搜索)

    Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合 ...

  2. A.归并排序

    归并排序 (求逆序数) 归并排序:递归+合并+排序 时间复杂度:O(n logn)    空间复杂度:O(n) 用途:1.排序  2.求逆序对数 Description In this problem ...

  3. Longest Substring Without Repeating Characters - 哈希与双指针

    题意很简单,就是寻找一个字符串中连续的最长包含不同字母的子串. 其实用最朴素的方法,从当前字符开始寻找,找到以当前字符开头的最长子串.这个方法猛一看是个n方的算法,但是要注意到由于字符数目的限制,其实 ...

  4. C++对C语言的非面向对象特性扩充(2)

    上一篇随笔写了关于C++在注释,输入输出,局部变量说明的扩充,以及const修饰符与C中的#define的比较,也得到了几位学习C++朋友们的帮助讲解,十分感谢,我也希望欢迎有更多学习C++的朋友一起 ...

  5. Map接口的学习

    接口Map<K, V> 一.Map功能 1.添加 put(K key, V value) putAll(Map<? extends K, ? extends V>); 2.删除 ...

  6. MySql 初次安装登陆

    名称:随便写 服务器:127.0.0.1或者localhost 端口:在安装mysql应该看到是3306 用户:root 密码:(默认的是空,如果你设置过自己应该知道) 其他就可以不用设置

  7. PowerShell入门(序):为什么需要PowerShell?

    原文:http://www.cnblogs.com/ceachy/archive/2013/01/23/PowerShellPreface.html 曾几何时,微软的服务器操作系统因为缺乏一个强大的S ...

  8. 我的ubuntu

    题外话:不知不觉也已经大三,最近思考了很多.在腾讯网看到了对李嘉诚的一篇专访,感触颇深. 想起来我从第一次接触ubuntu到现在也有一年了,记得第一个版本还是12.04,不过很快就换成了12.10,在 ...

  9. JRebel 6 破解版及使用方法

    最近更新到jrebel6.2.1了,我自己做了个技术分享的微信公众号(茶爸爸),有心的朋友可以来这里一起学习 云盘下载链接: http://pan.baidu.com/s/1bnGzMUF 配置: - ...

  10. cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(二)

    在了解地图的初始化和加载之前,我们先了解下mario的地图. 用tiled工具打开mario地图 从地图中可以看到,mario的地图有很多层构成: objects层:怪物,会动的怪物 coin层:金币 ...