Muokkaa

Jaa


Set the Bing Maps key at the web and farm level in SharePoint

How to topic

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:

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

  1. Log on to the SharePoint server as an administrator, and open the SharePoint Management Shell.

  2. 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

  1. Open the SharePoint Online Management Shell
  2. Connect to the site you want to add/update the Bing Maps key
Connect-PnPOnline -url "https://TENANT.sharepoint.com/sites/SITEURL"
  1. Add the Bing Maps Key to the Site Property Bag (Update "YOURKEYVALUE")
Set-PnPPropertyBagValue -Key "BING_MAPS_KEY" -Value "YOURKEYVALUE"
  1. 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

  1. Start Visual Studio.

  2. On the menu bar, choose File, New Project. The New Project dialog box opens.

  3. In the New Project dialog box, choose C# in the Installed Templates box, and then choose the Console Application template.

  4. Give the project a name, and then choose the OK button.

  5. Visual Studio creates the project. Add a reference to the following assemblies, and choose OK.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
  1. In the default .cs file, add a using directive as follows. using Microsoft.SharePoint.Client;

  2. 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();
        }
    }

  1. Replace the <Site Url>; and <Valid Bing Maps Key> with valid values.
  2. Set the target framework in Project Properties as .NET Framework 4.0, and run the example.
  3. 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: