Add custom and listview web part to a page in wiki page using powershell

时间:2022-06-01 19:38:57

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&sol;Remove listview web part in publish site via powershell

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

  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. &lbrack;webgrid&rsqb; &ndash&semi; header - &lpar;How to Add custom html to Header in WebGrid&rpar;

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

  4. Visual Studio发布Web项目报错&colon;Unable to add &&num;39&semi;xxx&&num;39&semi; to the Web site&period; Unable to add file &&num;39&semi;xxx&&num;39&semi;&period; The specified file could not be encrypted&period;

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

  5. ListView Web 服务器控件概述&lpar;MSDN&rpar;

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

  6. How To Add Custom Build Steps and Commands To setup&period;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. &lbrack;原创&rsqb;java WEB学习笔记16:JSP指令&lpar;page&comma;include&rpar;,JSP标签&lpar;forwar&comma;include&comma;param&rpar;

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

  9. How to&colon; 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. PL&sol;SQL查询oracle数据库对象

    dictionary 全部数据字典表的名称和解释,它有一个同义词dict,dict_column 全部数据字典表里字段名称和解释 如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句: se ...

  2. UICollectionView基础

    初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...

  3. poj2407 Relatives 欧拉函数基本应用

    题意很简单 就是欧拉函数的定义: 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) .题目求的就是φ(n) 根据 通式:φ(x)=x*(1-1/p1)*(1-1/ ...

  4. OCR怎么能离开扫描仪呢?

    说起OCR,说来说去就是和各种各样的图片打交道. 所以图片的质量很的关键. 说起图片的质量,不得不提的就是图片的採集. 眼下最靠谱的图像採集来源就是扫描仪. 扫描仪的话就大有说法,最靠谱的扫描仪,扫描 ...

  5. 12 款最好的 Bootstrap 设计工具

    作为一位设计师,会经常追寻新鲜有趣的设计工具,这些工具会提高工作的效率,使得工作更有效, 最重要的是使工作变得更方便.非常肯定的说,随着日益增长的工具和应用的数量,设计和开发变得越来越简单了. 其中最 ...

  6. docker——安全防护与配置

    Docker是基于Linux操作系统实现的应用虚拟化.运行在容器内的进程,跟运行在本地系统的进程本质上并无区别,配置不合适的安全策略将可能给本地系统带来安全风险,因此,Docker的安全性在生产环境中 ...

  7. &lbrack;GO&rsqb;使用select实现超时

    package main import ( "fmt" "time" ) func main() { ch := make(chan int) quit := ...

  8. &lbrack;学习笔记&rsqb;通过open函数改变标准输出的方法

    int main(void) { char s[] = "abc.txt"; ; close(STDOUT_FILENO);//关闭标准输出文件描述符 int fd1 = open ...

  9. C语言中强制类型转换总结

    C语言中强制类型转换总结  ● 字符型变量的值实质上是一个8位的整数值,因此取值范围一般是-128-127,char型变量也可以加修饰符unsigned,则unsigned char 型变量的取值范围 ...

  10. oracle表空间不足:ORA-01653&colon; unable to extend table

    问题背景: oracle表空间不足报错是比较常见的故障,尤其是没有对剩余表空间做定期巡检的系统: 报错代码如下: oracle表空间不足错误代码:ORA-01653: unable to extend ...