Loading...

Thursday, October 31, 2013

SharePoint hosted app to enable Geolocation column

GeoLocation field is newly introduced column type in SharePoint 2013 .You will not see any field of type GeoLocation by  by default but It needs to be enabled with program. This is quite useful to
save the Geographic info in SharePoint List.

1)Create a SharePoint hosted app and open up the Start page , here before executing actual CSOM
   following  few lines , get the context of hostweb url , app Web url and loads the sp.js

<script type="text/javascript">
    var hostweburl;
 var appweburl;
var web;
var list;
var context;
    // Load the required SharePoint libraries.
    $(document).ready(function () {

        // Get the URI decoded URLs.
        hostweburl =
            decodeURIComponent(
                getQueryStringParameter("SPHostUrl")
        );
  
  appweburl =
            decodeURIComponent(
                getQueryStringParameter("SPAppWebUrl")
        );

        // The js files are in a URL in the form:
        // web_url/_layouts/15/resource_file
        var scriptbase = hostweburl + "/_layouts/15/";

        // Load the js files and continue to
        // the execOperation function.
        $.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js", execOperation);
            }
        );
    });

2)Now with the help of CSOM ,create SP list with GeoLocation column added to it.

   // Function to execute basic operations.
    function execOperation() {

       
    //context = new SP.ClientContext(hostweburl);
    
 context = new SP.ClientContext(appweburl); 
 var appContextSite = new SP.AppContextSite(context, hostweburl);
   // this.web=context.get_web();
   this.web= appContextSite.get_web();   
     // Continue your program flow here.
  context.load(this.web);

var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title('CustomList');
listCreationInfo.set_templateType(SP.ListTemplateType.genericList);

this.web.get_lists().add(listCreationInfo);
this.list = this.web.get_lists().getByTitle('CustomList');


var fldCollection = this.list.get_fields();
alert(fldCollection);

var geoField = context.castTo(
fldCollection.addFieldAsXml('<Field Type="Geolocation" DisplayName="GeoLocation" Name="GeoLocation" />',
true,
SP.AddFieldOptions.addToDefaultContentType),
SP.FieldGeolocation);
geoField.set_title("Geo Location");
geoField.update();
alert(geoField);

context.executeQueryAsync(
 genericSuccess,
 genericFail
);

}

function genericSuccess(sender, args) {
    alert('success');
}
    
function genericFail(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

    // Function to retrieve a query string value.
    // For production purposes you may want to use
    // a library to handle the query string.
    function getQueryStringParameter(paramToRetrieve) {
        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] == paramToRetrieve)
                return singleParam[1];
        }
    }
</script>

3)Once this app is deployed successfully then you will see , List with name Custom List with  Geolocation locumn added to it in host site