Wednesday, December 31, 2014

C# code for Hashing String with SHA256



In this blog I will walk you through how to hash string using SHA256 HashAlgorithm, below is the detail code with inline comments,
   
                    //string which needs to be hashed with SHA256 algorithm             
            string Concatenatedstring = “StringToBeHashed”
                       
                     //Variable declared to store hashed string
            string stringhash = string.Empty;

             //SHA256Managed class instantiated

     SHA256Managed sha256managed = new SHA256Managed();

      // Convert string to bytes

            byte[] utf8 = Encoding.UTF8.GetBytes(concatenatedstring);

             // compute hash value from bytes
            var HashValue = sha256managed.ComputeHash(utf8);

             // get string value
            foreach (byte b in HashValue)
            {
                stringhash += b.ToString("X2");
            }




Saturday, December 13, 2014

Get Current User name using JQuery in sharepoint 2013

Here is the code to fetch current user name ( logged in user name ) using Jquery , this can be user on content editor webpart or list form .

things to take care before u use below code
1. download jquery-1.4.2.min.js and jquery.SPServices-0.5.7.min.js and
2. upload both the downloaded files to style library under new folder named as js.


<script language="javascript" src="../../Style Library//js/jquery-1.4.2.min.js" type="text/javascript"></script>  
<script language="javascript" src="../../Style Library/js/jquery.SPServices-0.5.7.min.js" type="text/javascript"></script>
<script type="text/javascript">
 
//to make sure above added script links are loaded on page
  $(document).ready(function() {
 
// to get formlabel control
  if (document.getElementById("formlabel") != null)
{

document.getElementById("formlabel").innerText =  $().SPServices.SPGetCurrentUser(
{fieldName:"Title",
debug:false
});
}
</script>

formlabel is html label , innertext of label is set to current logged in user name programmatically using above code.

Sunday, December 7, 2014

Programmatically Download sharepoint file


      Here I wil talk about how to download sharepoint file (image/Document/Video) on button click, SharePoint provides you inbuilt solution using STSNavigate function which helps to navigate to system page download.aspx and download.aspx contains code to download file


1. If using server side code


downloadButton.OnClientClick = "STSNavigate('" + SPContext.Current.Web.Url + "/_layouts/15/download.aspx?SourceUrl=" + fileURL + "' ); return false;";


In above line of code STSNavigate will do magic of giving user save as option of specified file URL, also here we are using system page download.aspx


2. If using client object model

if below code the only thing you need to do is to form fileURL.
$(‘#<%=btnDownload.ClientID%>’).click(function (e) {
STSNavigate(‘<%= SPContext.Current.Web.Url %>/_layouts/15/download.aspx?SourceUrl=’ + fileURL);
return false;
})