Open List attachments in new tab/window

Hello Floks,

Recently one my client is looking for solution in which all the list item attachment should be open in new tab or window as they have compare more than one files simultaneously.

There is a very easy solution to make it feasible. Open you List Edit/Disp Form and add below JavaScript inside it.

 $('#idAttachmentsTable a').attr('onclick','');
 $('#idAttachmentsTable a').attr('target','_blank');

Above code remove On Click event from all of the list attachment and add target as blank.

Enjoy.. 🙂 Happy Coading… 🙂 🙂

USE OF DEFERRED OBJECT IN SHAREPOINT JSOM

In previous post I explain use of Deferred Object in SharePoint REST API. In this post I’m going to explain use of Deferred object in JSOM/CSOM.

To use client context in the JavaScript you need to as below files in your page or masterpage as reference which is available in /_layouts/15/ folder.

  • inheritMicrosoftAjax.js
  • sp.runtime.js
  • sp.js

JQuery Deferred object can be used here too to make things simple, we have created a small example for that too.

functionToCall().done(function (returnedValue) {
     // do something with the returned value
});

function functionToCall() {
     var dfd = $.Deferred(function () {
     var valueToReturn = clientContext.get_web();
     clientContext.load(valueToReturn);
     clientContext.executeQueryAsync(function () {
               dfd.resolve(valueToReturn);
          },
          function (sender, args) {
               //throw an error
          });
     });
     return dfd.promise();
}

Hope this will help you and save your time..

Happy Coading… 🙂

Thanks, Jay Doshi

Use of Deferred Object in SharePoint Rest API

I have one issue in the project in which I have to update two lists item. Once first list item would updated then and then another list item would be updated. I have done Googling and get to know that we can handle two asynchronization call once first is executed. JavaScript API offers conecpt of Deferred Object which supports this type of functionality.

To communicate with SharePoint, developers mostly use SharePoint Rest API for better speed and performance.

The SharePoint Rest API can be used by inheriting the SP.RequestExecutor.js which available at the “…/_layout/15/” folder of SharePoint.

When there is requirement in which first asynchronous call is dependent upon another asynchronization call’s output at that time you can use concept of Deferred Object in JavaScript. For that we will need to control the calls sequences. But as we all know the fact that JavaScript is asynchronous and the flow of asynchronous calls cannot be controlled.

The JQuery Deferred object maintains an asynchronous call and it returns a Promise object when the call is complete.

The Promise object has two main methods to use,

done(fnSuccess, fnError, fnInProgress)
then(fnSucess)

I have created a small example of using JQuery Deferred object as shown below,

functionToCall().done(function (returnedValue) {
     // do something with the returned value or make another call here
});

function functionToCall() {
     var dfd = new $.Deferred();
     var executor;
     executor = new SP.RequestExecutor(appweburl);
     executor.executeAsync({ 
                             url: queryUrl, 
                             method: "GET", 
                             headers: {"Accept": "application/json; odata=verbose"},
                             success: onSuccess,
                             error: onError
                           });
     
     function onSuccess(data){
          var myVar = data.d.results;
          dfd.resolve(myVar);
     }

     function onError(data, errorCode, errorMessage){
          dfd.reject('Failed to load due to : '+errorMessage);
     }
return dfd.promise();
}

Additional things which needs to be consider while working with Deferred object:

  • Make sure the success and error handler are inside the async function so that the deferred object (dfd) remains global to them.
  • Both success and error handler should have deferred resolve and deferred reject methods.

Enjoy coading.. 🙂

Regards, Jay Doshi

Populate Current Loggedin User in People Picker

In one of my project we need populate current logged in user in People Picker by default. I have implemented below code to do it.

Add 1.7.2.jquery.min.js and jquery.SPServices-2014.02.min.js file in the master page or page level.

 
$(document).ready(function() { 
 ExecuteOrDelayUntilScriptLoaded(loadCurrentUser, "sp.js"); 
});

function loadCurrentUser() { 
 var userName = $().SPServices.SPGetCurrentUser({ fieldName: "UserName", debug: false});
 $('h3:contains("PeoplePickerFieldName")').closest('tr').find('div[title="People Picker"]').html(userName);
 $('img[title="Check Names"]').trigger('click'); 
} 

** Replace “PeoplePickerFieldName” with your people picker field name.

** Replace “userName” with your desired user’s user name to add other user in peoplepicker.

Let me know if you face any challenges.

Happy Coding.. 🙂

Regards, Jay Doshi