0

I need to update the value of newly introduced column "Location" field in SharePoint Online Via CSOM C#.

is it possible to do so?

I tried using FieldLocation and FieldGeoLocation classes but unable to get success on this.

If Yes please share the code or reference.

2
  • 1
    What is the internal name of your Geolocation field? Jan 22 '20 at 7:14
  • I am using Location Field. The internal name is "Address" Jan 22 '20 at 7:16
1

For GeoLocationField:

Assuming you have fetched the list items you want to update in oListItem then try something like below:

FieldGeolocationValue oGeolocationValue = new FieldGeolocationValue();
oGeolocationValue.Latitude = (double)17.4;
oGeolocationValue.Longitude = (double)78.4;
oListItem["Address"] = oGeolocationValue;

oListItem.Update();
context.ExecuteQuery();

Reference:

Add a list item with the Geolocation field value to a SharePoint list programmatically.

Update from Comments:

For "Location" field:

  1. While typing address in location field if bing finds the address in its database then it stores the value in column as JSON object in below format:
{
    "EntityType": "LocalBusiness",
    "LocationSource": "Bing",
    "LocationUri": "https://www.bingapis.com/api/v6/localbusinesses/YN873x16209519703153017334",
    "UniqueId": "https://www.bingapis.com/api/v6/localbusinesses/YN873x16209519703153017334",
    "DisplayName": "Microsoft Corporation",
    "Address": {
        "Street": "1 Microsoft Way",
        "City": "Redmond",
        "State": "WA",
        "CountryOrRegion": "US",
        "PostalCode": "98052"
    },
    "Coordinates": {
        "Latitude": 47.6398811340332,
        "Longitude": -122.12833404541016
    }
}

So you need to pass it in your code like below:

string addressJSON = "{'EntityType':'LocalBusiness','LocationSource':'Bing','LocationUri':'https://www.bingapis.com/api/v6/localbusinesses/YN873x16209519703153017334','UniqueId':'https://www.bingapis.com/api/v6/localbusinesses/YN873x16209519703153017334','DisplayName':'Microsoft Corporation','Address':{'Street':'1 Microsoft Way','City':'Redmond','State':'WA','CountryOrRegion':'US','PostalCode':'98052'},'Coordinates':{'Latitude':47.6398811340332,'Longitude':-122.12833404541016}}";
oListItem["Address"] = addressJSON;
oListItem.Update();

It will store the data in each sub-entities of location field like City, State, Country, etc.

  1. If the bing is not able to find the address in its database then it stores the value in below format:
{
    "DisplayName": "9585 Marshal, Centreville, VA, USA",
    "EntityType": "Custom"
}

In this case you need to pass it in similar way like:

string addressJSON = "{'DisplayName':'9585 Marshal, Centreville, VA, USA','EntityType':'Custom'}";
oListItem["Address"] = addressJSON;
oListItem.Update();

But it will store the data only in "Name" sub-entity of location field.

Hope this helps!!

7
  • I am having address values in string. Not coordinates. Jan 22 '20 at 7:20
  • Check the reference in my answer. There are only two ways to add data in geolocation field, either as mentioned in answer or like oListItem["Address"] = "POINT (78.4 17.4)" ; Jan 22 '20 at 7:25
  • but I have values like "9585 Marshal, Centreville, VA, USA".. If I add via GUI it allow me to enter that. Jan 22 '20 at 7:27
  • Can you please check again the data type of your field is "Geolocation" or something else (maybe single line of text)? Also, can you please add the screenshot of the form field where it is allowing you to add the address in this format. Jan 22 '20 at 7:32
  • 1
    That worked perfectly fine and great detail level information... Jan 22 '20 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.