Monday 5 January 2015

Null Reference Exception While Executing the Webpart



In SharePoint some times after the deployment the Webpart may shows the below error.




If we look into the log, the following error message will be logged:
Error while executing web part: System.NullReferenceException: Object reference not set to an instance of an object.   
at System.Xml.Xsl.XslCompiledTransform.Load(MethodInfo executeMethod, Byte[] queryData, Type[] earlyBoundTypes)   
at Microsoft.Xslt.STransform.GetCompiledTransform()   
at Microsoft.SharePoint.WebPartPages.BaseXsltListWebPart.LoadXslCompiledTransform(WSSXmlUrlResolver someXmlResolver)
at Microsoft.SharePoint.WebPartPages.DataFormWebPart.GetXslCompiledTransform()   
at Microsoft.SharePoint.WebPartPages.DataFormWebPart.PrepareAndPerformTransform(Boolean bDeferExecuteTransform)

Solution:
This is because of one windows update. The update Windows KB2844286 is causing this error.
So uninstall the Windows KB2844286 update. Then the webpart will work.
To Uninstall: Control PanelàPrograms And Featuresà View installed updates àSecurity update for Microsoft windows Windows KB2844286 (under Microsoft Windows category)àUninstall

Script to Remove Duplicate Registration of Event Receiver



# List all the Events registered
$weburl = "http://SharepointWeb"
$spweb = Get-SPWeb $weburl
$spList = $spWeb.Lists["ListName"]
$spList.EventReceivers | Select Name, Assembly, Type

# Remove the Event Registration at index 0
$spList.EventReceivers[0].delete()

Setting Default Value to the Person/Group Column in New/Edit form



By using JavaScript the default value for a person or group column can be set. The below are the steps
(i) Add content Webpart
(ii)Inside that put the following script
(This script will fetch the current username and display it in the people picker which is mentioned in the script)
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(GetUserLoginName, "sp.js");
 var context = null;
    var web = null;
    var currentUser = null;
    var currentUserId=''
function GetUserLoginName() {
 context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(onSuccessMethod, onFaiureMethodl);
}
function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
           AddCurrentUserToPP(userObject.get_title());
    }
  
    function onFaiureMethodl(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }

function AddCurrentUserToPP(LoginName)
{
var pickerNo = 1;
var pp = getPickerImputElement(pickerNo);
if(pp != null)
{
pp.innerHTML = LoginName;
}
}

function getPickerImputElement(pickerNo)
{
var result = '';
var divs = document.getElementsByTagName('DIV');
var j = 0;
for(var i=0; i < divs.length; i++)
{
if(divs[i].id.indexOf('UserField_upLevelDiv') > 0)
{
j++;
if(j == pickerNo)
{
result = divs[i];
break;
}
}
}
return result;
}</script>
(iii) Stop Editing

Script to Add Header and Links to Quick Launch



Add Header

$w = get-spweb http://SharepointWeb
$nav = $w.Navigation.QuickLaunch
$head=$nav|where { $_.Title -eq "Lists" }
$newLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode(“Custom Home", "/Site/SitePages/Home.aspx")
$nav.Add($newLink,$head)

Add Links

$w = get-spweb http://SharepointWeb
$nav = $w.Navigation.QuickLaunch
$head=$nav|where{$_.Title -eq "Custom Header"}
$newLink = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Home", "http://SharepointWeb/SitePages/Home.aspx")
$head.children.AddAsFirst($newLink)

Script to Find the Master Page Used



$site = Get-SPSite "http://site.domain.com"
$webs = $site.AllWebs

$webs | % {
    $obj = new-object System.Object
    $obj | Add-Member -type NoteProperty -name WebUrl -value $_.Url
    $obj | Add-Member -type NoteProperty -name Master -value $_.MasterUrl
    $obj | Add-Member -type NoteProperty -name CustomMaster -value $_.CustomMasterUrl
    $obj
} | Export-CSV -NoTypeInformation -Path "C:\folder\MasterPages.csv"