Friday, 15 February 2019

Add document library to publishing page powershell

Why can't you see the export on out of box web part?  This is by design but there is a very easy workaround.

In SharePoint design change the web parts that you would like to export 

  1. Configure the web part on the publish page the way you would like it to look
  2. Open the site in SharePoint Designer 
  3. In the code view find the web part and find <ExportControlledProperties>false</ExportControlledProperties> and change from “false” to “true”
  4. Save the page
  5. Open the web page in the browser, Edit the Page and export the web part.
  6. Give the file a name and save it somewhere where you can find it in the next step.

Powershell code

$wpxml = '<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="ShowWithSampleData" type="bool">False</property>
        <property name="Default" type="string" />
        <property name="NoDefaultStyle" type="string" null="true" />
        <property name="CacheXslStorage" type="bool">True</property>
        <property name="ViewContentTypeId" type="string" />
        <property name="XmlDefinitionLink" type="string" />
        <property name="ManualRefresh" type="bool">False</property>
        <property name="ListUrl" type="string" />
        <property name="ListId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">3ba65105-0767-4f57-9aa8-3d2e3c1e10cf</property>
        <property name="TitleUrl" type="string">/My School/2392/Shared Documents Area</property>
        <property name="EnableOriginalValue" type="bool">False</property>
        <property name="Direction" type="direction">NotSet</property>
        <property name="ServerRender" type="bool">False</property>
        <property name="ViewFlags" type="Microsoft.SharePoint.SPViewFlags, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">Html, TabularView, Hidden, Mobile</property>
        <property name="AllowConnect" type="bool">True</property>
        <property name="ListName" type="string">{3BA65105-0767-4F57-9AA8-3D2E3C1E10CF}</property>
        <property name="ListDisplayName" type="string" />
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="ChromeState" type="chromestate">Normal</property>
        <property name="DisableSaveAsNewViewButton" type="bool">False</property>
        <property name="ViewFlag" type="string" />
        <property name="DataSourceID" type="string" />
        <property name="ExportMode" type="exportmode">All</property>
        <property name="AutoRefresh" type="bool">False</property>
        <property name="FireInitialRow" type="bool">True</property>
        <property name="AllowEdit" type="bool">True</property>
        <property name="Description" type="string" />
        <property name="HelpMode" type="helpmode">Modeless</property>
        <property name="BaseXsltHashKey" type="string" null="true" />
        <property name="AllowMinimize" type="bool">True</property>
        <property name="CacheXslTimeOut" type="int">86400</property>
        <property name="ChromeType" type="chrometype">Default</property>
        <property name="Xsl" type="string" null="true" />
        <property name="JSLink" type="string" null="true" />
        <property name="CatalogIconImageUrl" type="string">/_layouts/15/images/itdl.png?rev=23</property>
        <property name="SampleData" type="string" null="true" />
        <property name="UseSQLDataSourcePaging" type="bool">True</property>
        <property name="TitleIconImageUrl" type="string" />
        <property name="PageSize" type="int">-1</property>
        <property name="ShowTimelineIfAvailable" type="bool">True</property>
        <property name="Width" type="string" />
        <property name="DataFields" type="string" />
        <property name="Hidden" type="bool">False</property>
        <property name="Title" type="string" />
        <property name="PageType" type="Microsoft.SharePoint.PAGETYPE, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">PAGE_NORMALVIEW</property>
        <property name="DataSourcesString" type="string" />
        <property name="AllowClose" type="bool">True</property>
        <property name="InplaceSearchEnabled" type="bool">True</property>
        <property name="WebId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">00000000-0000-0000-0000-000000000000</property>
        <property name="Height" type="string" />
        <property name="GhostedXslLink" type="string">main.xsl</property>
        <property name="DisableViewSelectorMenu" type="bool">False</property>
        <property name="DisplayName" type="string" />
        <property name="IsClientRender" type="bool">False</property>
        <property name="XmlDefinition" type="string">&lt;View Name="{FA0742C6-54A6-476C-8DB6-247AE4DAE34C}" MobileView="TRUE" Type="HTML" Hidden="TRUE" DisplayName="" Url="/My School/2392/default.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/15/images/dlicon.png?rev=23" &gt;&lt;Query&gt;&lt;OrderBy&gt;&lt;FieldRef Name="FileLeafRef"/&gt;&lt;/OrderBy&gt;&lt;/Query&gt;&lt;ViewFields&gt;&lt;FieldRef Name="DocIcon"/&gt;&lt;FieldRef Name="LinkFilename"/&gt;&lt;FieldRef Name="Modified"/&gt;&lt;FieldRef Name="Editor"/&gt;&lt;/ViewFields&gt;&lt;RowLimit Paged="TRUE"&gt;30&lt;/RowLimit&gt;&lt;JSLink&gt;clienttemplates.js&lt;/JSLink&gt;&lt;XslLink Default="TRUE"&gt;main.xsl&lt;/XslLink&gt;&lt;Toolbar Type="Standard"/&gt;&lt;/View&gt;</property>
        <property name="InitialAsyncDataFetch" type="bool">False</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="ParameterBindings" type="string">
  &lt;ParameterBinding Name="dvt_sortdir" Location="Postback;Connection"/&gt;
            &lt;ParameterBinding Name="dvt_sortfield" Location="Postback;Connection"/&gt;
            &lt;ParameterBinding Name="dvt_startposition" Location="Postback" DefaultValue=""/&gt;
            &lt;ParameterBinding Name="dvt_firstrow" Location="Postback;Connection"/&gt;
            &lt;ParameterBinding Name="OpenMenuKeyAccessible" Location="Resource(wss,OpenMenuKeyAccessible)" /&gt;
            &lt;ParameterBinding Name="open_menu" Location="Resource(wss,open_menu)" /&gt;
            &lt;ParameterBinding Name="select_deselect_all" Location="Resource(wss,select_deselect_all)" /&gt;
            &lt;ParameterBinding Name="idPresEnabled" Location="Resource(wss,idPresEnabled)" /&gt;&lt;ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noitemsinview_doclibrary)" /&gt;&lt;ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noitemsinview_doclibrary_howto2)" /&gt;</property>
        <property name="DataSourceMode" type="Microsoft.SharePoint.WebControls.SPDataSourceMode, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">List</property>
        <property name="AutoRefreshInterval" type="int">60</property>
        <property name="AsyncRefresh" type="bool">False</property>
        <property name="HelpUrl" type="string" />
        <property name="MissingAssembly" type="string">Cannot import this Web Part.</property>
        <property name="XslLink" type="string" null="true" />
        <property name="SelectParameters" type="string" />
      </properties>
    </data>
  </webPart>
