Set the Bing Maps key at the web and farm level in SharePoint
Learn how to set the Bing Maps key programmatically at the web (SharePoint Server and SharePoint Online) and farm(SharePoint Server) level by using the SharePoint client object model and Windows PowerShell, to enable the Bing Maps functionality in SharePoint lists and location-based web and mobile apps.
Prerequisites for setting the Bing Maps key
To follow the steps in this example, you should have the following:
- SharePoint - with administrative privileges at farm OR site collection level
- SharePoint Online Management Shell - https://www.microsoft.com/download/details.aspx?id=35588
- SharePoint Online PnP PowerShell - https://github.com/SharePoint/PnP-PowerShell/
- Visual Studio 2010 or later
- A valid Bing Maps key, which you can obtain from the Bing Maps Account Center.
Important
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.
Note
PnP PowerShell is an open-source solution with active community providing support for it. There is no SLA for the open-source tool support from Microsoft.
Code example: Set the Bing Maps key at the farm or web level
The Bing Maps key can be set at the farm or web level. To set the Bing Maps key at the farm level, you need administrator rights on the server; you can then add the key by using the SharePoint Management Shell. To set the Bing Maps key at the web level, write a console application that uses the SharePoint client object model or leverage SharePoint PnP PowerShell.
Tip
The Bing Maps key set at web level has higher precedence order than the Bing Maps key set at farm level.
To set the Bing Maps key at the farm level using Windows PowerShell
Log on to the SharePoint server as an administrator, and open the SharePoint Management Shell.
Execute the following command:
Set-SPBingMapsKey -BingKey "<Enter a valid Bing Maps key>"
The Bing Maps key is now set at the farm level in SharePoint.
Note
When you use Windows PowerShell, the Bing Maps key can be set only at the farm level. If you want to set the Bing Maps key at the web level, you can set the key programmatically, as shown in the following section.
To set the Bing Maps key at the web level with SharePoint PnP PowerShell
- Open the SharePoint Online Management Shell
- Connect to the site you want to add/update the Bing Maps key
Connect-PnPOnline -url "https://TENANT.sharepoint.com/sites/SITEURL"
- Add the Bing Maps Key to the Site Property Bag (Update "YOURKEYVALUE")
Set-PnPPropertyBagValue -Key "BING_MAPS_KEY" -Value "YOURKEYVALUE"
- If you receive an error regarding NoScript being enabled, you must disable it in the site. Then rerun step 3.
Set-PnPSite -NoScriptSite $false
Note
Ensure you understand the implications of changing a site's NoScript Policy - Allow or prevent custom script.
To set the Bing Maps key at the farm or web level using the client object model with Visual Studio
Start Visual Studio.
On the menu bar, choose File, New Project. The New Project dialog box opens.
In the New Project dialog box, choose C# in the Installed Templates box, and then choose the Console Application template.
Give the project a name, and then choose the OK button.
Visual Studio creates the project. Add a reference to the following assemblies, and choose OK.
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
In the default .cs file, add a using directive as follows.
using Microsoft.SharePoint.Client;
Add the following code to the Main method in the .cs file.
class Program
{
static void Main(string[] args)
{
SetBingMapsKey();
Console.WriteLine("Bing Maps set successfully");
}
static private void SetBingMapsKey()
{
ClientContext context = new ClientContext("<Site Url>");
Web web = context.Web;
web.AllProperties["BING_MAPS_KEY"] = "<Valid Bing Maps Key>"
web.Update();
context.ExecuteQuery();
}
}
- Replace the <Site Url>; and <Valid Bing Maps Key> with valid values.
- Set the target framework in Project Properties as .NET Framework 4.0, and run the example.
- The key should now be set at the web level.
Next steps
To learn more about working with location and map functionality in SharePoint, see the following: