Sharepoint User Profile context Filter webpart using powershell


In sharepoint application very often we use listview webpart. Sometme on top of the current view we need to use filters for the list data.

In one of article I have explained how to implement the commonly used Query string webpart using powershell scripting.

In this article I will consolidate the details which deals with a User profile filter. It is almost same as of the other OOTB filter other than the highlighted parts.

Following piece of code is ready to use for the same purpose

# 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-ObjectMicrosoft.SharePoint.Portal.WebControls.UserContextFilterWebPart;
$queryPart.FilterName="<Filter Name>";
$queryPart.ProfilePropertyName="<USER PROFILE PROPERTYC NAME>";

   $queryPart.ValueKind=”ProfileValue”;


$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.
Comments (0)

Skip to main content