</webParts>'

$serverRelativeUrl = '/My School/2392/default.aspx'

$pageRelativeUrl='/My School/2392/default.aspx'


$clientContext= [Microsoft.SharePoint.Client.ClientContext,Microsoft.SharePoint.Client, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]

Foreach ($i in $pageRelativeUrl)
    
{
$siteurl='https://portal.peelschools.org/My School/2392'
 write-host "test 1 " $siteUrl
    $name = $i.Substring($i.IndexOf("/My School/2392")+9,$i.IndexOf("/Lists")-13)

    $siteurl = "$siteurl$name"
    write-host "test " $siteUrl
    $context=New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
    

    write-host "Reading file " $i

    $oFile = $context.Web.GetFileByServerRelativeUrl($i);

    $limitedWebPartManager = $oFile.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);

    write-host "getting xml reader from file"

    $xtr = New-Object System.Xml.XmlTextReader($localWebpartPath)

    [void] [Reflection.Assembly]::LoadWithPartialName("System.Text")

    $sb = new-object System.Text.StringBuilder

    try

    {

    if ($xtr -ne $null)

    {

    $xtr.Close()

    }

    #Add Web Part to catalogs folder

    write-host "Adding Webpart....."

    $oWebPartDefinition = $limitedWebPartManager.ImportWebPart($wpxml);

    $limitedWebPartManager.AddWebPart($oWebPartDefinition.WebPart, "Main", 0);

    $context.ExecuteQuery();

    write-host "Adding Web Part Done"

    }

    catch

    {

    write-host "Error while 'AddWebPartToPage'" $_.exception

    }
}