Tuesday, April 11, 2017

Change the "Source" redirection of SharePoint New and Edit forms on Save and Cancel

On document.ready put this code:-

    var button = $("input[id$=SaveItem]");
    // change redirection behavior
    button.removeAttr("onclick");
    button.click(function() {
        var elementName = $(this).attr("name");
        var aspForm = $("form[id=aspnetForm]");
        var oldPostbackUrl = aspForm.get(0).action;
        var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl);
        var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "https%3A%2F%2Fint%2Eapac%2Esp%2Ewp%2Ecorpintra%2Enet%2Fsites%2F04400%2FDashboard%2520UAT%2FSitePages%2FProjects%2Easpx");

        if (!PreSaveItem()) return false;
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));

    });

Monday, April 10, 2017

2 level Cascading Lookup using SP Services

Pre-requisites :

  • JQuery Reference
  • SP Services Reference
Code :


<script type="text/javascript" src="../../SiteAssets/Scripts/jquery.2.1.3.min.js"></script>
<script type="text/javascript" src="../../SiteAssets/Scripts/jquery.SPServices-2014.02.js"></script>  
<script type="text/javascript">  
$(document).ready(function ()  
{  
$().SPServices.SPCascadeDropdowns({
  relationshipWebURL: "",
  relationshipList: "StateList",
  relationshipListParentColumn: "LookupCountry",
  relationshipListChildColumn: "State",
  parentColumn: "LookupCountryParent",
  childColumn: "LookupStateParent",
  debug: false
});
$().SPServices.SPCascadeDropdowns({
  relationshipWebURL: "",
  relationshipList: "CityList",
  relationshipListParentColumn: "LookupState",
  relationshipListChildColumn: "City",
  parentColumn: "LookupStateParent",
  childColumn: "LookupCityParent",
  debug: false
});

});
</script>

Explanations:-

  1. relationshipList - The list which has parent/child relationship.
  2. relationshipListParentColumn - The lookup column in relationshipList.
  3. relationshipListChildColumn - The column which is dependent on relationshipListParentColumn.
  4. parentColumn - The lookup column in parent list/master.
  5. childColumn - The lookup column in parent list/master list.
Analogy:-

CountryList

Title

India
UK
US

StateList

State LookupCountry

MP              India
Karnataka   India


CityList

City             LookupState

Bangalore    Karnataka
Ujjain          MP

ParentList - This is the list where I want cascading lookups i.e. on change of Country respective State should come, and further city should load for respective states.

Title      LookupCountryParent     LookupStateParent     LookupCityParent

New      India                                    Karnataka                      Bangalore

Wednesday, March 15, 2017

Use keyboard "Enter" to function same as the custom button.

$(document).on("keypress","#txtParam",function (e) {
// Keypress event of the textbox

 var key = e.which;
 if(key == 13)  // the enter key code
  {
    $("input[id ='btnSearch']").click(); // simulates the function of "btnSearch"  button on ENTER.
    return false;
  }
});

Monday, March 13, 2017

Move a SharePoint List Item to another List using SPD 2013 Workflow

The option to move the list item directly was present in SPD 2010 workflows. But in SPD 2013 workflow, we need to  -

1) Create Item in new list with referencing the same column in the current item as shown in the screenshot below.
2) Delete current item.






Calculate days difference between 2 dates sharepoint + JQuery

// This function calculates the difference between the Date field of SharePoint and the current Date.
function calculateDays()
{
var myDate =  $('input[title="Date"]').val();
var chunks = myDate.split('/');
var formattedDate = new Date([chunks[1],chunks[0],chunks[2]].join("/"));
var currDate = new Date(new Date().getTime());
console.log( Math.floor((currDate-formattedDate)/(1000*60*60*24)));
return Math.floor((currDate-formattedDate)/(1000*60*60*24));
}

JS Link Example: Display Images, Remove lookup's hyperlink, Change column names

(function () {

// function to give images in the column based on the values in o0ther columns.
    function GetTitleFieldDynamicUrl(ctx) {
        var days = ctx.CurrentItem.DaysInWorkshop;
        if(days == 0 || days == 1)
        return '<img src="/sites/03813/18/SiteAssets/Images/green.png"/>';
        if(days == 2)
        return '<img src="/sites/03813/18/SiteAssets/Images/yellow.png"/>';
    if(days >= 3)
        return '<img src="/sites/03813/18/SiteAssets/Images/red.png"/>';
    }
  // function to remove Lookup column's  link from the view
    function GetVSFieldDynamicUrl(ctx) {
    return ctx.CurrentItem.VehicleStatus[0].lookupValue;    }

function preTaskFormRenderer(renderCtx) {
       modifyColumns(renderCtx);      
    }

// function to change the names of columns in the views
    function modifyColumns(renderCtx)
    {
      var arrayLength= renderCtx.ListSchema.Field.length;
        for (var i=0; i < arrayLength;i++)
        {
           if(renderCtx.ListSchema.Field[i].DisplayName == 'OrderNo(D2S)')
             {
               var newTitle= "Order # (D2S)";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'PartsNo')
             {
               var newTitle= "Part #";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
if(renderCtx.ListSchema.Field[i].DisplayName == 'VehicleStatus')
             {
               var newTitle= "Status";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'SubStatus')
             {
               var newTitle= "Sub-Status";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'OutgoingDate')
             {
               var newTitle= "Outgoing Date";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'IncomingDate')
             {
               var newTitle= "Incoming Date";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'LicenseName')
             {
               var newTitle= "License #";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'JobCardNumber')
             {
               var newTitle= "Job Card #";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'CustomerName')
             {
               var newTitle= "Customer Name";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'DaysInWorkshop')
             {
               var newTitle= "Days in Workshop";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }
             if(renderCtx.ListSchema.Field[i].DisplayName == 'AgreementChoice')
             {
               var newTitle= "Acknowledgement";
               var linkTitleField = renderCtx.ListSchema.Field[i];
               linkTitleField.DisplayName = newTitle;
             }

         }
    }
    var fieldJSLinkOverride = {};
    fieldJSLinkOverride.Templates = {};
    fieldJSLinkOverride.ListTemplateType = 100;
    fieldJSLinkOverride.OnPreRender = preTaskFormRenderer;
    fieldJSLinkOverride.Templates.Fields = {
        'StatusFlag': { 'View': GetTitleFieldDynamicUrl },
        'VehicleStatus': { 'View': GetVSFieldDynamicUrl }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldJSLinkOverride);
})();

Thursday, February 2, 2017

Show images in calculated fields(once again)

=IF(DaysInWorkshop="1","<DIV><img src=/sites/05374/DowntimeDB/SiteAssets/Images/green.png></DIV>",IF(DaysInWorkshop="2","<DIV><img src=/sites/05374/DowntimeDB/SiteAssets/Images/yellow.png></DIV>",IF(DaysInWorkshop>="3","<DIV><img src=/sites/05374/DowntimeDB/SiteAssets/Images/red.png></DIV>")))

Clearing Person/Group Values

To clear out or empty the values in person or group columns, how you do it depends on if the column is single-value or multi-value. For ...