您的位置:首页 > 运维架构 > Shell

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.

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:

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