views:

333

answers:

2

I've got a custom new item form which I'm using in SharePoint 2007. What I want to happen is that once the user has entered an asset id and they leave the field, an ajax query checks to see if that asset id is available.

I've used the following code successfully on a normal .NET web applicaiton, however when using the same code in SharePoint, all I get returned is an Internal Server Error and the IIS logs record a status code of 500.

JQuery in new asset form

    $(document).ready(function() {

        $('#ctl00_PlaceHolderMain_newAsset_assetId').blur(function() {
            assetId = $(this).val();

            if (assetId.length >= 3) {
                $.ajax({
                    type: "POST",
                    url: "checkAsset.aspx/AssetIdAvailable",
                    data: "{'assetId':'" + assetId + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(message) {
                        if (message.d != true) {
                            //$('#ctl00_PlaceHolderMain_saveNew').attr('disabled', 'disabled');
                            alert('This asset id is available');
                        }
                        else {
                            alert('This asset id is NOT available');
                            //$('#ctl00_PlaceHolderMain_saveNew').removeAttr('disabled');
                        }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(xhr.statusText);
                    }
                });
            }
        });            
    });

Code behind

public class CheckAssetAvailable : Page
{
    [WebMethod]
    public static bool AssetIdAvailable(string assetId)
    {
        int recCount = 0;
        bool available = false;

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ICTAssetDb"].ConnectionString);
        SqlCommand comm = new SqlCommand("usp_IsAssetIdAvailable", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add("@AssetId", SqlDbType.NVarChar).Value = assetId;
        conn.Open();
        recCount = int.Parse(comm.ExecuteScalar().ToString());
        if (recCount == 0)
        {
            available = true;
        }

        comm.Dispose();
        conn.Close();
        conn.Dispose();

        return available;
    }
}
+1  A: 

There is something strange about your URL: checkAsset.aspx/AssetIdAvailable I'm not sure if this is supposed to be a ? with a parameter or whether it is trying to call a method inside your class, but the URL is not valid (unless you have some custom URL rewrite rules in place).

It looks like if you can the service to work correctly outside of the jQuery call and you fix your URL then it will work correctly.

Peter Jacoby
A: 

I second Peter's answer, try checking your service call in a browser and once you get the direct browser call working, then you can make that work with your ajax. I haven't done a whole bunch of Sharepoint to date, but most of the URLs I've had to code in them had nice little "~" type features in them if they were deployed with my solution, and if its deployed directly as a Sharepoint resource then it would be /somedir/myfile.

mezmo