Wednesday, June 25, 2014

JavaScript: Easy way to Cache objects in Local Storage/ Client Browser in JavaScript

Here is a easy way to store and retrieve  your java script objects ( even more than 4KB) in the local /Client browser . I'm using jStorage for this method and jStorage supports storing Strings, Numbers, JavaScript objects, Arrays and even native XML nodes. jStorage also supports setting TTL values for auto expiring stored keys and - best of all - notifying other tabs/windows when a key has been changed or publishing/subscribing to events from the same or another tab/window, which makes jStorage also a local PubSub platform for web applications.


 < script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js">
< script src="http//code.jquery.com/jquery-1.9.1.min.js">
< script src="jstorage.js">


< script type="text/javascript">
var testObject = "test123"; /* Sample Data. You can fetch the objects from Server and store it*/

// Check if "key" exists in the storage
var value = $.jStorage.get("key");
if(!value){
    $.jStorage.set("key",testObject);
}
< /script >


Here you go, Now its easy to store to the cache with bigger data. hope this helps.
Article Source and .js file available at : www.jstorage.info

Happy Browsing!
Relax..

Thursday, June 19, 2014

SharePoint 2013 - Client Object Model - Document Library Author error and SP.UserProfiles.PeopleManager Error Handling


If you are getting the PeopleManager as object referance error, then try this method.

Try to load the script file above the calling function.
< script src="/_layouts/15/sp.userprofiles.js" type="text/javascript" >

Try to wrap the function inside the SP.SOD.executeFunc. This will make sure to load the profile manager.

   SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
   SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {
    var targetUser = "Domain\\" + createdBy.split('\\')[1]; //Splitting to get the userID
    var clientCtx = new SP.ClientContext.get_current();
    var peopleManager= new SP.UserProfiles.PeopleManager(clientCtx );
    var i = peopleManager.getPropertiesFor(targetUser)
    //This is not yet finished till you execute the query.
   ctx.load(i);
    ctx.executeQuery(
    function () {
        alert("Display Name:" + i.get_displayName());
    } , function (sender, args) {
        alert(JSON.stringify(args));
    });

   });

});




If you are not able to get Author field in Document Library , try this field Created_x0020_By. This will give you the value as domain\\userid. I have splited here to get the exact user ID. (I'm not sure to get the display name from this. tried to fetch from user profile. bu the function was loading post as async. So opted for 2nd option using WF)

Happy Browsing!
Relax..

Tuesday, June 17, 2014


SharePoint 2013- Get/Fetch SharePoint User Details using JavaScript/Client Object Model

To fetch the current user details in the SharePoint using client object model, Add a new Content Editor on your page and insert the below code. This will display the user id, name and email of the current logged in user.

< script type="text/javascript" >
//Delay until the page loads
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.QuerySucceeded), Function.createDelegate(this,this.QueryFailed));
}

function QuerySucceeded() {
    document.getElementById('userLogin').innerHTML = currentUser.get_loginName();
    document.getElementById('userName').innerHTML = currentUser.get_title();
    document.getElementById('userEmail').innerHTML = currentUser.get_email();
}

function QueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

< / script >
<!-- Display on the screen.-->
< div  >Current User Details:
    < span id="userLogin"></ span >
    < span id="userName"></ span >
    < span id="userEmail"></ span >
< /div  >


PS: White Space are added in the HTML tag to avoid the blog to hide the controls. Please clear the white space on the tags to use at your end.


Article Source
Happy Browsing!
Relax..
 

Monday, June 2, 2014

SharePoint 2010/2013 - Today as Calculated Column - Used for date age calculation

 

SharePoint does not allow the Calculated field with function like [Today] or [Me] , And when we required to calculate a age of an issue created from today's date, this will be an issue.  Here is  a simple way to fix this issue

  1. Add a new column to list with Today as Column name and select Date Time as Type.
  2. Select the default value as Today's date and save it 
  3. Create  a new column as Age and select type as calculated field. Use the below formulate to get the date difference. 
  4.         =IF([Created]="","NA",Today-[Created]) 
  5. Select the data type returned from this formula as Number and set number of decimals to 0 and save it.
  6. Now delete the Today columns that is created from step 1. ( if you don't delete this , answer will be based on this column which will be empty and you'll get a -ve value).
  7. Now your list display difference as the age in days.
Here , you can use any other date column that you have created. This formula will give you the result as difference between today and created date. EX: Created Date = 5/1 and Today is 6/2, then Age = 32.  And for custom columns if no date is present, this will display NA.


PS: The calculated fields will not auto refresh every day. For auto refresh, it has to go through custom code or timer job.

Happy Browsing!
Relax..