weixin_33691598
2014-05-28 18:58
采纳率: 0%
浏览 42

SharePoint ASMX,AJAX

I have been working for a while now on a project that will be using JQueryUI autocompletes for user names (based on SPWeb.AllUsers) and will also be pulling content from a potentially large list separately in the same form. I initially attempted a page method and could not manage to get a return other than the whole page html (despite setting content type, etc), and so have moved on to attempting it via ASMX. The result of this is a 500. Running through Fiddler gave me the full ASP error: "The file you are attempting to save or retrieve has been blocked from this Web site by the server administrators." I'm fairly certain the code inside the method is not being hit, as a breakpoint set on the first line of the method is not triggered.

Here's my web service:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class projectWebService : System.Web.Services.WebService
{
    public class person
    {
        public person(string name, string company)
        {
            this.name = name;
            this.company = company;
        }

        public string name { get; set; }
        public string company { get; set; }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string getNames()
    {
        try
        {
            //Query list to filter by company and sort by name
            var userQuery = from SPUser user in SPContext.Current.Web.AllUsers
                            orderby user.Name ascending
                            select user;

            List<person> userList = new List<person>();
            foreach (SPUser user in userQuery)
            {
                userList.Add(new person(user.Name, "something"));
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(userList);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}

I've tried a few variations on this with the same result. And here's the AJAX call:

$.ajax({
        type: 'POST',
        dataType: 'json',
        data: '{}',
        contentType: 'application/json; charset=utf-8',
        url: 'projectWebService.asmx/getNames',
        success: function (msg) {
            alert(msg.toString());
            var people = $.parseJSON(msg);
            $('#projectManager').autocomplete({
                source: people
            });
        },
        error: function (xhr, msg, error) {
            alert(msg.toString());
            alert(error);
        }
    });

Attempting to set "UseHttpGet" and making a GET request has been unsuccessful as well.

I found one related question here: This one mentions a web.config entry that may be the culprit. I have set this to no avail.

Also, this article covers other web.config changes that may(?) be required. This did not work for me, instead getting an ASP error for an invalid web.config.

I'm hoping someone has either had to deal with this before or sees something I do not. Thanks for any help!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • ℙℕℤℝ 2014-06-19 14:41

    As it turns out, I had overlooked a crucial behavior: Neither a web service class nor a page method is accessible from within a user control where I was attempting to place it as part of a SharePoint web part. For that matter, nor is it accessible from within a web part. My solution was to create a separate asmx web service and deploy it to my site as a farm solution via the instructions here. From there it was relatively painless.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题