Add custom and listview web part to wiki page using powershell
来源:互联网 发布:华客数据恢复 编辑:程序博客网 时间:2024/06/10 04:58
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, we 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:
Call 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); #if you wan to add the web part to web part zone page instead of wiki page, you just need to change the value "WPZ" to the zone # name you want, and the follow code wiil be not added. The same to add list view web part to wiki page# 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:
call the function :
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 wiki page using powershell
- Add/Remove listview web part in publish site via powershell
- Add Script Editor and Content Editor Web Part via powershell
- SharePoint关于publish page, WiKi page, Web part page区别
- How to add custom fields to Customer Registration and Account page in magento
- SilverLight_How to: Add Silverlight to a Web Page by Using HTML
- Custom PublishingLayoutPage(moss page to add codebehind code)
- powershell 给用户添加自定义属性 Using PowerShell to Set a Custom Attribute
- Web Part Page Maintenance(Redirect to this page)
- How to add custom property in custom webpart using VS 2012
- Add a custom tool to the web mapping application
- LINQ to SQL系列Part 9-Using a Custom LINQ Expression with the control
- How to add custom scripts
- Using Index Server to Search Your Web Site - Part I
- Using Index Server to Search Your Web Site - Part II
- Unable to add selected web part(s). Cannot import this web part
- Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
- Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically
- 浅析远程过程调用 RPC
- Windows Embedded 8.1 Industry 的新增功能
- 从零开始学习jQuery (二) 万能的选择器
- 质量管理术语解释
- 好玩的笔试题
- Add custom and listview web part to wiki page using powershell
- 装系统那些事儿-1-电脑的启动流程
- 在Ubuntu上安装GNOME 3.8
- 24点|穷举法【待续】
- Win32 RPC Demo之Hello World
- 从零开始学习jQuery (三) 管理jQuery包装集
- CSharp Algorithm - Replace multiplication operator with a method
- 【Apache ZooKeeper】理解ZooKeeper中的ZNodes
- 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式