Sunday, June 18, 2017

SharePoint jQuery generic call to get rest data

Lately I've been working with SharePoint a lot and creating functions to read data from lists and manipulate / display it to the user.  Below are two generic JS / jQuery functions that I created to do this.

I've explained the parameters so hopefully you will find it useful.

// inListSiteURL is the base URL of the SharePoint site... such as https://somespsite.somedomain.com or https://somespsite.somedoamin.com/someSubSite
// inListTitle is the title of the SharePoint list
// inHaveResultsFunction is the function to call when data is found
// inNoResultsFunciton is the function to call when no data is found
// inFilter is the filter to use... maybe something like "?$select=Title,URL&$top=100&$filter=Editor/Id%20eq%20123&$orderby=Title asc"... you can leave this blank if you want
// inFinalFunction is the function to call after all results
function spGetListData(inListSiteURL, inListTitle, inHaveResultsFunction, inNoResultsFunction, inFilter, inFinalFunction)
{
    try {
        var callURL = inListSiteURL + "/_api/web/lists/GetByTitle('" + inListTitle + "')/items" + inFilter;
        spGetRestDataCall(callURL, inHaveResultsFunction, inNoResultsFunction, null, inFinalFunction);
    } catch(err) {
        
    }
} // function spGetListData


// this function is a 'generic' ajax call used by the other functions to simplify the code
// inURL is the URL to the SharePoint list to retrieve data from
// inSuccessFunction is the function to call when data is returned
// inNoResultFunction is the function to call when no data is returned
// inErrorFunction is the function to call when an error is encountered
// inFinalFunction is the function to call when after all the data is processed
function spGetRestDataCall(inURL, inSuccessFunction, inNoResultFunction, inErrorFunction, inFinalFunction)
{
    var spRestContentType = "application/json;odata=verbose"; // you should not have to change this
    $.ajax({
        url: inURL,
        method: "GET",
        processData: false,  
        contentType: spRestContentType,
        headers: { "Accept": spRestContentType },
        success: function (data) {
            try {
                if (data.d.results.length == 0) {
                    if (inNoResultFunction !== null) {
                        inNoResultFunction();
                    }
                } else {
                    $.each(data.d.results, function(index, item){
                        inSuccessFunction(index,item);
                    });
                    if (inFinalFunction !== null) {
                        inFinalFunction();
                    }
                }
            } catch(err) {
                inSuccessFunction(data.d);
            }
        },
        error: function (data) {
            //console.log(JSON.stringify(data));
            if (inErrorFunction !== null) {
                inErrorFunction(data);
            }
        }
    });
} // function spGetRestDataCall

Friday, May 12, 2017

Psycho Chat

Years ago, a friend wrote an automated help chat program that the users would use to solve basic computer questions. He wrote it in Pascal (circa 1985). When I was running a BBS, he thought it would be cool to use his program to answer basic BBS questions, I agreed.

So I took his program, ran it through a Pascal to C converter and 'fixed' it and made a BBS door called Psycho Chat out of it. I hooked it to the Page Sysop feature of the BBS (circa 1992 running Wildcat!) and the transcripts were hilarious.

Years later (circa 2002), just for fun, I hand converted it to Delphi (which is based on Pascal... funny how the program came full circle). It worked OK but just wasn't the same.

On and off for the past 15 years I've been thinking of making it a web page using PHP and Ajax. My friend had the same idea yesterday so I decided it was time to actually do something about it. Took me a couple hours to hand convert the Delphi program to Javascript / jQuery / PHP but now its working. Still using the response files from the BBS.

It was a fun project and I don't know why I waited so long to do it.

This version doesn't record what is being said... hmmm... maybe I should add that just for fun :)

Sunday, March 26, 2017

Calculated Image

A billion years ago I found a one liner in a magazine and it made a pretty cool image. Recently I found it again and thought it would be cool to try it on a modern computer. Below is the algorithm I came up with. Just drop a Picture Box onto a form, add an X, Y and Decimal (from .2 to .9):
// Created by Gregg Buntin
private void DrawImage()
{
    int sizeX = Convert.ToInt32(tbX.Text); // this is the X (width) of the image
    int sizeY = Convert.ToInt32(tbY.Text); // this is the Y (height) of the image
    int halfX = sizeX / 2;
    int halfY = sizeY / 2;
    calculatedImage = new Bitmap(sizeX, sizeY);
    Color tempColor = new Color();
    double num = Convert.ToDouble(tbDec.Text); // This is where you plug in the Decimal value
    for (int Y = -(halfY); Y < 1; Y++)
    {
        int Y2 = Y * Y;
        for (int X = -(halfX); X < 1; X++)
        {
            tempColor = Color.White;
            double tempR = Math.Pow((Y2 + (X * X)), num);
            double R = (10000000 - tempR) / 2;
            int intR = Convert.ToInt32(R);
            double roundR = Math.Truncate(R);
            if (roundR != intR) tempColor = Color.Black;
            int X1 = X + halfX;
            int Y1 = Y + halfY;
            calculatedImage.SetPixel(X1, Y1, tempColor);
            calculatedImage.SetPixel(sizeX - 2 - X1, Y1, tempColor);
            calculatedImage.SetPixel(X1, sizeY - 2 - Y1, tempColor);
            calculatedImage.SetPixel(sizeX - 2 - X1, sizeY - 2 - Y1, tempColor);
        } // for X
    } // for Y
    pbImage.Image = calculatedImage;
} // method DrawImage
Copyright Realm Laboratories, LLC