Geolocation column in SharePoint

by | Mar 2, 2016 | Office 365, SharePoint | 0 comments

Bing Map

Did you ever try to use Geolocation column in SharePoint? No? So today I would like to write about something new. I mean that today I am going to write about Geolocation column in SharePoint. This topic is not described to often, which is strange to me as it’s very interesting and also it could be very useful. For my tests and coding I will use SharePoint online. Geolocation column is a new feature, which has been introduced in SharePoint 2013. If you are not familiar with this topic you should read first article at MSDN pages. In general new column give us possibility to keep in SharePoint column (Geolocation column) geographical coordinates, which can be used to build nice looking Bing maps with our custom location. Let’s see how we can do this.

The first step is to get a key for Bing maps which can be generated at this Microsoft portal. For our needs we can use Basic key, which can be obtained for free. But if you plan to build something big probably you will need paid key. If you get your key you can now activate geolocation columns in your site. By default this option in not active. If you run SharePoint on premise you can use PowerShell script.

 

$WebUrl = "https://mojaWitryna.sharepoint.com/KRMBasicData"
$EmailAddress = “mojUser”
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$Credentials = Get-Credential -UserName $EmailAddress -Message “Podaj hasło do Office 365”
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($EmailAddress,$Credentials.Password)
$List = $Context.Web.Lists.GetByTitle(“Moja lista“)
$FieldXml = “<Field Type=’Geolocation’ DisplayName=’Location‘/>”
$Option=[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView
$List.Fields.AddFieldAsXml($fieldxml,$true,$option)
$Context.Load($list)
$Context.ExecuteQuery()
$Context.Dispose()

Some people says that they were able to run this script against SharePointa online, with SharePoint online management shell. Well, I always get in this case errors. I don’t know. Maybe there’s a problem with a files version or something… Microsoft make a lot of changes to Office 365 stack. For my environment I used CSOM, as console application. Works perfect for me.

var webUrl = new Uri("https://mojawitryna.sharepoint.com/Site");
                using (ClientContext ctx = new ClientContext(webUrl))
                {
                    var login = "user";
                    var password = "password";
                    var secureStrPwd = new SecureString();
                    foreach (char c in password)
                    {
                        secureStrPwd.AppendChar(c);
                    }

                    var creds = new SharePointOnlineCredentials(login, secureStrPwd);
                    ctx.Credentials = creds;

                    var web = ctx.Web;
                web.AllProperties["BING_MAPS_KEY"] = "Bing key ";
                 web.Update();

                ctx.ExecuteQuery();

                List officeLocationList = ctx.Web.Lists.GetByTitle("Moja lista");
                officeLocationList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Column name'/>", true,AddFieldOptions.AddToAllContentTypes);
                try
                {
                    officeLocationList.Update();
                    ctx.ExecuteQuery();
                }
                catch (Exception)
                {

                    throw;
                }

 

And that’s all. Please note that our code adds column to existing list! You can’t just activate geolocation column and then add it later to the list with browser. You have to always use code to add such column to the specific list.

Bing Map
Bing Map

Above you can see a map which displays our location base on the data in SharePoint list. By default you can’t create such view in SharPoint list. But when you activate geolocation column you will get new view template called  “Map View”. There’s one more thing. Geolocation data is difficult to add to the list. In my next post I will show you how to make in easy way.

Written by Tomasz Szulczewski

Hi, my name is Tomasz Szulczewski, and I have been in love with information technology for over 25 years, but I still have an IT passion and feel like a geek. I am a person who is problem solver who thinks that not all people must be experts in IT.

Related Posts

Power virtual agent for office 365

Power virtual agent for office 365

Why Should I even care about power virtual agent for office 365 or power virtual agent for teams? Simple answer? Do you need an AI assistant who will help you run your business in many different ways? If yes, that's your answer :). But let's start from the beginning....

read more
What is SharePoint in Office 365?

What is SharePoint in Office 365?

What is SharePoint in Office 365, and why is SharePoint for small business the critical product? So you bought a Microsoft 365 license, and your tenant is online. You open a browser, and you have no idea what to do next? Don't worry; I will try to give you a few...

read more

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.