Add custom and listview web part to a page in wiki page using powershell
2013-09-13 21:19
633 查看
Asweknow,Addinglistviewwebpartisdifferentfromcustomwebpartusingpowershell,what'smore,therearealsodifferencebetweenaddingwebparttowebpartzonepageandwikipag.hereisthemethod.
And,becauseofaddwebparttowikipage,thewikipagedidn'thavewebpartzone,butithasthehidezonenamed"WPZ",afteraddingwebparttothewikipage,westillcouldn'tseeit,thereasonisthatthewikipageisreloadeybythehtmlcode,wohadtore-writethehtml:
<divclass="ms-rtestate-readms-rte-wpbox"contenteditable="false"style="float:left;width:30&%;min-width:300px;">
<divclass="ms-rtestate-notifyms-rtestate-read$($lvwpGuid)"id="div_$($lvwpGuid)"unselectable="on"></div>
<divid="$($lvwpGuid)"unselectable="on"style="display:none"></div>
</div>
sowejustchangetheidofdiv,thenwewillgettheresult.
Hereisthefunction:
AddCustomWebParthttp://localhost"SitePages/Home.aspx""TrendingTagsWebPart_TrendingTags.webpart""TrendingTags"
functionAddCustomWebPart($siteCollectionUrl,$pageUrl,$webPartName,$title){
$site=new-objectMicrosoft.SharePoint.SPSite($siteCollectionUrl);
$web=$site.OpenWeb()
$defaultPage=$web.GetFile($pageUrl)
$item=$defaultPage.Item
#CreatefancyGUID
$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);
#AddtheHTMLcontentandwebpartcontainerstothepage.
$wikiContent=@"
<divclass="ms-rtestate-readms-rte-wpbox"contenteditable="false"style="float:left;width:30&%;min-width:300px;"> <divclass="ms-rtestate-notifyms-rtestate-read$($lvwpGuid)"id="div_$($lvwpGuid)"unselectable="on"></div> <divid="$($lvwpGuid)"unselectable="on"style="display:none"></div> </div>
"@
#wikicontentisstoredinthefield“WikiContent”
$item["WikiField"]=$wikicontent
$item.Update()
$xmlReader.Close()
$web.Dispose()
$site.Dispose()
write-host"Done"
}
1.Addlistviewwebparttowikipage:
AddWebPartToWikihttp://localhost"SitePages/Home.aspx""Links""LinkOne"
functionAddWebPartToWiki($siteCollectionUrl,$pageUrl,$listName,$viewName){
$web=get-spweb$siteCollectionUrl
$list=$web.Lists[$listName]
$wpPage=$web.GetFile($pageUrl)
$item=$wpPage.Item
#GettheLimitedWebPartManager
$webpartmanager=$wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
#CreatefancyGUID
$lvwpGuid=[System.Guid]::NewGuid().ToString()
$lvwpKey="g_"+$lvwpGuid.Replace("-","_")
#Instantiatewp
$lvwp=new-objectMicrosoft.SharePoint.WebPartPages.XsltListViewWebPart
$lvwp.ID=$lvwpKey
$lvwp.WebID=$web.ID;
$lvwp.ChromeType="TitleOnly";
$lvwp.Title="YourTitle";
#$lvwp.TitleUrl="http://dev-sp";
$lvwp.Toolbar="NoToolbar";
$lvwp.ListID=$list.ID;
$lvwp.ListName=$list.ID.ToString();
#Settheview
$lvwp.ViewGuid=$list.Views[$viewName].ID.ToString();
#Addthewebpart
$webpartmanager.AddWebPart($lvwp,"WPZ",0);
#AddtheHTMLcontentandwebpartcontainerstothepage.
$wikiContent=@"
<divclass="ms-rtestate-readms-rte-wpbox"contenteditable="false"style="float:left;width:30%;min-width:300px;">
<divclass="ms-rtestate-notifyms-rtestate-read$($lvwpGuid)"id="div_$($lvwpGuid)"unselectable="on"></div>
<divid="$($lvwpGuid)"unselectable="on"style="display:none"></div>
</div>
"@
#wikicontentisstoredinthefield“WikiContent”
$item["WikiField"]+=$wikicontent
$item.Update()
#Updatetheweb
$web.Update();
$web.Dispose();
write-host"success"
}
MoretoLink:'target='_blank'>http://soufiane-benyoussef.blogspot.in/2011/09/add-custom-webpart-to-page-using-power.html
1.Addcustomwebparttowikipage:
Note:becauseofcustomwebpart,wecouldn'tnewthewebpartvianew-objectMicrosoft.SharePoint.WebPartPages.XsltListViewWebPart,hadtogetthewebpartfromthefolderofthewebpartcatalog.first,weshouldgetthecustomwebpartvianameinthewebpartfolder,second,readthecustomviaOpenBinaryStream()methodandimportthefiletowebpartobject.And,becauseofaddwebparttowikipage,thewikipagedidn'thavewebpartzone,butithasthehidezonenamed"WPZ",afteraddingwebparttothewikipage,westillcouldn'tseeit,thereasonisthatthewikipageisreloadeybythehtmlcode,wohadtore-writethehtml:
sowejustchangetheidofdiv,thenwewillgettheresult.
Hereisthefunction:
$site=new-objectMicrosoft.SharePoint.SPSite($siteCollectionUrl);
$web=$site.OpenWeb()
$defaultPage=$web.GetFile($pageUrl)
$item=$defaultPage.Item
#CreatefancyGUID
$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);
#AddtheHTMLcontentandwebpartcontainerstothepage.
$wikiContent=@"
<divclass="ms-rtestate-readms-rte-wpbox"contenteditable="false"style="float:left;width:30&%;min-width:300px;"> <divclass="ms-rtestate-notifyms-rtestate-read$($lvwpGuid)"id="div_$($lvwpGuid)"unselectable="on"></div> <divid="$($lvwpGuid)"unselectable="on"style="display:none"></div> </div>
"@
#wikicontentisstoredinthefield“WikiContent”
$item["WikiField"]=$wikicontent
$item.Update()
$xmlReader.Close()
$web.Dispose()
$site.Dispose()
write-host"Done"
}
1.Addlistviewwebparttowikipage:
$web=get-spweb$siteCollectionUrl
$list=$web.Lists[$listName]
$wpPage=$web.GetFile($pageUrl)
$item=$wpPage.Item
#GettheLimitedWebPartManager
$webpartmanager=$wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
#CreatefancyGUID
$lvwpGuid=[System.Guid]::NewGuid().ToString()
$lvwpKey="g_"+$lvwpGuid.Replace("-","_")
#Instantiatewp
$lvwp=new-objectMicrosoft.SharePoint.WebPartPages.XsltListViewWebPart
$lvwp.ID=$lvwpKey
$lvwp.WebID=$web.ID;
$lvwp.ChromeType="TitleOnly";
$lvwp.Title="YourTitle";
#$lvwp.TitleUrl="http://dev-sp";
$lvwp.Toolbar="NoToolbar";
$lvwp.ListID=$list.ID;
$lvwp.ListName=$list.ID.ToString();
#Settheview
$lvwp.ViewGuid=$list.Views[$viewName].ID.ToString();
#Addthewebpart
$webpartmanager.AddWebPart($lvwp,"WPZ",0);
#AddtheHTMLcontentandwebpartcontainerstothepage.
$wikiContent=@"
<divclass="ms-rtestate-readms-rte-wpbox"contenteditable="false"style="float:left;width:30%;min-width:300px;">
<divclass="ms-rtestate-notifyms-rtestate-read$($lvwpGuid)"id="div_$($lvwpGuid)"unselectable="on"></div>
<divid="$($lvwpGuid)"unselectable="on"style="display:none"></div>
</div>
"@
#wikicontentisstoredinthefield“WikiContent”
$item["WikiField"]+=$wikicontent
$item.Update()
#Updatetheweb
$web.Update();
$web.Dispose();
write-host"success"
}
MoretoLink:
相关文章推荐
- Add custom and listview web part to wiki page using powershell
- How to add custom fields to Customer Registration and Account page in magento
- Add/Remove listview web part in publish site via powershell
- Add/Remove listview web part in publish site via powershell
- How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- how to add Javascript and CSS in page layout with sharepoint 2013
- How To Do Math Using PowerShell, Part 1 and Part 2
- Add Script Editor and Content Editor Web Part via powershell
- Open Baidu webpage and type in Chinese characters to start searching, press “Back” button to go to B
- How to create your own custom 404 error page and handle redirect in SharePoint
- [Android 問題] How to Add a Button in ListView, and Make the Event of onClick and onItemClick Coexist?
- How to deal with the "ErrorWebPart" error displayed in the web part maintenance page
- using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.
- How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6)
- How to add custom property in custom webpart using VS 2012
- Using Timer to get history values from EDNA database, an error is prompted in the page : "Sys.WebForms.PageRequestManagerTimeout
- Export Grid Data To Excel In Advance Kendo UI Using MVC WEB API And Entity Framework
- Connect to URL and dump webpage in Groovy - Stack Overflow
- Using Imagecache and CCK Imagefield to Add an Image in Drupal
- (极力推荐)BW:How to load and generate a custom hierarchy in SAP Business Warehouse (SAP BW) without using flat files