Skip to content

Commit cbff147

Browse files
committed
📜🧼 fix numerous markdown formatting errors
1 parent d2a20e2 commit cbff147

File tree

2 files changed

+138
-261
lines changed

2 files changed

+138
-261
lines changed
Lines changed: 105 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Add a Geolocation column to a list programmatically in SharePoint
33
description: Learn how to programmatically add a Geolocation column to a list in SharePoint. Integrate ___location information and maps in SharePoint lists and ___location-based websites by using the new Geolocation field creating your own Geolocation-based field type.
4-
ms.date: 11/08/2022
4+
ms.date: 11/22/2022
55
ms.prod: sharepoint
66
ms.assetid: f31a3594-c328-4731-b8eb-5da6b85103ad
77
ms.localizationpriority: high
@@ -10,209 +10,152 @@ ms.localizationpriority: high
1010

1111
Learn how to programmatically add a Geolocation column to a list in SharePoint. Integrate ___location information and maps in SharePoint lists and ___location-based websites by using the new Geolocation field creating your own Geolocation-based field type.
1212

13-
SharePoint introduces a new field type named Geolocation that enables you to annotate SharePoint lists with ___location information. In columns of type Geolocation, you can enter ___location information as a pair of latitude and longitude coordinates in decimal degrees or retrieve the coordinates of the user's current ___location from the browser if it implements the W3C Geolocation API. For more information about the Geolocation column, see [Integrating ___location and map functionality in SharePoint](integrating-___location-and-map-functionality-in-sharepoint.md). The Geolocation column is not available by default in SharePoint lists. To add the column to a SharePoint list, you have to write code. In this article, learn how to add the Geolocation field to a list programmatically by using the SharePoint client object model.
14-
15-
16-
13+
SharePoint introduces a new field type named Geolocation that enables you to annotate SharePoint lists with ___location information. In columns of type Geolocation, you can enter ___location information as a pair of latitude and longitude coordinates in decimal degrees or retrieve the coordinates of the user's current ___location from the browser if it implements the W3C Geolocation API. For more information about the Geolocation column, see [Integrating ___location and map functionality in SharePoint](integrating-___location-and-map-functionality-in-sharepoint.md). The Geolocation column isn't available by default in SharePoint lists. To add the column to a SharePoint list, you have to write code. In this article, learn how to add the Geolocation field to a list programmatically by using the SharePoint client object model.
14+
15+
An MSI package named SQLSysClrTypes.msi must be installed on every SharePoint front-end web server to view the geolocation field value or data in a list. This package installs components that implement the new geometry, geography, and hierarchy ID types in SQL Server 2008. By default, this file is installed for SharePoint Online. However, it isn't for an on-premises deployment of SharePoint. You must be a member of the Farm Administrators group to perform this operation. To download SQLSysClrTypes.msi, see [Microsoft SQL Server 2008 R2 SP1 Feature Pack](https://www.microsoft.com/download/details.aspx?id=30437) for SQL Server 2008, or [Microsoft SQL Server 2012 Feature Pack](https://www.microsoft.com/download/details.aspx?id=29065)for SQL Server 2012 in the Microsoft Download Center.
1716

