Email: info@zenconix.com

Gets a username from SharePoint’s User profile service



Visit Site:

Overview

Here is script of “How to get username from user id from Active directory”

 

[code language=”css”]
// get user name from id
function getUserFromId(userid) {
// var userid = _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = {
"accept": "application/json;odata=verbose"
};
$.ajax({
url: requestUri,
async: false,
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: onSuccess,
error: onError
});
function onSuccess(data, request) {
var Logg = data.d;
userLoginName = Logg.Title;
}
function onError(error) {
alert("error");
}
return userLoginName;
}
[/code]

Features

Specifications

Preview

Get the User ID of Active Directory name in SharePoint by Javascript



Visit Site:

Overview

How to get User Id from Site User if you have User’s active directory name
[code language=”css”]
//Get the id of user from AD name.
function getADNameId(adName) {
var UserData;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/SiteUsers?$Select=Id,Title&$filter=Title eq ‘" + adName + "’",
type: "GET",
async: false,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
console.log(JSON.stringify(data));
UserData= data.d.results;
},
error: function (err) {
console.log(JSON.stringify(err));
}
});
return UserData;
}
[/code]

Features

Specifications

Preview

Get selected element Id from Task List



Visit Site:

Overview

“Very BAD practices !!!” 

Sometimes situation comes that you don’t have any other options but you have to work on it and get work done.

If this situation comes then you are just searching for any possible solution (many times you know that it is wrong approch to do so…). 

I have come across the situation, wherein I wants to have Id of the ListItem from task list in client side scripting (JQuery).

Following is the code to get id of the ListItem of Task List.

[code language=”css”]
$(document).on("click", "#idHomePageEditItem", function () {
$("#Hero-WPQ2").next().next().next().children("tbody").children().each(function () {
if ($(this).hasClass("s4-itm-selected")) {
var selectedRecordId = $(this).children().eq(1).children().children().children().children().attr("liid");
alert(selectedRecordId );
}
}
}
[/code]

SelectItemFromTasklist

Features

Specifications

Preview

Write Secure Javascript for Project.



Visit Site:

Overview

Best Practices to Write Secure Javasctript Code.

javascript_logo

XSS

In order to evaluate the code for Cross-Site Scripting, usage of keywords outlined at https://www.owasp.org/index.php/JavaScript/Web_2.0_Keywords_and_Pointers so one need to avoid use of keywords given in above link

Some general Keywords which onc uses in coding

Keyword Security Concern

document.body

These elements help creating the dynamic content and may result in unsafe code. Complete file contents need to be checked to validate and make sure about security considerations. 

document.createElement

eval(

It may execute arbitrary code in the context of the page and hence can be gate for cross site scripting. If the code under eval is using user input then it must be redesigned as it pose a security risk.

location.href

Location of new URL provided by the user is vulnerable to security breach.

window.attachEvent

It can allow mouse events to be tracked even on minimized window. This has been reported on Internet Explorer and compromise even virtual keyboards. Please check the link

http://www.spider.io/blog/2012/12/internet-explorer-data-leakage/

window.location

Any intrusion that changes the window.location value in the website database can result in end user browsing a malicious site 

General Guidlines

1) It is expected that a var will be declared only once, and that it will be declared before it is used.

2) Events should not handled before defined.

3) Variables should not defined after being used.

4) Try to avoid Unused parameters and variables are found in the code.

5) Do not use Un-Escaped character sequence.

6) Do not use Un-Expected tokens  in the code.

Always use JSLint to varify your JavaScript File

JSLint is a JavaScript program that looks for problems in JavaScript programs. It does not prove that your program is correct. It just provides another set of eyes to help spot problems. For details refer: http://www.jslint.com/lint.html

General Errors in JSLint Scanning

Missing ‘use strict’ statement.

Solution –  Insert keyword ‘use strict’ before defining variables in javascript. The purpose of “use strict” is to indicate that the code should be executed in “strict mode”.

