다음을 통해 공유


Sharepoint Query String Filter webpart using powershell

Despite of some limitations often we need to deal with filter webparts in sharepoint application. Query string filter is is very common and widely used. And sometime it is required to automate the entire process, in order to do so powershell scripting is must.

Here I will just explain using powershell scripting how to add

1. A Listview webpart with a Query string filter webpart on a sharepoint page

2. Establish the connection between the two webpart

 # SPWeb context 
$web = Get-SPWeb   <site url>  
 $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web) 
 #Get the webpart manager
 $oWebPartManager = $web.GetLimitedWebPartManager(  <page url>  , [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
 #Get the list
 $list = $web.Lists[  <List Name>  ];
 #Instantiate listview webpart and added to the page
 $listVWP = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart;
$listVWP.Title =  <Listview webpart Title.  ;
$listVWP.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None; 
$listVWP.Visible = $true;
$listVWP.EnableViewState = $true;
 $listVWP.ListName =   <List Name>;  
 $listVWP.ViewGuid =   <View Guid>  
$listVWP.WebId = $list.ParentWeb.ID;
$listVWP.ListId = [System.Guid]$list.ID;

 $oWebPartManager.AddWebPart($listVWP, "Main", 0);

 $oWebPartManager.SaveChanges($listVWP);

#Instantiate and add the Query webpart and set the connection

 $queryPart = New-Object Microsoft.SharePoint.Portal.WebControls.QueryStringFilterWebPart;
$queryPart.FilterName="  <Filter Name>  ";
$queryPart.QueryStringParameterName = "  <query string parameter name>  "; 
$queryPart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None; 
$oWebPartManager.AddWebPart($queryPart, "Main", 0);
$oWebPartManager.SaveChanges($queryPart);
$provWPCPs = $oWebPartManager.GetProviderConnectionPoints($queryPart) ; 
$conWPCPs = $oWebPartManager.GetConsumerConnectionPoints($listVWP) ;
foreach($provWPCP in $provWPCPs){
 
if($provWPCP.ID -eq "ITransformableFilterValues"){
 $providerCon = $provWPCP;
 }
 }

 foreach($conWPCP in $conWPCPs){

 if($conWPCP.ID -eq "DFWP Filter Consumer ID"){
 $consumerCon = $conWPCP;
 }
 }
$trans = New-Object Microsoft.SharePoint.WebPartPages.TransformableFilterValuesToParametersTransformer
$trans.ConsumerFieldNames = new-object System.string("  <List Field on which Filter to be applied>  ");
$trans.ProviderFieldNames = new-object System.string(" <Filter Name> ");
    
$newConn = $oWebPartManager.SPConnectWebParts($queryPart, $providerCon, $listVWP,$consumerCon, $trans)
$oWebPartManager.SPWebPartConnections.Add($newConn);
 $PublishingPage = $PubWeb.GetPublishingPage("  <Page Url>  ");
$PublishingPage.CheckIn("Auto deployed - intermediate");

# Fix for list web-part sorting/paging issue

$PublishingPage.CheckOut();
     
$page = $web.GetFile("  <Page Url>  "); 

 $wpmgr = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
        
 foreach($wpInstance in $wpmgr.WebParts)
 {
 if ($wpInstance.ToString() -eq "Microsoft.SharePoint.WebPartPages.XsltListViewWebPart")
 {
 $wpmgr.SaveChanges($wpInstance);
 }
 }

$page.Update();

# End fix for list web-part sorting/paging issue

$PublishingPage.CheckIn("Auto deployed - final");
$PublishingPage.ListItem.File.Publish("Auto deployed - final");
  
 Now we are done with the scripting part once we run this script it will add the Listview, 
 Query string web part on the page and set the required connection.
 In url if we pass the query string value the given list view will be filtered based on that.