Wednesday, 5 June 2019

Ensure SPServices script load before calling it

function ensureSPServices(callbackFunction) {
if($().SPServices == null) {
console.log("$().SPServices not loaded");
// If SPServices has not been loaded, this will return as a null value and let us know we need to load the library. The jQuery
// getScript method runs asynchronously, so we use the callback function for initial calls to the service
jQuery.getScript("jquery.SPServices.js", callbackFunction);
console.log("SPServices is now loaded!!!");
} else {
// SPServices is already loaded, so execute our callback function which contains the rest of the page initialization.
console.log("SPServices already loaded");
callbackFunction.call(null, "SPServices already loaded");
}
}
function InitializePage(data, textStatus) {
// This is the callback function called by the ensureSPServices function. 
// Here is where I would place any web-service calls that happen on the loading
// of the page. 
// i.e. GetDocumentLibraryTotalCount(libraryName, countContainer);
initPage();
}
function initPage() {
console.log("start initPage");

returnedListData(onComplete);
}
function returnedListData(whenDone){
console.log("returnedListData called");
var listData;
setTimeout(function(){
 console.log("start getting list data");
 RequestId = 1;
 listData = GetListItem(RequestId, "List Name");
 console.log("done getting list data");
 return whenDone(listData);
},10);
}


        _spBodyOnLoadFunctionNames.push("ensureSPServices(InitializePage)");

Tuesday, 25 April 2017

Getting binary data from SQL db and generate update statement

CONVERT([varchar](max), binarydata, 1)


SELECT   'UPDATE table SET Field1 = Value 1 WHERE id = 14006 '+ CHAR(13)+CHAR(10) +'GO'
FROM            table WHERE id = 14006

Tuesday, 11 April 2017

Excel VBS for ConcatenateRange

Function ConcatenateRange(ByVal cell_range As Range, ByVal marks As Range) As String

Dim cell As Range
Dim coursesString As String
Dim cellArray As Variant
Dim cellArray2 As Variant
Dim i As Long, j As Long

Dim CoursesArray As New Collection

cellArray = cell_range.Value
cellArray2 = marks.Value

' Add courses into array if there's mark
For i = 1 To UBound(cellArray, 1)
    For j = 1 To UBound(cellArray, 2)
        If Len(cellArray(i, j)) <> 0 Then
            ' check if there's mark and add course to courses array
            If cellArray2(i, j) <> 0 Then
             CoursesArray.Add (cellArray(i, j))
            End If
        End If
    Next
Next

'return string of courses delimited with new line character
    For Each CourseId In SortCollection(CoursesArray)
      coursesString = coursesString & (CourseId & Chr(10))
    Next CourseId

' return result
ConcatenateRange = coursesString

End Function



' sort collection
Function SortCollection(Optional ByVal listToSort As Collection) As Collection
    Dim list As Collection
    Dim vItm As Variant
    Dim i As Long, j As Long
    Dim vTemp As Variant

    Set list = listToSort

    'Two loops to bubble sort
    For i = 1 To list.Count - 1
        For j = i + 1 To list.Count
            If list(i) > list(j) Then
                'store the lesser item
                vTemp = list(j)
                'remove the lesser item
                list.Remove j
                're-add the lesser item before the
                'greater Item
                list.Add vTemp, vTemp, i
            End If
        Next j
    Next i
   
    ' return result
    Set SortCollection = list

End Function


Wednesday, 22 February 2017

SQL URL Decode

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[UrlDecode](@url varchar(3072))
RETURNS varchar(3072)
AS
BEGIN
    DECLARE @count int, @c char(1), @cenc char(2), @i int, @urlReturn varchar(3072)
    SET @count = Len(@url)
    SET @i = 1
    SET @urlReturn = ''
    WHILE (@i <= @count)
     BEGIN
        SET @c = substring(@url, @i, 1)
        IF @c LIKE '[!%]' ESCAPE '!'
         BEGIN
            SET @cenc = substring(@url, @i + 1, 2)
            SET @c = CHAR(CASE WHEN SUBSTRING(@cenc, 1, 1) LIKE '[0-9]'
                                THEN CAST(SUBSTRING(@cenc, 1, 1) as int)
                                ELSE CAST(ASCII(UPPER(SUBSTRING(@cenc, 1, 1)))-55 as int)
                            END * 16 +
                            CASE WHEN SUBSTRING(@cenc, 2, 1) LIKE '[0-9]'
                                THEN CAST(SUBSTRING(@cenc, 2, 1) as int)
                                ELSE CAST(ASCII(UPPER(SUBSTRING(@cenc, 2, 1)))-55 as int)
                            END)
            SET @urlReturn = @urlReturn + @c
            SET @i = @i + 2
         END
        ELSE
         BEGIN
            SET @urlReturn = @urlReturn + @c
         END
        SET @i = @i +1
     END
    RETURN @urlReturn
END

GO

Wednesday, 7 December 2016

Preffer IPv4 over IPv6 in Windows

start-->run-->regedit

Navigate to:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters

Create a new 32bit Dword (this works for both x86 and x64 machines) and call it "DisabledComponents"

Right-click and select modify. Fill in the field with all F's.

Reboot.

Thursday, 13 October 2016

Tuesday, 4 October 2016

Fetch the row count for all tables in a SQL SERVER

CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC
DROP TABLE #counts