如何通过OnClick使用SharePoint用户名更新SharePoint 2010列
How to update SharePoint 2010 Column with SharePoint Username via OnClick?
早上好,我来找你们,寻求帮助,让两个函数工作起来。我想我快到了,但我错过了一些东西。我无法更新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 + ", "" + path + "")'><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 + ", "" + path + "")'><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");
相关文章:
- Javascript循环不会自我更新
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 添加文字和评论功能更新Div
- AngularJS:ng之后,重复$scope值未按预期更新
- 如何通过数组更新角度子范围
- Ajax聊天消息重复而不仅仅是更新
- 通过CSV文件上载更新数据库表
- 平均值:无法将数据更新到数据库
- $rootScope未使用forEach进行更新
- 如何通过OnClick使用SharePoint用户名更新SharePoint 2010列
- SharePoint 2010 - Javascript 根据标题更新列表项值
- SharePoint 2013 更新列表项时保存冲突错误
- 使用CAML/jQuery将多条记录更新为Sharepoint(批量更新)
- SharePoint 2013列表项更新,列表名称中包含空格
- 使用jquery更新Sharepoint中的富文本列
- SharePoint 2013:如何使用JavaScript CSOM更新多值查找字段
- 使用REST - javascript更新SharePoint文档库中的文件内容
- SharePoint 2010异步更新打破了内联编辑的日期选择器
- 在Sharepoint 2013中使用REST更新管理元数据值
- Document.location.href在sharepoint上没有更新onkeypress