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

1 comment:

zee said...

http://practicalguidetosharepoint.com
saif@walisystemsinc.com

A PRACTICAL GUIDE TO SHAREPOINT

Learn SharePoint using this practical guide! This is a complete practical guide. It’s full of useful tips and exercises to get you started in no time! It has no fluff, just practical exercises that will provide you the knowledge and know-how to implement SharePoint solutions easily and professionally. SharePoint has a steep learning curve. Without a book like this in your hands, it will probably take you months to learn SharePoint. Learn from others experiences. You don’t have to take the pain to learn SharePoint on your own. Learn from the experts! Make good use of your time! This book has everything that you need to equip yourself to work professionally on SharePoint projects. Regardless if you are a seasoned professional or a beginner willing to learn SharePoint, this book has it all.

User level: Intermediate - Advanced
ISBN-13: 978-0-9915203-0-5
ISBN-10: 0991520300
Library of Congress Control Number: 2014902311
Copyright:©2014 Saifullah Shafiq
Language: English
Country: United States
Published by: Wali Systems, Inc.
Publication Date: Feb 06, 2014

Page Count: 378 pages
Size: Crown Quarto (7.44 x 9.68 in)
Binding: Perfect bound
Price (Paperback): $23.95