For more details visit here.

‘$’ was used before it was defined.

If using jquery in javascript file then it will show above error, because JSLint dont understand Jquery.

Solution – Write following line of code in start of your Javascript file

/*jslint browser: true*/

/*global $, jQuery */

Expected ‘;’ and instead saw ‘$’.

Solution– Function should be end with ‘;’
Ex
function someFunction() {
//some code
};

 Expected ‘===’ and instead saw ‘==’.

Solution– Try to use strict equal operator wheneven needed. If dont want to use strict equel operator then write following code snippet at the start of your Javascript code

/*jslint eqeq: true */

use knockout and any other js library

If want to use Knockout or any other library in the file, JSLint will show error. So need to define that libraries globaly.
Solution- Add following line of code at the start of Javascript file
/*jslint browser: true*/
/*global $, jQuery, alert, Swiper, ko*/
where ko and Swiper are js libraries which usign in the file

‘localStorage’ was used before it was defined.

If you are using LocalStorage to store and retrive data then it will show above error

solution – To resolve this error write following code at the start of javascript file. 
/*global localStorage: false, console: false, $: false */

Unexpected (

Dont use ‘(‘ when ever not needed

Ex– return(emi);
Solution-  return emi;

Unexpected use of typeof

typeof should be use without brackets ‘()’

Ex– typeof(someValue); // wrong method
Solution-  typeof someValue;

Unnecessary ‘else’ after disruption.

If using if- else loop, and returning some value in else loop then above error will be occure.

Ex-
     if( someCondition){
 //some code
        } else {
return 0;
       }

Solution – 

    if(someCondition){
       // somecode —
     }

return 0; 

Only one space should be there after } and else

 Ex-
 if( someCondition){
//Some code here
}
else{
return 0;
}
Solution-
 if( someCondition){
//Some code here
} else {
//Some code here
}

missing radix parameter

Ex- 

imageIndex = parseInt(id.substring(id.length -1))-1;

Solution

It always a good practice to pass radix with parseInt –

parseInt(string, radix)

For decimal –

parseInt(id.substring(id.length -1),10)

If the radix parameter is omitted, JavaScript assumes the following:

 

  • If the string begins with “0x”, the radix is 16 (hexadecimal)
  • If the string begins with “0”, the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)

Always should have ; after ending a line

Always should have ; after ending of ko function.

Don’t declare variables in a loop.

 

Features

Specifications

Preview

Object Oriented Javascript part-2



Visit Site:

Overview

Today we will see more concepts on the javascript

this article is continued article of yesterday’s one Object oriented javascript

Today we will see some more concept of object oriented javascript which are essential for writing script with OOP

First we will see To instantiate or not to be instantiate

Change in object literal will affect that object accross the entire script whereas in constructor function is instantiated and then changes is made to that instant, won’t affect any other instance

for example

[code language="javascript"]<br /><br />var myObjectLiteral={<br /><br />myProperty: 'this is a property'<br /><br />}<br />//alert this property<br />alert(myObjectLiteral.myProperty);<br />//this will alert as: 'this is a property'<br />now change this property<br />myObjectLiteral.myProperty='this is new property'<br />//alert this<br />alert(myObjectLiteral.myProperty);<br />//this will alert as: 'this is new property'  as we have expected<br />[/code]

Now create new variable and point towards the object, it will
have some changes

[code language="javascript"]<br />var myObjectLiteral = {<br />myProperty: 'this is my property'<br />}<br />alert(myObjectLiteral.myProperty);<br />// this will alert as: 'this is my property'<br />// take new variable now<br />var someObject = myObjectLiteral;<br />// Change property<br />myObjectLitral.myproperty = 'this is new property' ;<br />alert(someObject.myProperty);<br />&lt;strong&gt;//this will still alert : this is my property&lt;/strong&gt;<br />[/code]

There will not change for new variable

Now we will do same for constructor

