Thursday, May 15, 2014

Modal Popup- close sharepoint page and sharepoint popup page in code behind

Below is the code to close a SharePoint popup window on button click or any event from code behind

// Close if dialog

HttpContext context = HttpContext.Current;

if (HttpContext.Current.Request.QueryString["IsDlg"] != null)



{
 
context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup()</script>");




}
 
else

SPUtility.Redirect(SPContext.Current.Web.Url, SPRedirectFlags.UseSource, HttpContext.Current);




Source attribute is a Query string which holds the url of the source page from where the page is usually called .This is default behavior of SharePoint. We can use this to navigate when the popup is closed and redirect to same source page as shown above

Eg: http://webpalication:1111?Source=http:wwww.google.com


 

Friday, May 9, 2014

Add Sharepoint Navigation links with audience targeting through powershell

Below is the powershell command to add Navigation links in SharePoint.This was tested in SharePoint 2013 environment.This script will check if the link is present in the site if not it will add the links along with audience targeting



$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }

if ($snapin -eq $null) {

Write-Host "[INIT] Loading SharePoint Powershell Snapin"

Add-PSSnapin "Microsoft.SharePoint.Powershell"


}

 
 
$Web = Get-SPWeb http://testWebapplication:7777

$TopNav = $Web.Navigation.TopNavigationBar

Write-Host -ForegroundColor Green "Creating Navigation links ......................."



$Heading = $TopNav | where { $_.Title -eq "Link1" }

if($Heading -eq $null)



{
 
$newLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList @("Link1", "/Lists/CreateNewPublication/DocProjects.aspx");

$Heading = $Web.Navigation.TopNavigationBar.AddAsLast($newLink)

$Web.Update()





}

 
 
$Heading = $TopNav | where { $_.Title -eq "Search" }

if($Heading -eq $null)


{
 
$newLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList @("Search", "/SitePages/Search.aspx");

$Heading = $Web.Navigation.TopNavigationBar.AddAsLast($newLink)

$Web.Update()


}
 
$Heading = $TopNav | where { $_.Title -eq "Google" }

if($Heading -eq $null)


{
 
$newLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList @("Google", http://www.google.com);

$Heading = $Web.Navigation.TopNavigationBar.AddAsLast($newLink)

$Web.Update()


}
 
$Heading = $TopNav | where { $_.Title -eq "Yahoo" }

if($Heading -eq $null)



{
 
$newLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList @("Yahoo", http://yahoo.co.in);

$Heading = $Web.Navigation.TopNavigationBar.AddAsLast($newLink)

$Heading.Properties["Audience"] = ";;;;" + "IK,IL,R,C"

$Heading.update()

$Web.Update()





}
 
$Heading = $TopNav | where { $_.Title -eq "Reports" }

if($Heading -eq $null)



{
 
$newLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList @("Reports", "/SitePages/Reports.aspx");

$Heading = $Web.Navigation.TopNavigationBar.AddAsLast($newLink)

$Heading.Properties["Audience"] = ";;;;" + "PM,TL,IL,C"

$Heading.update()

$Web.Update() 



}
 
  $Heading = $TopNav | where { $_.Title -eq " Help" }

if($Heading -eq $null)

{
 
$newLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList @(" Help", "/Lists/HelpLibrary ");

$Heading = $Web.Navigation.TopNavigationBar.AddAsLast($newLink)

$Web.Update()


}
 
Write-Host -ForegroundColor Green "Navigation link creation completed"

Pause