zoukankan      html  css  js  c++  java
  • Add custom and listview web part to a page in wiki page using powershell

    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;30&%;min-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;30&%;min-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;30%;min-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

  • 相关阅读:
    不要在股市上浪费时间(够深刻,耽误自己真本事的提高,即使是价值投资也不值得去做)
    在公司里混日子最终伤害的是你自己
    天使投资人的作用
    115太酷了,居然出了个TV版客户端
    QWidget与HWND的互相转换
    Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十五)
    QString的不常见用法
    不要神话创业,什么东西都可以自己做,损失就是不拿工资。如果吃不上饭了,那还是不要创业。服务器很便宜
    C++的try catch到底能防止什么错误?
    迷茫的时候,就随机择一去学,时间不要浪费在选择上了
  • 原文地址:https://www.cnblogs.com/riskyer/p/3320308.html
Copyright © 2011-2022 走看看