如何通过OnClick使用SharePoint用户名更新SharePoint 2010列

How to update SharePoint 2010 Column with SharePoint Username via OnClick?

本文关键字:SharePoint 更新 2010列 用户 OnClick 使用 何通过      更新时间:2023-09-26

早上好,我来找你们,寻求帮助,让两个函数工作起来。我想我快到了,但我错过了一些东西。我无法更新SharePoint中的字段,但我可以打开文档,没有问题。下面的代码中缺少什么吗?

<script type="text/javascript">
function fnUpdateRecord(userId, id) {
            $.getJSON("PROGRAM/_vti_bin/ListData.svc/List(" + id + ")?$select=ViewBy", function (data) {
                            var viewby = data.d.ViewBy;
                            var username = userId;
                            var doc = new Object();
                                doc.ViewBy = username;
                            $.ajax({
                                            method: "POST",
                                            url: "PROGRAM/_vti_bin/listdata.svc/List(" + id + ")",
                                            contentType: "application/json; charset=utf-8",
                                            processData: false,
                                            beforeSend: beforeSendFunction,
                                            data: JSON.stringify(doc),
                                            dataType: "json",
                                            error: function (xhr) {
                                                            alert(xhr.status + ": " + xhr.statusText);
                                            },
                                            success: function () {  
                                            }
                            });
            });
}
function fnRecordAccess(id, path) {
            $.ajax({
                            url: "GetCurrentUser.aspx",
                            context: document.body
                            }).success(function(result) {
                                            var userId = $(result).find('.wtf').text()
                                            fnUpdateRecord(userId, id);
                    window.open(path, "othrWn");
                            }).error(function(error) {
                                            console.log(error);
            });

}
</script>

我认为通过OnClick:调用这些函数

onclick='fnRecordAccess(" + i.Id + ", &quot;" + path + "&quot;)'><i class='fa fa-lg fa-pencil'></i> View</a>

我可以在另一个窗口中加载item/infopath表单,但它似乎没有运行在ViewBy列中添加用户名的函数。有什么想法吗?感谢您的协助!

编辑:添加了fnCountrySearch;这将调用其他函数。

function fnCountrySearch(choice) {
    fnWaitDialog("show");
    var searchId = choice;
    $("#tableBody tr").remove();
    $.getJSON("PROGRAM/_vti_bin/ListData.svc/List?$filter=Country eq '" + searchId + "'&$orderby=Name", function (data) {
        var d = data.d;
        if (d.results.length == 0) {
            $("#noResultsAlert").show();
            $("#notingQueried").hide();
        }
        else {
            $.each(d.results, function (n, i) {
            var path = i.Path + "/" + i.Name;
                $("#tableBody").append("<tr><td>" + "<a class='btn btn-sm btn-default' class='pull-left' href='#' onclick='fnRecordAccess(" + i.Id + ", &quot;" + path + "&quot;)'><i class='fa fa-lg fa-pencil'></i> View</a></td></tr>");

            });
            $("#noResultsAlert").hide();
            $("#notingQueried").hide();
        }
    })
    .always(function () {
        fnWaitDialog("hide");
    });
}

beforeSendFunction:

function beforeSendFunction(xhr) {
// Manipulate headers for update
xhr.setRequestHeader("If-Match", "*");
// Using MERGE so that the entire entity doesn't need to be sent over the wire.
xhr.setRequestHeader("X-HTTP-Method", 'MERGE');

}

REST

要将您的代码与已发布的示例进行比较,您可以在此处参考Microsoft关于SharePoint 2010 REST界面的文档:

  • 客户端应用程序的数据访问:使用REST接口
  • 参考实现:客户端:使用JavaScript中的REST接口

JSOM

SharePoint 2010有一个JavaScript客户端对象模型,您可以将其用作REST API的替代方案。如果您发现自己通过JavaScript调用REST API,这可能是一个特别有吸引力的选项,因为客户端对象模型不需要额外的库。

如果您要重写fnUpdateRecord方法以使用JavaScript客户端对象模型,它将如下所示:

fnUpdateRecord(userId, id){
    var listName = "List", fieldName = "ViewBy", newValue = userId + " @ " + new Date() + ";'n";
    var clientContext = new SP.ClientContext();
    var list = clientContext.get_web().get_lists().getByTitle(listName);
    var item = list.getItemById(id);
    clientContext.load(item);
    clientContext.executeQueryAsync(Function.createDelegate(this,function(){
        // get current field value...
        var currentValue = item.get_item(fieldName);
        item.set_item(fieldName, newValue + currentValue);
        item.update();
        // update the field with the new value
        clientContext.executeQueryAsync();
    }),Function.createDelegate(this,function(sender,args){alert(args.get_message());}));
}

请注意,在使用JavaScript客户端对象模型时,需要等待SP.JS库首先加载。这可以使用内置的ExecuteOrDelayUntilScriptLoaded方法来实现,比如:

ExecuteOrDelayUntilScriptLoaded(yourFunctionName,"SP.JS");

ExecuteOrDelayUntilScriptLoaded(function(){
    // your code here
},"SP.JS");