[code language="javascript"]<br />var myObjectConstructor = function(){<br />this.myProperty = 'this is property';<br />// now instantiate constructor<br />var constOne = new myObjectConstructor();<br />// instantiate second Constructor<br />var consTwo = new myObjectConstructor();<br />// now we will alert one by one<br />alert(constOne.myProperty);<br />alert(constTwo.myProperty);<br />//this both will alert as : 'this is property'<br />}<br /><br />[/code]

so as expected this will return correct values but lets change myProperty for one of the instance

[code language="javascript"]<br />var myObjectConst = function(){<br />this.myProperty = 'this is property' ;<br />}<br />//instantiate constructor ;<br />var constOne = new myObjectConst();<br />//change in property now<br />constOne.myProperty = ' this is new ' ;<br />// instantiate second constructor<br />var constTwo = new myObjectConst();<br />//alert this<br />alert(constOne.myProperty);<br />//this will alert as 'this is new '<br />alert( constTwo.myProperty);<br />//but this will alert as: ' this is property '<br />[/code]

as you can see the differences in both of this , there is change in alerts when we change the properties of the constructor but it will not affects the previous one or entire script

Hope you understood this concept

Enjoy Javascript !!!

Features

Specifications

Preview

Object Oriented Javascript



Visit Site:

Overview

Hello All !

Today we will see how to start studying object oriented javascript

This will be just how to begin use javascript with great Object oriented approach

1) First How to create objects in javascript ?

there are two ways to create an object 1) Constructor Function 

                                                                      2) Literal function

We can declare this function as

1) Constructor function

function myObject(){ }

2) Literal Notation

var myObject={ };

Literal is preferable option for name spacing so javascript code doesn’t interface with other language scripting code on the same page or an application

Now we will see how to define method and properties in both of this object types

1) Constructor version

[code language=”javascript”]
function myObject(){
this.iAm = ‘an Object’ ;
this.whatIAm = function(){
alert(‘I am’ + this.iAm);
}
}
[/code]

2) Literal version

[code language=”javascript”]
var myObject={
iAm:’an object’,
whatIam: function(){
alert(‘I am’+this.iAm);
}
}
[/code]

Properties are variable created inside the object and methods are functions inside objects”

– To use properties, first you type the object it belongs to then for example for our case it will be

[code language=”javascript”]

myObject.whatIam

[/code]

this will return ‘an object’

-To use method

this is same just put parenthesis after it ; otherwise you will just return the referance to function and not what the actual function return so it will like

[code laguage=”javascript”]

myObject.whatIam()

[/code]

it will return ‘an object’

Now we will see what are the difference in the both of this

1) Constructor object has properties and method defined with keywords ‘this’ in front of it, whereas literal version doesn’t

2) In constructor object the properties/ methods have there ‘values’ defined after ‘=’ where in literal, they are define after “:”

3) Constructor function can have semicolon (‘;’) at end of each property / method whereas in literal if we have more than one method / property they separated by comma (‘,’)

4) Object declaration

for literal we can declare as

[code language=”javascript”]

myObject.whatIam();

[/code]

for constructor

[code language=”javascript”]

var myNewObject= new myObject();

myNewObject.whatIAm();

[/code]

Using a construction function

when we instantiate, then function will perform some basic operation

for example

[code language=”javascript”]

function myObject(){

this.IAm =’an object’ ;

this.whatIAm = function (){

alert("I am" +this.IAm);

};

};

[/code]

we also can pass the argument to function

for example

[code language=”javascript”]

function myObject(what) {

this.IAm = what ;

this.whatIAm= function (language) {

alert("I am " + this.IAm + "at" + language +"language" );

};

};

[/code]

To call above function, we can have

[code language=”javascript”]

var myNewObject =  new myObject(‘an object’);

myNewObject.whatIam(‘javascript’);

[/code]

then this will alert

“I am an object at javascript language”

Features

Specifications

Preview