Top Menu

Wednesday, February 18, 2015

PowerShell - Download Images from Web

I had a requirement where I have to export user profile images from MySite 2013 and I end up writing following PowerShell script:

[Reflection.Assembly]::LoadFile( `
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Web.dll')`
  | out-null 

$FileName = "C:\Temp\ImagesURL.txt";
$Loc = "C:\Temp\Images\"
$ImageName = ""

$wc = New-Object System.Net.WebClient

$content = Get-Content $FileName
foreach ($line in $content)
{
    $Image = $Loc + $line.Substring($line.LastIndexOf("/") + 1)
    $url = $line
    
    Write-Host $url
    Write-Host $Image
    
    $wc.DownloadFile($url, $Image)
    
}

write-host "Finished successfully."

Friday, February 13, 2015

HTTP Request with Javascript

<script type="text/javascript">

var oReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");

function handler()
{
    if (oReq.readyState == 4 /* complete */) {
        if (oReq.status == 200) {
            document.getElementById("myDiv").innerText = oReq.responseText;
        }
    }
}

function SendRequest()
{
    var vURL = "http://IdolApp:12345/data/data.xml";

    if (oReq != null) {
        oReq.open("GET", vURL, true);
        oReq.onreadystatechange = handler;
        oReq.send();
    }
    else {
        window.console.log("AJAX (XMLHTTP) not supported.");
    }
}

</script>
<input type="button" id="btnSubmit" value="Go" onclick="SendRequest()"/>
<br/>
<div id="myDiv" >:o)</div>
Ref: https://msdn.microsoft.com/en-us/library/ie/ms535874(v=vs.85).aspx

Note:
Initialize the oReq with following to make it IE and Chrome compatible:
var oReq;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 oReq=new XMLHttpRequest();
}
else
{// code for IE6, IE5
 oReq=new ActiveXObject("Microsoft.XMLHTTP");
}

Monday, January 19, 2015

Using Charts.js with ASP.NET

Following is an example of using Charts.js library to display nice graphs & charts after providing the data via C#/ASP.NET:
  1. Download and unzip the Charts.js javascript library and add the entire folder into the ASP.NET project in the Visual Studio. 
  2. Add the refrence in head of ASP.NET:
    <script src="Scripts/Chart.js-master/Chart.js"></script>
    
  3. Add the following javascript in ASP.NET file with .NET variables (ChartLables, ChartData1 & ChartData2) that will be populated via C#:
    <script>
            var randomScalingFactor = function () { return Math.round(Math.random() * 100) };
            var lineChartData = {
                //labels: ["January", "February", "March", "April", "May", "June", "July"],
                labels: <% =this.ChartLabels %>,
                datasets: [
                    {
                        label: "Query Count",
                        fillColor: "rgba(220,220,220,0.2)",
                        strokeColor: "rgba(220,220,220,1)",
                        pointColor: "rgba(220,220,220,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(220,220,220,1)",
                        //data: [0, 1, 4, 6, 10, 8, 6]
                        data: <% =this.ChartData1 %>
                    },
                    {
                        label: "My Second dataset",
                        fillColor: "rgba(151,187,205,0.2)",
                        strokeColor: "rgba(151,187,205,1)",
                        pointColor: "rgba(151,187,205,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(151,187,205,1)",
                        //data: [28, 48, 40, 19, 86, 27, 90]
                        data: <% =this.ChartData2 %>
                    }
                ]
            }
            function DrawChart() {
                var ctx = document.getElementById("canvas").getContext("2d");
                window.myLine = new Chart(ctx).Line(lineChartData, {
                    responsive: true
                });
            }
        </script>
    
  4. Add the following HTML snippet to ASP.NET page where the chart will actually be displayed:
    <div style="width: 100%">
      <div>
        <canvas id="canvas" height="250" width="400"></canvas>
      </div>
    </div>
    
  5. On the C# file (.NET code behind), simply populate the data into the three C# variables and call the javascript function DrawChart(); on some ASP.NET button click:
    public string ChartLabels = null;
    public string ChartData1 = null;
    public string ChartData2 = null;
    
    this.ChartLabels = "['January', 'February', 'March', 'April', 'May', 'June', 'July']";
    this.ChartData1 = "[65, 59, 80, 81, 56, 55, 40]";
    this.ChartData2 = "[28, 48, 40, 19, 86, 27, 90]";
    
    //Call the Javascript function from C#
    Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "DrawChart()", true);
    
    

SharePoint 2007 - Hiding the Navigation

Scenario:
I had a small requirement to hide left and top navigation from a SharePoint 2007 Publishing Site on just one page (not the whole site).

Solution:
Two line javascrpit :o) in a hidden Content Editor Web Part, after identifying the IDs of the HTML elements which render the navigation.
<script type="text/javascript">

document.getElementById('onetIdTopNavBarContainer').style.display = 'none';
document.getElementById('LeftNavigationAreaCell').style.display = 'none';

</script>

Thursday, December 4, 2014

Reading XML with PowerShell

Here is an example of accessing XML data via PowerShell:

Script:
$xmlDocument = [xml] @"
<catalog>
   <book id="101">
      <author>Author1</author>
      <title>Title1</title>
      <genre>Genere1</genre>
      <price>45.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>This is a description1.</description>
   </book>
   <book id="101">
      <author>Author2</author>
      <title>Title2</title>
      <genre>Genere2</genre>
      <price>75.99</price>
      <publish_date>1998-7-15</publish_date>
      <description>This is a description2.</description>
   </book>
</catalog>
"@

$xmlDocument.catalog.book | Select-Object -Property author, title, price

Result:
PS C:\Users\tnaveed\Desktop> C:\Users\tnaveed\Desktop\CheckUser.ps1

author                         title                         price                        
------                         -----                         -----                        
Author1                        Title1                        45.95                        
Author2                        Title2                        75.99                        

Wednesday, December 3, 2014

Wednesday, November 5, 2014

SSIS - Remove Rows with Null Values

Scenario:
I was importing a CSV file with two columns (GPID, MemberOf) in a SSIS package when I found that GPID column might have null values and I don't need the entire row in my output if the GPID is null.

Solution:
One solution is to handle it via Script but why write code when Conditional Split is available.



Conditional Split has function ISNULL([Column Name]) which takes Column name to check if its null or not and returns the output if condition meets.

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...