Get List Field Type using ECMA Script in SP Hosted Apps

Leave a Comment
Hi All,

In a following post , we will see about how to check the SharePoint List Column(Field) type by using ECMA Script.

Please find the below code snippet.

NOTE: below code was written in SharePoint Hosted App.

'use strict';
var SPAppWebUrl;
var spHostUrl;
var field;
(function () {
$(document).ready(function () {
spHostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
        SPAppWebUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
        $('#btnGetFieldType').on('click', function () {
            GetFieldType();
        });
});
function GetFieldType()
{
var context = new SP.ClientContext.get_current();
if(context!==undefined && context !==null)
{
var parentContext=new SP.AppContextSite(context,spHostUrl);
var web=parentContext.get_web();
var list=web.get_lists().getByTitle("HelloTest");
if(list!==null)
{
//get the all column collection of the list
var fldCollection=list.get_fields();
//column name
field=fldCollection.getByTitle("test2");
context.load(field);
context.executeQueryAsync(onQuerySuccess,onQueryFailure);
}
}
function onQuerySuccess()
{
alert(field.get_typeAsString());
}

function onQueryFailure(sender,args)
{
alert('Failed to get the details:'+args.get_message());
}
}
function getQueryStringParameter(urlParameterKey) {
        var params = document.URL.split('?')[1].split('&');
        var strParams = '';
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split('=');
            if (singleParam[0] == urlParameterKey)
                return decodeURIComponent(singleParam[1]);
        }
    }
})();

We can see the out put as follows,

Thanks.

Related Post

0 comments:

Post a Comment