{"id":553,"date":"2012-09-18T23:58:58","date_gmt":"2012-09-19T05:58:58","guid":{"rendered":"https:\/\/toddbaginski.com\/blog\/?p=553"},"modified":"2013-06-28T15:44:25","modified_gmt":"2013-06-28T21:44:25","slug":"a-simple-sharepoint-2013-app-to-create-geolocation-fields-and-map-views","status":"publish","type":"post","link":"https:\/\/toddbaginski.com\/blog\/a-simple-sharepoint-2013-app-to-create-geolocation-fields-and-map-views\/","title":{"rendered":"A Simple SharePoint 2013 App To Create Geolocation Fields and Map Views"},"content":{"rendered":"<p>One of the new features in SharePoint 2013 is the Geolocation field.&#160; You can learn more about it <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/jj163135(v=office.15).aspx\" target=\"_blank\">here<\/a> in the MSDN.&#160; As I was developing some new SharePoint 2013 demos for my upcoming sessions at the <a href=\"http:\/\/www.devconnections.com\/shows\/fall2012\/default.aspx?s=192\" target=\"_blank\">SharePoint Connections Fall 2012 Conference<\/a> I started looking into this new functionality.&#160; My goal was to see how I could use the new Geolocation field type it to replace the Silverlight Bing Maps control I created for my SharePoint 2010 <a href=\"http:\/\/www.sharepointpromag.com\/article\/sharepoint\/creating-sharepoint-mashups-129463\" target=\"_blank\">mashup demo<\/a>.<\/p>\n<p>As I went through the MSDN examples I saw that all of the examples and code samples use command line programs to create the Geolocation field and map-enabled list view that relies on the Geolocation field.&#160; Personally, I\u2019m not a fan of using command line tools when I develop solutions, I\u2019d rather have something with a simple user interface that allows me to create new components quickly and easily without having to open a command prompt and remember the syntax associated with the command line program or a script.&#160; If you have been following my blog for a while you\u2019re probably familiar with the SharePoint Feature Manager I created about 6 years ago to manage features for a SharePoint site which falls into this same category.<\/p>\n<p>So, along these lines, I decided to create a simple SharePoint Hosted App that allows me to easily specify which list I would like to add a Geolocation field to, give the Geolocation field a name, specify a name for the map-enabled list view that relies on the Geolocation field, and click a button or two to make everything for me.<\/p>\n<p><strong><em>This is also a fantastic way to roll out this functionality to your users and give them self service capabilities to create this type of functionality directly from a web browser.<\/em><\/strong><\/p>\n<p>It turned out to be a very easy exercise!&#160; Here\u2019s what the SP APP looks like.<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image_thumb.png\" width=\"434\" height=\"417\" \/><\/a><\/p>\n<p>Just fill in the values, click the buttons the Geolocation field, and the Geolocation column and the map-enabled view are created in the target list as you can see below.<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image_thumb1.png\" width=\"601\" height=\"227\" \/><\/a><\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image_thumb2.png\" width=\"383\" height=\"132\" \/><\/a><\/p>\n<p>Here\u2019s what the new Map list view looks like.&#160; That\u2019s some pretty sweet out of the box functionality!<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image3.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/toddbaginski.com\/blog\/wp-content\/uploads\/2012\/09\/image_thumb3.png\" width=\"485\" height=\"623\" \/><\/a><\/p>\n<p>Here\u2019s how I went about it.&#160; First, I added some simple controls to the default.aspx page.<\/p>\n<div style=\"border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px\" id=\"codeSnippetWrapper\">\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\" id=\"codeSnippet\">&lt;%-- The following 4 lines are ASP.NET directives needed when <span style=\"color: #0000ff\">using<\/span> SharePoint components --%&gt;<br \/>&lt;%@ Page Inherits=<span style=\"color: #006080\">&quot;Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;<\/span> MasterPageFile=<span style=\"color: #006080\">&quot;~masterurl\/default.master&quot;<\/span> language=<span style=\"color: #006080\">&quot;C#&quot;<\/span> %&gt;<br \/>&lt;%@ Register Tagprefix=<span style=\"color: #006080\">&quot;SharePoint&quot;<\/span> Namespace=<span style=\"color: #006080\">&quot;Microsoft.SharePoint.WebControls&quot;<\/span> Assembly=<span style=\"color: #006080\">&quot;Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;<\/span> %&gt;<br \/>&lt;%@ Register Tagprefix=<span style=\"color: #006080\">&quot;Utilities&quot;<\/span> Namespace=<span style=\"color: #006080\">&quot;Microsoft.SharePoint.Utilities&quot;<\/span> Assembly=<span style=\"color: #006080\">&quot;Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;<\/span> %&gt;<br \/>&lt;%@ Register Tagprefix=<span style=\"color: #006080\">&quot;WebPartPages&quot;<\/span> Namespace=<span style=\"color: #006080\">&quot;Microsoft.SharePoint.WebPartPages&quot;<\/span> Assembly=<span style=\"color: #006080\">&quot;Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;<\/span> %&gt;<br \/><br \/>&lt;%-- The markup and script <span style=\"color: #0000ff\">in<\/span> the following Content element will be placed <span style=\"color: #0000ff\">in<\/span> the &lt;head&gt; of the page --%&gt;<br \/>&lt;asp:Content ContentPlaceHolderId=<span style=\"color: #006080\">&quot;PlaceHolderAdditionalPageHead&quot;<\/span> runat=<span style=\"color: #006080\">&quot;server&quot;<\/span>&gt;<br \/>    &lt;script type=<span style=\"color: #006080\">&quot;text\/javascript&quot;<\/span> src=<span style=\"color: #006080\">&quot;..\/Scripts\/jquery-1.6.2.min.js&quot;<\/span>&gt;&lt;\/script&gt;<br \/><br \/>    &lt;!-- Add your CSS styles to the following file --&gt;<br \/>    &lt;link rel=<span style=\"color: #006080\">&quot;Stylesheet&quot;<\/span> type=<span style=\"color: #006080\">&quot;text\/css&quot;<\/span> href=<span style=\"color: #006080\">&quot;..\/Content\/App.css&quot;<\/span> \/&gt;<br \/><br \/>    &lt;!-- Add your JavaScript to the following file --&gt;<br \/>    &lt;script type=<span style=\"color: #006080\">&quot;text\/javascript&quot;<\/span> src=<span style=\"color: #006080\">&quot;..\/Scripts\/App.js&quot;<\/span>&gt;&lt;\/script&gt;<br \/><br \/>    &lt;!-- The following script runs when the DOM <span style=\"color: #0000ff\">is<\/span> ready. The inline code uses a SharePoint feature to ensure --&gt;<br \/>    &lt;!-- The SharePoint script file sp.js <span style=\"color: #0000ff\">is<\/span> loaded and will then execute the sharePointReady() <span style=\"color: #0000ff\">function<\/span> <span style=\"color: #0000ff\">in<\/span> App.js --&gt;<br \/>    &lt;script type=<span style=\"color: #006080\">&quot;text\/javascript&quot;<\/span>&gt;<br \/>        $(document).ready(<span style=\"color: #0000ff\">function<\/span> () {<br \/>            SP.SOD.executeFunc(<span style=\"color: #006080\">'sp.js'<\/span>, <span style=\"color: #006080\">'SP.ClientContext'<\/span>, <span style=\"color: #0000ff\">function<\/span> () { sharePointReady(); });<br \/>        });<br \/>    &lt;\/script&gt;<br \/>&lt;\/asp:Content&gt;<br \/><br \/>&lt;%-- The markup and script <span style=\"color: #0000ff\">in<\/span> the following Content element will be placed <span style=\"color: #0000ff\">in<\/span> the &lt;body&gt; of the page --%&gt;<br \/>&lt;asp:Content ContentPlaceHolderId=<span style=\"color: #006080\">&quot;PlaceHolderMain&quot;<\/span> runat=<span style=\"color: #006080\">&quot;server&quot;<\/span>&gt;<br \/><br \/>    &lt;div&gt;<br \/>        List Name:&lt;br \/&gt;&lt;input type=<span style=\"color: #006080\">&quot;text&quot;<\/span> id=<span style=\"color: #006080\">&quot;listName&quot;<\/span>\/&gt;&lt;p \/&gt;<br \/>        Geolocation Field Name:&lt;br \/&gt;&lt;input type=<span style=\"color: #006080\">&quot;text&quot;<\/span> id=<span style=\"color: #006080\">&quot;fieldName&quot;<\/span>\/&gt;&lt;p \/&gt;<br \/>        View Name:&lt;br \/&gt;&lt;input type=<span style=\"color: #006080\">&quot;text&quot;<\/span> id=<span style=\"color: #006080\">&quot;viewName&quot;<\/span>\/&gt;&lt;p \/&gt;<br \/>        &lt;button id=<span style=\"color: #006080\">&quot;createFieldButton&quot;<\/span> type=<span style=\"color: #006080\">&quot;button&quot;<\/span> value=<span style=\"color: #006080\">&quot;&quot;<\/span>&gt;Create Geolocation Field&lt;\/button&gt;&lt;p \/&gt;<br \/>        &lt;button id=<span style=\"color: #006080\">&quot;createViewButton&quot;<\/span> type=<span style=\"color: #006080\">&quot;button&quot;<\/span> value=<span style=\"color: #006080\">&quot;&quot;<\/span>&gt;Create Map View&lt;\/button&gt;<br \/>    &lt;\/div&gt;<br \/><br \/>&lt;\/asp:Content&gt;<br \/><\/pre>\n<p><\/div>\n<p>Then, I wrote the JavaScript to do the work and added it to the app.js file.<\/p>\n<div style=\"border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px\" id=\"codeSnippetWrapper\">\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\" id=\"codeSnippet\"><span style=\"color: #0000ff\">var<\/span> context;<br \/><span style=\"color: #0000ff\">var<\/span> web;<br \/><span style=\"color: #0000ff\">var<\/span> user;<br \/><span style=\"color: #0000ff\">var<\/span> field;<br \/><span style=\"color: #0000ff\">var<\/span> view;<br \/><br \/><span style=\"color: #0000ff\">function<\/span> sharePointReady() {<br \/>    context = <span style=\"color: #0000ff\">new<\/span> SP.ClientContext.get_current();<br \/>    web = context.get_web();<br \/><br \/>    $(<span style=\"color: #006080\">&quot;#createFieldButton&quot;<\/span>).click(<span style=\"color: #0000ff\">function<\/span> () {<br \/>        <span style=\"color: #0000ff\">var<\/span> fieldName = $(<span style=\"color: #006080\">&quot;#fieldName&quot;<\/span>).val();<br \/>        createGeoLocationField(fieldName);<br \/>    });<br \/><br \/>    $(<span style=\"color: #006080\">&quot;#createViewButton&quot;<\/span>).click(<span style=\"color: #0000ff\">function<\/span> () {<br \/>        <span style=\"color: #0000ff\">var<\/span> fieldName = $(<span style=\"color: #006080\">&quot;#fieldName&quot;<\/span>).val();<br \/>        <span style=\"color: #0000ff\">var<\/span> viewName = $(<span style=\"color: #006080\">&quot;#viewName&quot;<\/span>).val();<br \/>        createGeoLocationView(viewName, fieldName);<br \/>    });<br \/>}<br \/><br \/><span style=\"color: #0000ff\">function<\/span> createGeoLocationView(viewName, fieldName) {<br \/>    spHostUrl = decodeURIComponent(getQueryStringParameter(<span style=\"color: #006080\">&quot;SPHostUrl&quot;<\/span>));<br \/>    rootClientContext = <span style=\"color: #0000ff\">new<\/span> SP.ClientContext(spHostUrl);<br \/>    list = rootClientContext.get_web().get_lists().getByTitle($(<span style=\"color: #006080\">&quot;#listName&quot;<\/span>).val());<br \/><br \/>    <span style=\"color: #0000ff\">var<\/span> viewCreationInformation = <span style=\"color: #0000ff\">new<\/span> SP.ViewCreationInformation();<br \/>    viewCreationInformation.set_title(viewName);<br \/><br \/>    <span style=\"color: #0000ff\">var<\/span> viewType = <span style=\"color: #0000ff\">new<\/span> SP.ViewType();    <br \/>    viewCreationInformation.set_viewTypeKind(viewType.html)<br \/><br \/>    view = list.get_views().add(viewCreationInformation);<br \/><br \/>    view.set_jsLink(<span style=\"color: #006080\">&quot;mapviewtemplate.js&quot;<\/span>);<br \/>    view.get_viewFields().add(<span style=\"color: #006080\">&quot;Location&quot;<\/span>);<br \/><br \/>    view.update();<br \/><br \/>    rootClientContext.load(view);<br \/>    rootClientContext.executeQueryAsync(<br \/>        Function.createDelegate(<span style=\"color: #0000ff\">this<\/span>, <span style=\"color: #0000ff\">this<\/span>.onCreateGeoLocationViewSucceeded),<br \/>        Function.createDelegate(<span style=\"color: #0000ff\">this<\/span>, <span style=\"color: #0000ff\">this<\/span>.onCreateGeoLocationViewFailed)<br \/>    );<br \/>}<br \/><br \/><span style=\"color: #0000ff\">function<\/span> onCreateGeoLocationViewSucceeded() {<br \/>    <span style=\"color: #0000ff\">var<\/span> result = <span style=\"color: #006080\">'The view '<\/span> + view.get_title() + <span style=\"color: #006080\">' was successfully added.'<\/span>;<br \/>    alert(result);<br \/>}<br \/><br \/><span style=\"color: #0000ff\">function<\/span> onCreateGeoLocationViewFailed(sender, args) {<br \/>    alert(<span style=\"color: #006080\">'Request failed. '<\/span> + args.get_message() +<br \/>        <span style=\"color: #006080\">'\\n'<\/span> + args.get_stackTrace());<br \/>}<br \/><br \/><span style=\"color: #0000ff\">function<\/span> createGeoLocationField(fieldName) {<br \/>    spHostUrl = decodeURIComponent(getQueryStringParameter(<span style=\"color: #006080\">&quot;SPHostUrl&quot;<\/span>));<br \/>    rootClientContext = <span style=\"color: #0000ff\">new<\/span> SP.ClientContext(spHostUrl);<br \/>    list = rootClientContext.get_web().get_lists().getByTitle($(<span style=\"color: #006080\">&quot;#listName&quot;<\/span>).val());<br \/>    field = list.get_fields().addFieldAsXml(<br \/>        <span style=\"color: #006080\">'&lt;Field DisplayName=\\''<\/span> + fieldName + <span style=\"color: #006080\">'\\' Type=\\'Geolocation\\' \/&gt;'<\/span>,<br \/>        <span style=\"color: #0000ff\">true<\/span>,<br \/>        SP.AddFieldOptions.defaultValue<br \/>    );<br \/><br \/>    rootClientContext.load(field);<br \/>    rootClientContext.executeQueryAsync(<br \/>        Function.createDelegate(<span style=\"color: #0000ff\">this<\/span>, <span style=\"color: #0000ff\">this<\/span>.onCreateGeoLocationFieldSucceeded),<br \/>        Function.createDelegate(<span style=\"color: #0000ff\">this<\/span>, <span style=\"color: #0000ff\">this<\/span>.onCreateGeoLocationFieldFailed)<br \/>    );<br \/>}<br \/><br \/><span style=\"color: #0000ff\">function<\/span> onCreateGeoLocationFieldSucceeded() {<br \/>    <span style=\"color: #0000ff\">var<\/span> result = <span style=\"color: #006080\">'The GeoLocation field '<\/span> + field.get_title() + <span style=\"color: #006080\">' was successfully added.'<\/span>;<br \/>    alert(result);<br \/>}<br \/><br \/><span style=\"color: #0000ff\">function<\/span> onCreateGeoLocationFieldFailed(sender, args) {<br \/>    alert(<span style=\"color: #006080\">'Request failed. '<\/span> + args.get_message() +<br \/>        <span style=\"color: #006080\">'\\n'<\/span> + args.get_stackTrace());<br \/>}<br \/><br \/><span style=\"color: #0000ff\">function<\/span> getQueryStringParameter(paramToRetrieve) {<br \/>    <span style=\"color: #0000ff\">var<\/span> <span style=\"color: #0000ff\">params<\/span> =<br \/>        document.URL.split(<span style=\"color: #006080\">&quot;?&quot;<\/span>)[1].split(<span style=\"color: #006080\">&quot;&amp;&quot;<\/span>);<br \/>    <span style=\"color: #0000ff\">var<\/span> strParams = <span style=\"color: #006080\">&quot;&quot;<\/span>;<br \/>    <span style=\"color: #0000ff\">for<\/span> (<span style=\"color: #0000ff\">var<\/span> i = 0; i &lt; <span style=\"color: #0000ff\">params<\/span>.length; i = i + 1) {<br \/>        <span style=\"color: #0000ff\">var<\/span> singleParam = <span style=\"color: #0000ff\">params<\/span>[i].split(<span style=\"color: #006080\">&quot;=&quot;<\/span>);<br \/>        <span style=\"color: #0000ff\">if<\/span> (singleParam[0] == paramToRetrieve)<br \/>            <span style=\"color: #0000ff\">return<\/span> singleParam[1];<br \/>    }<br \/>}<br \/><\/pre>\n<p><\/div>\n<p>It\u2019s pretty simple, as you can see.&#160; Hopefully this helps you out and gives some ideas that you can use to create your own SP APPs.<\/p>\n<p>It\u2019s already snowing at the ski resorts in Colorado!&#160; Opening day shouldn\u2019t be more than 30 days away at some of them!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the new features in SharePoint 2013 is the Geolocation field.&#160; You can learn more about it here in the MSDN.&#160; As I was developing<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":557,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[99],"tags":[10,22,75,5,76,7,68,74,72,73],"class_list":["post-553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sharepoint-2013","tag-custom-development","tag-development-utilities","tag-geolocation","tag-helpful-code","tag-mapping","tag-sharepoint","tag-sharepoint-2013","tag-sharepoint-apps","tag-sharepoint-hosted","tag-sp-app"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/comments?post=553"}],"version-history":[{"count":6,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":735,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/553\/revisions\/735"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media\/557"}],"wp:attachment":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media?parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/categories?post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/tags?post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}