18-
An MSI package named SQLSysClrTypes.msi must be installed on every SharePoint front-end web server to view the geolocation field value or data in a list. This package installs components that implement the new geometry, geography, and hierarchy ID types in SQL Server 2008. By default, this file is installed for SharePoint Online. However, it is not for an on-premises deployment of SharePoint. You must be a member of the Farm Administrators group to perform this operation. To download SQLSysClrTypes.msi, see [Microsoft SQL Server 2008 R2 SP1 Feature Pack](https://www.microsoft.com/download/details.aspx?id=30437) for SQL Server 2008, or [Microsoft SQL Server 2012 Feature Pack](https://www.microsoft.com/download/details.aspx?id=29065)for SQL Server 2012 in the Microsoft Download Center.
1917
## Prerequisites for adding a Geolocation column
20-
<a name="SP15addgeo_prereq"> </a>
2118

19+
- Access to a SharePoint list, with sufficient privileges to add a column.
20+
- A valid Bing Maps key set at the farm or web level, which can be obtained from the [Bing Maps Account Center](https://www.bingmapsportal.com/).
21+
22+
> [!IMPORTANT]
23+
> Please note that you are responsible for compliance with terms and conditions applicable to your use of the Bing Maps key, and any necessary disclosures to users of your application regarding data passed to the Bing Maps service.
2224
23-
24-
25-
25+
- Visual Studio 2010
26+
- SharePoint Online Management Shell: https://www.microsoft.com/download/details.aspx?id=35588
27+
- SharePoint PnP-PowerShell (legacy): https://github.com/SharePoint/PnP-PowerShell/
28+
- SharePoint PnP.PowerShell (latest): https://github.com/pnp/powershell
2629

27-
- Access to a SharePoint list, with sufficient privileges to add a column.
28-
29-
30-
- A valid Bing Maps key set at the farm or web level, which can be obtained from the [Bing Maps Account Center](https://www.bingmapsportal.com/).
31-
32-
> **Important:**
33-
> Please note that you are responsible for compliance with terms and conditions applicable to your use of the Bing Maps key, and any necessary disclosures to users of your application regarding data passed to the Bing Maps service.
34-
- Visual Studio 2010.
35-
- SharePoint Online Management Shell - https://www.microsoft.com/download/details.aspx?id=35588
36-
- SharePoint PnP-PowerShell (legacy) - https://github.com/SharePoint/PnP-PowerShell/
37-
- SharePoint PnP.PowerShell (latest) - https://github.com/pnp/powershell
38-
3930
[!INCLUDE [pnp-powershell](../../includes/snippets/open-source/pnp-powershell.md)]
4031

4132
## Code example: Add a Geolocation column to an existing list programmatically
42-
<a name="SP15addgeo_addcolumn"> </a>
4333

4434
Follow these steps to add the Geolocation column to a list. This must be done programmatically with CSOM or PowerShell
45-
35+
4636
### To add the Geolocation column to a list using PnP PowerShell
37+
4738
1. Open PowerShell
48-
2. Connect to the site you wish to add the column to
49-
```csharp
50-
Connect-PnPOnline -url "https://TENANT.sharepoint.com/sites/SITEURL"
51-
```
52-
3. Open the list you wish to add the column to
53-
```csharp
54-
$list = Get-PnPList -Identity "LISTNAME"
55-
```
56-
4. #Add the Geolocation field - Change parameters as necessary
57-
```csharp
58-
Add-PnPField -List $list -Type GeoLocation -DisplayName "GeoLocationField" -InternalName "GeoLocationField" -AddToDefaultView -Required
59-
```
60-
61-
62-
39+
1. Connect to the site you wish to add the column to
6340

64-
### To add the Geolocation column to a list using the client object model in Visual Studio
41+
```csharp
42+
Connect-PnPOnline -url "https://TENANT.sharepoint.com/sites/SITEURL"
43+
```
6544

45+
1. Open the list you wish to add the column to
46+
47+
```csharp
48+
$list = Get-PnPList -Identity "LISTNAME"
49+
```
50+
51+
1. #Add the Geolocation field - Change parameters as necessary
52+
53+
```csharp
54+
Add-PnPField -List $list -Type GeoLocation -DisplayName "GeoLocationField" -InternalName "GeoLocationField" -AddToDefaultView -Required
55+
```
56+
57+
### To add the Geolocation column to a list using the client object model in Visual Studio
6658

6759
1. Start Visual Studio.
68-
69-
70-
2. On the menu bar, choose **File, New Project**. The **New Project** dialog box opens.
71-
72-
73-
3. In the **New Project** dialog box, choose **C#** in the **Installed Templates** box, and then choose the **Console Application** template.
74-
75-
76-
4. Give the project a name, and then choose the **OK** button.
77-
78-
79-
5. Visual Studio creates the project. Add a reference to the following assemblies, and choose **OK**.
80-
81-
Microsoft.SharePoint.Client.dll
82-
83-
Microsoft.SharePoint.Client.Runtime.dll
84-
85-
86-
6. In the default .cs file, add a **using** directive as follows.
87-
88-
`using Microsoft.SharePoint.Client;`
89-
90-
91-
7. Add the following code to the **Main** method in the .cs file.
92-
93-
```csharp
94-
95-
class Program
60+
1. On the menu bar, choose **File, New Project**. The **New Project** dialog box opens.
61+
1. In the **New Project** dialog box, choose **C#** in the **Installed Templates** box, and then choose the **Console Application** template.
62+
1. Give the project a name, and then choose the **OK** button.
63+
1. Visual Studio creates the project. Add a reference to the following assemblies, and choose **OK**.
64+
65+
- Microsoft.SharePoint.Client.dll
66+
- Microsoft.SharePoint.Client.Runtime.dll
67+
68+
1. In the default .cs file, add a **using** directive as follows.
69+
70+
```csharp
71+
using Microsoft.SharePoint.Client;
72+
```
73+
74+
1. Add the following code to the **Main** method in the .cs file.
75+
76+
```csharp
77+
class Program
9678
{
97-
static void Main(string[] args)
98-
{
99-
AddGeolocationField();
100-
Console.WriteLine("Location field added successfully");
101-
}
102-
private static void AddGeolocationField()
103-
{
104-
// Replace site URL and List Title with Valid values.
105-
ClientContext context = new ClientContext("<Site Url>");
106-
List oList = context.Web.Lists.GetByTitle("<List Title>");
107-
oList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Location'/>",true, AddFieldOptions.AddToAllContentTypes);
108-
oList.Update();
109-
context.ExecuteQuery();
110-
}
79+
static void Main(string[] args)
80+
{
81+
AddGeolocationField();
82+
Console.WriteLine("Location field added successfully");
83+
}
84+
private static void AddGeolocationField()
85+
{
86+
// Replace site URL and List Title with Valid values.
87+
ClientContext context = new ClientContext("<Site Url>");
88+
List oList = context.Web.Lists.GetByTitle("<List Title>");
89+
oList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Location'/>",true, AddFieldOptions.AddToAllContentTypes);
90+
oList.Update();
91+
context.ExecuteQuery();
92+
}
11193
}
112-
```
113-
114-
8. Replace \<Site Url\> and \<List Title\> with valid values.
115-
116-
117-
9. Set the target framework in Project Properties as .NET Framework 4.0 or 3.5, and run the example.
118-
119-
120-
10. Navigate to the list. You should be able to see a column named **Location** of type Geolocation in the list. You can now enter some values and see it in action. Figure 1 shows the default ___location and map features that you can expect to see in your list.
121-
122-
**Figure 1. Summarized view of the default ___location and map features**
94+
```
12395

124-
96+
1. Replace `<Site Url>` and `<List Title>` with valid values.
97+
1. Set the target framework in Project Properties as .NET Framework 4.0 or 3.5, and run the example.
98+
1. Navigate to the list. You should be able to see a column named **Location** of type Geolocation in the list. You can now enter some values and see it in action. Figure 1 shows the default ___location and map features that you can expect to see in your list.
12599

100+
**Figure 1. Summarized view of the default ___location and map features**
126101
![Default Geolocation and Map feature](../images/SP15Con_HowToAddGeolocationColumnUpdated_Fig1.png)
127-
128-
129-
130-
131-
132102

133103
## Add a list item with the Geolocation field value to a SharePoint list programmatically
134-
<a name="SP15addgeo_addlistitem"> </a>
135104

136105
After the Geolocation field is added to a SharePoint list, the developer can add the list item to the list programmatically. There are two ways to add the list item programmatically: by passing the **FieldGeolocationValue** object to the Geolocation field, and by passing **Raw Value** to the Geolocation field.
137-
138-
139-
140106

141107
### Method A: Pass the FieldGeolocationValue object to the Geolocation field
142108

143-
144109
- The following method adds a list item by passing the Geolocation value as an object.
145-
146-
```csharp
147-
148-
private void AddListItem()
149-
{ // Replace site URL and List Title with Valid values.
150-
ClientContext context = new ClientContext("<Site Url>");
151-
List oList = context.Web.Lists.GetByTitle("<List Name>");
152-
153-
ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
154-
ListItem oListItem = oList.AddItem(itemCreationInfo);
155110

156-
oListItem["Title"] = "New Title";
111+
```csharp
112+
private void AddListItem() {
113+
// Replace site URL and List Title with Valid values.
114+
ClientContext context = new ClientContext("<Site Url>");
115+
List oList = context.Web.Lists.GetByTitle("<List Name>");
157116

158-
FieldGeolocationValue oGeolocationValue = new FieldGeolocationValue();
159-
oGeolocationValue.Latitude = (double)17.4;
160-
oGeolocationValue.Longitude = (double)78.4;
161-
oListItem["___location"] = oGeolocationValue;
117+
ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
118+
ListItem oListItem = oList.AddItem(itemCreationInfo);
162119

163-
oListItem.Update();
164-
context.ExecuteQuery();
165-
}
120+
oListItem["Title"] = "New Title";
166121

167-
```
122+
FieldGeolocationValue oGeolocationValue = new FieldGeolocationValue();
123+
oGeolocationValue.Latitude = (double)17.4;
124+
oGeolocationValue.Longitude = (double)78.4;
125+
oListItem["___location"] = oGeolocationValue;
168126

127+
oListItem.Update();
128+
context.ExecuteQuery();
129+
}
130+
```
169131

170132
### Method B: Pass a raw value to the Geolocation field
171133

172-
173134
- The following method adds a list item to the SharePoint list by passing raw values to the Geolocation field.
174-
175-
```csharp
176-
177-
private void AddListItem()
178-
{ // Replace site URL and List Title with Valid values.
179-
ClientContext context = new ClientContext("<Site Url>");
180-
List oList = context.Web.Lists.GetByTitle("<List Name>");
181-
182-
ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
183-
ListItem oListItem = oList.AddItem(itemCreationInfo);
184135

185-
oListItem["Title"] = "New Title";
186-
// Data in WKT (World Known Text) format.
187-
oListItem["___location"] = "POINT (78.4 17.4)" ;
136+
```csharp
137+
private void AddListItem() {
138+
// Replace site URL and List Title with Valid values.
139+
ClientContext context = new ClientContext("<Site Url>");
140+
List oList = context.Web.Lists.GetByTitle("<List Name>");
188141

189-
oListItem.Update();
190-
context.ExecuteQuery();
191-
}
142+
ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
143+
ListItem oListItem = oList.AddItem(itemCreationInfo);
192144

193-
```
145+
oListItem["Title"] = "New Title";
146+
// Data in WKT (World Known Text) format.
147+
oListItem["___location"] = "POINT (78.4 17.4)" ;
194148

149+
oListItem.Update();
150+
context.ExecuteQuery();
151+
}
152+
```
195153

196154
## See also
197-
<a name="SP15addgeo_addlresources"> </a>
198-
199-
200-
- [Integrating ___location and map functionality in SharePoint](integrating-___location-and-map-functionality-in-sharepoint.md)
201-
202-
203-
- [How to: Set the Bing Maps key at the web and farm level in SharePoint](how-to-set-the-bing-maps-key-at-the-web-and-farm-level-in-sharepoint.md)
204-
205-
206-
- [How to: Extend the Geolocation field type using client-side rendering](how-to-extend-the-geolocation-field-type-using-client-side-rendering.md)
207-
208-
209-
- [Create a map view for the Geolocation field in SharePoint](create-a-map-view-for-the-geolocation-field-in-sharepoint.md)
210-
211-
212-
- [How to: Integrate maps with Windows Phone apps and SharePoint lists](how-to-integrate-maps-with-windows-phone-apps-and-sharepoint-lists.md)
213-
214-
215-
- [Use the SharePoint ___location field type in mobile applications](https://technet.microsoft.com/library/fp161355%28v=office.15%29.aspx)
216-
217-
218155

156+
- [Integrating ___location and map functionality in SharePoint](integrating-___location-and-map-functionality-in-sharepoint.md)
157+
- [How to: Set the Bing Maps key at the web and farm level in SharePoint](how-to-set-the-bing-maps-key-at-the-web-and-farm-level-in-sharepoint.md)
158+
- [How to: Extend the Geolocation field type using client-side rendering](how-to-extend-the-geolocation-field-type-using-client-side-rendering.md)
159+
- [Create a map view for the Geolocation field in SharePoint](create-a-map-view-for-the-geolocation-field-in-sharepoint.md)
160+
- [How to: Integrate maps with Windows Phone apps and SharePoint lists](how-to-integrate-maps-with-windows-phone-apps-and-sharepoint-lists.md)
161+
- [Use the SharePoint ___location field type in mobile applications](https://technet.microsoft.com/library/fp161355%28v=office.15%29.aspx)

0 commit comments

Comments
 (0)