Skip to content

Instantly share code, notes, and snippets.

@rheid
Created November 8, 2016 15:56
Show Gist options
  • Save rheid/00ffa1ab3cf572c8e9d878efb0596fc6 to your computer and use it in GitHub Desktop.
Save rheid/00ffa1ab3cf572c8e9d878efb0596fc6 to your computer and use it in GitHub Desktop.
SharePoint Lookup in other List (jQuery)
// A $( document ).ready() block.
$( document ).ready(function() {
console.log( "ready!" );
var itemId = getUrlParameter("EventId");
setLookupValue("Schulung_x002f_Unterweisung_6a5704d6-f2e4-46f4-9cf6-834e47e66d77_\\$LookupField", itemId);
$("select[id=Schulung_x002f_Unterweisung_6a5704d6-f2e4-46f4-9cf6-834e47e66d77_\\$LookupField]").change(checkBooking);
$("select[id=Schulung_x002f_Unterweisung_6a5704d6-f2e4-46f4-9cf6-834e47e66d77_\\$LookupField]").trigger( "change" );
})
function checkBooking ()
{
_this = this;
var bookingcount = 0;
console.log ("Search for EventID:" + this.value);
//Query book Events
$.ajax({
cache: false,
url: "/sites/a/departments/sav_biebesheim/schulungen/_api/web/lists/getByTitle('Schulung%20buchen')/items?$filter=(Schulung_x002f_Unterweisung eq " + this.value + ")",
type: 'GET',
dataType: 'json',
async: false,
headers: {
"accept": "application/json;odata=verbose;charset=utf-8"
}
}).done(function(data) {
console.log ("Booking Count:" + data.d.results.length);
var selBookingsText = $(_this).find("option:selected").text();
var selBookingsInt = parseInt(selBookingsText.substring(selBookingsText.lastIndexOf (":")+1, selBookingsText.length).trim());
console.log ("Seats:" + selBookingsInt);
if (data.d.results.length >= selBookingsInt )
{
reportStatus ('Diese Schulung ist bereits augebucht! Bitte wählen Sie eine andere Schulung aus.');
$("input[value=Speichern]").prop("disabled",true);
}
else
{
reportStatus ('Diese Schulung ist verfügbar.');
$("input[value=Speichern]").prop("disabled",false);
}
});
}
function reportStatus (text)
{
$(".reportmessage").remove();
$(".ms-formtoolbar").before( "<b class='reportmessage'>" + text + "</b>" );
}
function renderAssignedTo(ctx) {
//get current user properties
var currentUser = $().SPServices.SPGetCurrentUser({fieldNames: ["Title", "Name"]}); //get current user properties
var currentUserEntry = createUserEntity(currentUser.Name,currentUser.Title);
//Set user default value
ctx.CurrentFieldValue = []; //Note: it is assumed the user field is aaa multi-valued field (!)
ctx.CurrentFieldValue.push(currentUserEntry);
return SPClientPeoplePickerCSRTemplate(ctx);
}
function createUserEntity(userLoginName,userDisplayName)
{
return {
Description: userLoginName,
DisplayText: userDisplayName,
EntityGroupName: "",
EntityType: "",
HierarchyIdentifier: null,
IsResolved: true,
Key: userLoginName,
MultipleMatches: [],
ProviderDisplayName: "",
ProviderName: ""
};
}
function setLookupValue(fieldTitle, lookupVal)
{
//Set default value for lookups with less that 20 items
if ( $("select[id='" +fieldTitle+ "']").html() !== null)
{
$("select[id='"+ fieldTitle +"']").val(lookupVal);
}
else
{
choices = $("input[id='" +fieldTitle +"']").attr("choices");
hiddenInput = $("input[id='" +fieldTitle +"']").attr("optHid");
$("input[id='" +hiddenInput +"']").attr("value",lookupVal)
choiceArray = choices.split("|");
for (index = 1; index < choiceArray.length; index = index + 2)
{
if (choiceArray[index] == lookupVal){
$("input[id='" +fieldTitle +"']").val(choiceArray[index - 1]);
}
}
}
}
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment