PowerShell code to update a CRM 2011 field (using REST/oData)

In this earlier post I showed how to loop through all the contacts in CRM 2011. Next thing I wanted to do was to update a field on each Account. So I needed to figure out how to update data, not just read it. Here is the code to do that:

	$assembly = [Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
	$webclient = new-object System.Net.WebClient
	$webclient.UseDefaultCredentials = $true
	$webclient.Headers.Add("Accept", "application/json")
	$webclient.Headers.Add("Content-Type", "application/json; charset=utf-8");
	$webclient.Headers.Add("X-HTTP-Method", "MERGE")


4 Responses to PowerShell code to update a CRM 2011 field (using REST/oData)

  1. Mark May 16, 2014 at 6:09 pm #

    Hi Jeff

    Great post and I have found your site incredibly useful (thank you)

    I’m having an issue using the PowerShell code to update a field in CRM 2011 – below is the code I’m using

    $assembly = [Reflection.Assembly]::LoadWithPartialName(“System.Web.Extensions”)
    $url=”http://TestServer/TestCRM/xrmservices/2011/OrganizationData.svc/LeadSet?$filter=LeadId eq guid’$GUIDofAccount'”
    $webclient = new-object System.Net.WebClient
    $webclient.UseDefaultCredentials = $true
    $webclient.Headers.Add(“Accept”, “application/json”)
    $webclient.Headers.Add(“Content-Type”, “application/json; charset=utf-8”);
    $webclient.Headers.Add(“X-HTTP-Method”, “MERGE”)

    But when I execute the query I get the following error

    Exception calling “UploadString” with “2” argument(s): “The remote server returned an error: (405) Method Not Allowed.”
    At C:\scripts\CRMUpdateLead.ps1:48 char:1
    + $resultString=$webclient.UploadString($url,$stringToUpload)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

    The issue would appear to relate to the formatting of $stringToUpload=”{`”new_Contacted`”:`”Yes`”}”

    But I’m struggling to resolve or figure out the correct syntax – Can you help?

  2. Mark May 19, 2014 at 5:59 am #

    Hi Jeff,

    Resolved the problem now 🙂

    Would you know the correct syntax to update / change the ownerid of record ?


  3. Mark June 1, 2014 at 3:54 pm #

    Hi Jeff

    After spending some time, I have managed to figure out the correct syntax to update a lookup field 🙂

    For reference here is the syntax


  4. jbmurphy July 7, 2014 at 1:45 pm #

    Thank you for the updates! I have been working on different types of projects, so I have not had a chance to go back and look at these older posts. I hope you found everything you needed.

Powered by WordPress. Designed by WooThemes