Last active
April 25, 2017 07:58
-
-
Save durgesh97025/721b2cb0f4b46cc25634b6de287309ab to your computer and use it in GitHub Desktop.
An Js file for SharePoint for speedy development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof console == "undefined") { this.console = {log: function() {}}; }//If browser doesn't support console.log | |
//OTO = One Time Object | |
var rClientContext;//A context OTO used everywhere | |
var rWeb;//A web OTO used | |
var rCurrentUser;// A user OTO | |
$(document).ready(function(){ | |
rClientContext = SP.ClientContext.get_current(); | |
rWeb = rClientContext.get_web(); | |
rCurrentUser = rWeb.get_currentUser(); | |
}); | |
var extension = { | |
//Get The Field input Control by FieldName of List/Library | |
GetFieldControl: function(fieldName) { | |
var ctrl = $.GetFieldControls(fieldName) | |
if (ctrl.length > 1){ | |
$.each(ctrl, function(i,v){ | |
//console.log(v); | |
if (v.nodeName == "SELECT" || v.nodeName == "INPUT"){ | |
ctrl = $(ctrl[i]); | |
//console.log(ctrl); | |
return; | |
} | |
}); | |
} | |
return ctrl; | |
}, | |
GetFieldControls: function(fieldName) { | |
var ctrl = $("[title='"+fieldName+"']"); | |
if (ctrl.length< 1){ | |
ctrl = $("[ID^='"+fieldName+"']");//Start With | |
} | |
if (ctrl.length < 1){ | |
ctrl = $("[title~='"+fieldName+"']");//Word in it | |
} | |
if (ctrl.length< 1){ | |
ctrl = $("[title*='"+fieldName+"']"); | |
} | |
return ctrl; | |
}, | |
//Set The Value for People Picker Control by email address | |
SetValueForPeoplPicker: function(fieldName,email){ | |
var d = $.Deferred(); | |
var requestorCtrl = $.GetFieldControl("Requestor") | |
var peoplePickerDiv = requestorCtrl[0]; | |
var peoplePickerEditor = $(requestorCtrl[1]); | |
SP.SOD.executeFunc('clientpeoplepicker.js', 'SPClientPeoplePicker', function () { | |
var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv.id]; | |
peoplePickerEditor.val(email); | |
spPeoplePicker.AddUnresolvedUserFromEditor(true); | |
d.resolve(); | |
}); | |
return d.promise(); | |
}, | |
//A utility function returns Email from Login Name | |
GetEmailFromLoginName: function(loginName){ | |
var temp1 = loginName.split('|') | |
var currentUserEmail = temp1[temp1.length-1]; | |
return currentUserEmail; | |
} | |
}; | |
$.extend(extension); | |
function log(txt){ | |
console.log(txt); | |
} | |
//A utility function to Disable Field if not empty | |
function DisableFieldIfFilled(fieldName){ | |
var field = $.GetFieldControl(fieldName); | |
if (field.val()){ | |
field.attr("disabled","disabled"); | |
} | |
} | |
//A utility function to disable field | |
function DisableField(fieldName){ | |
var field = $.GetFieldControl(fieldName); | |
field.attr("disabled","disabled"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment