Wednesday, October 22, 2014

SQL Server Integration Service - NULL Values

Scenario:
I had a scenario where I will get the AD export in CSV format with two columns (samaccountname, GUID) and I had to check if the GUID is null then put the value "0" instead of null value.

Solution:
1. Load the CSV file into SSIS project
2. Make data connection.
3. Add the script component.

4. Edit the Script and overwrite the following function:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    if (Row.GUID_Input.Length > 0)
    {
        Row.GUID_Output = Row.GUID_Input;
    }
    else
    {
        Row.GUID_Output = "0";
    }

}

Wednesday, October 15, 2014

Export AD Users from a Group via PowerShell

I have to write this script recenely to export user and their few properties via PowerShell.
Import-Module ActiveDirectory

$ADGroup = "GroupName"
write-host "Accessing all users from group:" $ADGroup
Get-ADGroupMember -identity $ADGroup -recursive | select distinguishedName,name | Export-csv -path C:\Temp\users.csv -NoTypeInformation
write-host "Done."

Watch For:
You might see the following error:
Get-ADGroupMember : The size limit for this request was exceeded

Reason:
Default number of objects returned from AD are limited to 5,000 by default.

Resolution: Open the config file for ADWS
C:\Windows\ADWS\Microsoft.ActiveDirectory.WebServices.exe.config
and add the following line under <appsettings> tag
<add key="MaxGroupOrMemberEntries" value="25000">

Real Work Happens Here

Yup...Real work happens here.

Tuesday, October 14, 2014

SharePoint 2007 - Delete Global Navigation using PowerShell

Scenario:
Recently support team at my client found out that for some reason too many links (more than 1000) got created under a heading node in the Global Navigation in one of our SharePoint 2007 portals and it is making the whole site collection slow.

When I try to delete the group node from the UI, it gets deleted but the child nodes become the parent nodes. Funny....but not so funny when it is in production environment. So I wrote following the script in Powershell 2.0 for SharePoint 2007 to kill the parent node (and it will automatically delete all the child nodes under it).

Solution:
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Sharepoint”)

$siteURL = "https://sharepoint2007portal"
$spSite = new-object Microsoft.Sharepoint.SPSite($siteUrl)
$spWeb = $spSite.RootWeb

Foreach($node in ($spWeb.Navigation.TopNavigationBar))
{
    
    if ($node.Title -eq "MyHeading")
    {
        write-host "Node: " $node.Title

        //this will delete the node and its child
        $node.Delete() 

        write-host "Deleted successfully."
    }
    
}
$spWeb.Dispose() 

write-host "Done."

Friday, October 3, 2014

Content Approval on Multiple Lists

Scenario:
One of my colleague wanted to activate the Content Approval on a couple of lists and libraries within a site and we know that going to the settings of 25 libraries for the same setting is easy but a pain.

Solution:
This can be done through Powershell.
Add-PSSnapin "Microsoft.SharePoint.PowerShell"

$siteUrl="http://www.SharePointBlue.com"
$lists = @("Documents","New Documents","ListA")


$site=Get-SPSite $siteUrl
$web=$site.RootWeb

ForEach ($listName in $lists) 
{
    try
    {
        $list=$web.Lists[$listName]
        $list.EnableModeration = $false
        $list.Update()

        $MSG = $Now.ToString() +  " | Content Approval activated on list: " + $listName
        write-host $MSG
    }
    catch [system.exception]
    {
        $Now = [System.DateTime]::Now
        $MSG = $Now.ToString() +  "Exception: " + $_.Exception.Message
        write-host -f red $MSG
    }
}

Thursday, October 2, 2014

Real Quick JSON Example

JSON is better to handle than XML. Was using it in a project so thought to post here,
<div id="dvResult">Loading...</div>

<script>
var data = [
    {"firstName":"Michael","lastName":"Jackson","SSN":"123456789"}, 
    {"firstName":"Barack","Obama":"Smith","SSN":"124456789"},
    {"firstName":"Peter","lastName":"Jackson","SSN":"125456789"}
];

var x = '';

for (i = 0; i < data.length; i++)
{
    x = x + data[i].firstName + " " + data[i].lastName + " " + data[i].SSN + "<br/>";
}

document.getElementById("dvResult").innerHTML = x;

</script>

Bulk Site Collection Deletion using PowerShell

Description:
Following powershell reads the list of site collections from the file and deleted them:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

function WriteLog
{
    Param([string]$message, [string]$logFilePath)
    Add-Content -Path $logFilePath -Value $message
}

$LogFile = "G:\DeletedSites.log"
$ListFile = "G:\List.csv"

$Sites = Import-CSV $ListFile

ForEach ($S in $Sites) 
{
    
    try
    {  

        $URL = $S.URL

        $Now = [System.DateTime]::Now
        $MSG = $Now.ToString() +  " | Deleting site: " + $URL
        write-host $MSG
        WriteLog $MSG $LogFile
        
        Remove-SPSite -Identity $URL -GradualDelete -Confirm:$false


    }
    catch [system.exception]
    {
        $Now = [System.DateTime]::Now
        $MSG = $Now.ToString() + "Exp: " + $_.Exception.Message
        write-host -f red $MSG
        WriteLog $MSG $LogFile
    }

    $Site = $Null
}

write-host "Done."

Wednesday, October 1, 2014

Get Claims Programmatically

And that's how you get all the claims for a user programmatically in .NET. Almost similar to my older post:
public void GetClaims()
{

    System.Security.Principal.IPrincipal ipl = System.Web.HttpContext.Current.User;
    System.Security.Claims.ClaimsIdentity claimsIdentity = (System.Security.Claims.ClaimsIdentity)ipl.Identity;
    foreach (System.Security.Claims.Claim oClaim in claimsIdentity.Claims)
    {
        Response.Write("ClaimType [" + oClaim.Type.ToLower() + "] has value [" + oClaim.Value + "]");
    }

}

Get Query String from JavaScript

I use this javascript function in every other project and every time I have to go to the internet and search for it. I am just too lazy to look and copy it from the old projects. So I am copying it from the internet to save it in my personal source code repository.
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var strValue = getParameterByName('Value');

document.write(strValue);

Knowledge Chat Bot (No Code)

Recently my customers was looking for a solution where on-field guys can search for answers related to their tasks and it was a perfect scen...