Updating user through REST API returning XML error.

Updating user through REST API returning XML error.

This question is answered

Hi there,

We would like to create, update, and delete user accounts within Telligent Community (5.0.40807.7675) using data from external sources. Unfortunately, I am running into trouble when updating user accounts. The following code demonstrates what we are trying to do and strays very little from the API documentation:

   1:  using System;
2: using System.Collections.Generic;
3: using ISD.Web.AccountManagement.Common.Classes;
4: using dir = ISD.Web.Directory.Classes;
5: using cs = CommunityServer.Components;
6: using System.Net;
7: using System.Text;
8: using System.IO;
9: using System.Configuration;
10:
11: namespace ISD.Web.AccountManagement
12: {
13: public partial class _Default : System.Web.UI.Page
14: {
15: protected void Page_Load ( object sender, EventArgs e )
16: {
17: UpdateTony();
18: }
19:
20: public void UpdateTony ()
21: {
22: HttpWebRequest request =
WebRequest.Create( "http://isd.web.edu/api.ashx/v2/users/2726" ) as HttpWebRequest;
23: request.Method = "POST";
24: request.Headers.Add( "Rest-Method", "PUT" );
25:
26: AddApiKeyToRequest( ref request );
27:
28: string requestBody =
"<User><Id>2726</Id><DisplayName>Tony Updated</DisplayName></User>";
29:
30: byte[] bytes = Encoding.UTF8.GetBytes( requestBody );
31: request.ContentLength = bytes.Length;
32:
33: Stream requestStream = request.GetRequestStream();
34: requestStream.Write( bytes, 0, bytes.Length );
35: requestStream.Close();
36:
37: using ( HttpWebResponse webResponse = request.GetResponse() as HttpWebResponse )
38: {
39: using ( var reader = new StreamReader( webResponse.GetResponseStream() ) )
40: {
41: Response.Write( reader.ReadToEnd() );
42: }
43: }
44: }
45:
46: private void AddApiKeyToRequest ( ref HttpWebRequest request )
47: {
48: // Api Key
49: string apiKey64 = Convert.ToBase64String( Encoding.UTF8.GetBytes(
50: string.Format( "{0}:{1}", ConfigurationManager.AppSettings[ "apiKey" ],
ConfigurationManager.AppSettings[ "apiUser" ] ) )
51: );
52: request.Headers.Add( "Rest-User-Token", apiKey64 );
53: }
54: }
55: }

 

After receiving the generic "an error occurred" message from UpdateTony(), I looked in the exception log and found the following:

User Agent:
Path: /api.ashx/v2/users/2726 as HTTP POST
Referrer:
Message: <User xmlns=''> was not expected.
System.InvalidOperationException: <User xmlns=''> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUserUpdateRequest.Read17_Request()

 

Does anyone know what is causing this error? According to the API documentation for updating users, the simple requestBody XML on line 28 should work.

Verified Answer
  • Never mind. I answered my own question after looking at some other samples. The submitted XML needs to be contained within a root-level <Request> node:

    <Request>
    <User>
    <Id>2726</Id>
    <DisplayName>Tony Renamed</DisplayName>
    </User>
    </Request>

     

All Replies
  • Never mind. I answered my own question after looking at some other samples. The submitted XML needs to be contained within a root-level <Request> node:

    <Request>
    <User>
    <Id>2726</Id>
    <DisplayName>Tony Renamed</DisplayName>
    </User>
    </Request>