0

I have a problem with a service. I create a custom service with 1 method. When I test it on debug mode it work awesome, but when I call it with $.ajax it give a 50 error:

"{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}"

Not even get into the service. I have tested as a dummy returning just a simple string and it gives me the same error.

My method code:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public object GetRespuestas(string idSolicitudServicio)
        {
            Log.Monitorear("*** GetRespuestas - Iniciando");
            List<DocumentosProceso> listDocumentosProceso = new List<DocumentosProceso>();

            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("user", "pass", "domain");
            CredentialCache cc = new CredentialCache();

            cc.Add(new Uri(Sitio), "Negotiate", credentials);
            cc.Add(new Uri(Sitio), "Basic", credentials);
            cc.Add(new Uri(Sitio), "NTLM", credentials);

            using (ClientContext context = new ClientContext(Sitio))
            {
                Log.Monitorear("*** GetRespuestas - Iniciando Contexto en sitio: " + Sitio);
                context.Credentials = cc;
                context.AuthenticationMode = ClientAuthenticationMode.Default;
                context.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(clientContext_ExecutingWebRequest);

                Microsoft.SharePoint.Client.List list = context.Web.Lists.GetByTitle("Documentos Licitacion");
                FieldCollection fieldColl = list.Fields;

                Microsoft.SharePoint.Client.CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery();
                query.ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='IdSolicitudServicio' /><Value Type='Text'>" + idSolicitudServicio + "</Value></Eq><And><Eq><FieldRef Name='Aplica' /><Value Type='Choice'>Externo</Value></Eq><Eq><FieldRef Name='TipoDocumento' /><Value Type='Text'>Archivo de Respuestas y Aclaraciones</Value></Eq></And></And></Where></Query></View>";
                ListItemCollection listItems = list.GetItems(query);

                context.Load(listItems);
                context.ExecuteQuery();
                Log.Monitorear("*** GetRespuestas - Query Ejecutada");

                if (listItems.Count > 0)
                {
                    Log.Monitorear("*** GetRespuestas - Se encontraron " + listItems.Count + " registros");
                    foreach (ListItem doc in listItems)
                    {
                        DocumentosProceso documentosProceso = new DocumentosProceso();
                        documentosProceso.ID =
                            limpiarID((doc["ID"] == null)
                                ? ""
                                : doc["ID"].ToString());

                        documentosProceso.Nombre =
                           limpiarID((doc["FileLeafRef"] == null)
                               ? ""
                               : doc["FileLeafRef"].ToString());

                        documentosProceso.Url =
                           limpiarID((doc["FileRef"] == null)
                               ? ""
                               : doc["FileRef"].ToString());

                        documentosProceso.TipoDocumento =
                           limpiarID((doc["TipoDocumento"] == null)
                               ? ""
                               : doc["TipoDocumento"].ToString());


                        listDocumentosProceso.Add(documentosProceso);
                    }
                }
            }

            return _js.Serialize(listDocumentosProceso);
        }

My ajax call:

var body = JSON.stringify({IdSolicitudServicio: 09112016122351});
var urlServicio = "http://vmsps2013:800/_layouts/15/SGL_Services/SGL_Service_2.asmx";

$.ajax({
    url: urlServicio +"/GetRespuestas",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: body,
    success: function(data2) {
        console.log(data2); 
    },
    error: function(data) {
        console.log(data);
    }
});

I would appreciate your help

  • nevermind already found my error... I'm an idiot. sorry – Marco Nov 25 '16 at 20:46
1

You cannot just do an ajax request, you must use SP.RequestExecutor. The call is pretty much the same as jQuery ajax. First you declare a new object of RequestExecutor, then u call executeAsync. And here's MSDN page with usage example.

Your code would be like this:

var url = "http://vmsps2013:800";
var urlServicio = "http://vmsps2013:800/_layouts/15/SGL_Services/SGL_Service_2.asmx";
var body = JSON.stringify({IdSolicitudServicio: 09112016122351});
var executor = new SP.RequestExecutor(url);
executor.executeAsync(
    {
        url: urlServicio,
        method: "POST",
        body: body,
        headers: { "Accept": "application/json", "Content-Type": "application/json" },
        success: function(data2) {
            console.log(data2); 
        },
        error: function(data) {
            console.log(data);
        }
    }
);

Why do you use Client Object Model in your web-service, when you can use Server Object Model? Which is easier to code, in my opinion.

  • first that is not true. second my error was the parameter, key sensittive – Marco Nov 25 '16 at 20:47

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.