My first app: connecting to SharePoint

Locked My first app: connecting to SharePoint

  • Mittwoch, 2. Mai 2012 03:32
     
     
    Hello all,
     
    thanks a lot for Casablanca, it was a wonderful surprise indeed.
     
    At my workplace we do a lot of work with SharePoint. So, as my first
    proof-of-concept app, I decided to use SharePoint's RESTful features with
    Casablanca. I added a simple run-of-the-mill http_request to a SP site we
    have online, and it returned me a 400 Bad Request error. Then I remembered
    that the SP uses authentication. I would have expected an unauthorized
    error, but anyway I changed my code to use a http_request and add an
    Authorization header. Something like this:
     
    http_request req(methods::GET);
    req.set_relative_uri(uri::encode_uri(L"/somesubsite/_vti_bin/listdata.svc/SomeList"));
    req.headers().add(L"Authorization", L"Basic ZG9tYWluXHVzZXI6cGFzc3dvcmQ=");
     
    http_client client(L"http://intranetc.scanda.com.mx");
    task<http_response> resp = client.request(req);
     
    auto func = [=](task<http_response> resp_task) {
    http_response this_resp = resp_task.get();
     
    wcout << L"Server answered with code: " << this_resp.status_code() <<
    endl;
    wcout << L"\tMessage: " << this_resp.reason_phrase() << endl;
     
    if (this_resp.status_code() == status_codes::OK)
    {
    // do stuff here
    }
     
    };
    resp.then(func);
    resp.wait();
     
    system("Pause");
     
    However, it still throws a 400 Bad Request error. By the way, I tried this
    code on a non-SharePoint site and it did work perfectly fine.
     
    So, my question is: what could cause a 400 Bad Request error? Is there
    something I might be missing?
     
    Thanks in advance!
     
     


    Fernando A. Gómez F.
    fermasmas.wordpress.com
    Galería de ejemplos

Alle Antworten

  • Mittwoch, 2. Mai 2012 14:42
    Besitzer
     
     Beantwortet

    Fernando!

    This is a great proof-of-concept. When you have it working, we would love to see you write up how it works or let us use it as a sample.

    There's good news, and there's bad news, and then there's some potentially good news.... The good news is that you're obviously contacting the service and getting a reply back. That's a good start. From your code snippet, I can see that you have already learned enough of Casablanca to be on your way.

    The bad news is that we don't have any Sharepoint experts on the team. 400 can mean many pretty much anything -- the HTTP spec has this to say:

        The request could not be understood by the server due to malformed syntax.

    Not very specific, is it?

    Services sometimes use the standard status codes in ways that are clear to the service designers, but not as clear to users of it. What this error usually means is that you sent some bad data to the service, and it's not going to do what you ask of it until you fix it. It could be a missing header, or a resource that should be lower-case or not contain certain characters, a date header in the wrong format, or your authentication hash may be incorrect, etc. It'll be very service-dependent.

    When we were writing the libraries to target Azure storage, we had to figure out things we were doing wrong that were getting us 400 responses, too. Fortunately, and this is the potentially good news, Azure storage returns really good error details in the body of the response. Sharepoint may do so, too. It's worth checking whether it does the same: instead of just printing the status code and reason phrase, try printing the whole message with "this_resp.to_string()". If there are clues in the body of the response, and the body is of a textual MIME type (text/*, application/xml, application/json, etc.), it will be printed exactly as it is provided by the service.

    I hope this helps and that you can share further details with us as you get it working!

    Niklas


  • Mittwoch, 2. Mai 2012 15:21
     
     

    Hi Niklas, 

    thanks for the advice! The to_string method showed a correlation error, which is SharePoint's error ID in the logs. So I'll go check there; however, I'm anticipating that this is probably a SP error rather than something to do with my connection. I'll keep you posted.

    And sure, once I have this program running I'll give it to you, or post it at MSDN code gallery or whatever you advise. 

    Cheers and thanks again!



    Fernando A. Gómez F.
    fermasmas.wordpress.com
    Galería de ejemplos

  • Mittwoch, 2. Mai 2012 15:25
    Besitzer
     
     Vorgeschlagene Antwort

    Yes, it's definitely not a connection error.

    If you get a 400, it's not a connection issue, it is the service answering and slapping your hand. Connection errors are manifested by Casablanca as C++ exceptions instead of HTTP responses.

    Niklas

  • Mittwoch, 2. Mai 2012 21:06
     
     

    Follow up: well, the 400 thingy was because SharePoint authenticates using NTLM. Or at least this server (and I presume is the rule, since this was a run-of-the-mill installation). Sigh. So, now I'm working on imitate the NTLM "handshake". Haven't found much info yet, at least not on MSDN docs, so this could take a while. 

    Cheers again!



    Fernando A. Gómez F.
    fermasmas.wordpress.com
    Galería de ejemplos

  • Donnerstag, 3. Mai 2012 01:49
    Besitzer
     
     

    Very interesting! That's an acronym I had forgotten... :-)

    Please keep us informed of your progress and any issues that you run into! We're not the SP (or NTLM) experts and cannot be of much help there, but we do want to make sure that your experience is something we take into account when planning our next steps.

    Niklas