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:background属性, 下面的 button_select实际上是button_select.xml --> <Bu ...

  2. ASP.NET MVC3中的路由系统 Routes

    MVC中,用户访问的地址并不映射到服务器中对应的文件,而是映射到对应Control里对应的ActionMethod,由ActionMethod来决定返回用户什么样的信息.而把用户访问的地址对应到对应的 ...

  3. AutoCAD 2014简体中文版官方正式版x86 x64下载,带注册机,永久免费使用

    注册机使用说明:会有部分杀毒软件报病毒,请无视.操作步骤:1.安装Autodesk AutoCAD 20142.使用以下序列号666-69696969安装.3.产品密码为001F14.安装完成后,启动 ...

  4. fedora21安装无线驱动

    来源:http://www.2cto.com/os/201202/120249.html 一.导入rpmfushion源,使用第三方yum 源: su -c 'yum localinstall --n ...

  5. poj 1084 Brainman(归并排序)

    题目链接:http://poj.org/problem?id=1804 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可. 根据分治法思想,序列分为两个大小相等的两部分,分别求子序列的逆 ...

  6. mmc一维下料测试

    另一组数据, 长度 = 6000; 切割长度 = {1664, 1599, 1552, 1409, 1352, 802, 660}; 需求数量 = {32, 96, 160, 16, 384, 112 ...

  7. ThinkPHP的验证码刷新显示和验证码显示不出来的原因

    1.应当这样<imp src='验证码路径' onclick="this.src='验证码路径?'+Math.random()">;如果后面不加Math.random( ...

  8. java读文件的几个类

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100f628.html 最初Java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Wri ...

  9. 【QT相关】Qt Widgets Module

    Qt Widgets Module:提供了一些列UI元素. 使用: //头文件包含 #include <QtWidgets> //链接模式,在.pro文件中添加行: QT += widge ...

  10. c语言中malloc realloc 和calloc的联系与区别

    (1)C语言跟内存分配方式 <1>从静态存储区域分配.       内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量.<2> ...