使用Java脚本更改SharePoint Online列表项批准? [英] Changing SharePoint Online List Item Approval with Java Script ?

查看:67
本文介绍了使用Java脚本更改SharePoint Online列表项批准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有

这是几个星期前的一篇文章的后续跟进。

This is sort of follow up to a post of min a few weeks ago.

我的代码工作得很好,但我没有考虑的是列表中有SharePoint内置的批准/拒绝/处理。当我在显示页面上的代码运行它然后递增它,它将其更改为处理,因为它确实更改了项目的
部分。

The code i have works great, but I didn’t account for is the list has the SharePoint built in Approval / Reject / Processing. When the code, which i have on the display page, runs it then increments it which changes it to processing since it did change part of the item.

问题一,可以你用JavaScript修改了一个项目,如果是这样的话,我将如何做到这一点。

Question one, can you change an item to approved with JavaScript and if so how would I go about doing that.

问题二,是否会因为将命中列入一个单独的列表而增加更多而不是弄乱批准。

Question two, would it make more since to have the hits in a separate list and increment that rather than messing with Approval.

以下代码供参考,代码为Dennis Guo。

Code below for reference, credit to Dennis Guo for the code.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
	var listName="Test HC 1";
	var itemId=getUrlParameter("ID");
	updateHits(listName,itemId);
});	
function updateHits(listName,itemId){
	var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('"+listName+"')/items("+itemId+")";
	$.ajax({
		url: url,
		type: "GET",               
		headers: {
			"Accept": "application/json;odata=verbose",
		},
		success: function (data) {
			var item=data.d;			 
			var hits=0;
			if(item.Hits==null){
				hits=1;
			}else{
				hits=item.Hits+1;
			}			 
			var item = {
				"__metadata": { "type": "SP.Data.Test_x0020_HC_x0020_1ListItem" },
				"Hits": hits    
			};
			$.ajax({
				url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
				type: "POST",
				contentType: "application/json;odata=verbose",
				data: JSON.stringify(item),
				headers: {
					"Accept": "application/json;odata=verbose",
					"X-RequestDigest": $("#__REQUESTDIGEST").val(),
					"X-HTTP-Method": "MERGE",
					"If-Match": "*"
				},
				success: function (data) {
					console.log('hit +1');					
				},
				error: function (data) {
					console.log("Error");
				}
			});
					
		},
		error: function (error) {
			alert(JSON.stringify(error));  
		}
	});
}
function getUrlParameter(name){
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
</script>




推荐答案

嗨Joshua,

Hi Joshua,

问题1:

如果是OOTB批准工作流,然后我们可以使用此JavaScript代码批准相关的工作流任务(任务列表名称是"工作流任务1",项目ID:7):

If it is OOTB Approval workflow, then we can use this JavaScript code to approve the related workflow task (Task list name is "Workflow Tasks 1", item Id:7):

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(approveTask, 'sp.js');

function approveTask(){
    var ctx = SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle('Workflow Tasks 1');
    var item = list.getItemById(7);
    item.set_item('Completed',true);
    item.set_item('PercentComplete',1);
    item.set_item('Status','Approved');
    item.set_item('WorkflowOutcome','Approved');
    item.update();
    ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this. onFailed));
   }
   function onSuccess()
   {
     console.log('Approve Task Successfully');
   }
   function onFailed()
   {
     console.log( args.get_message() + '\n' + args.get_stackTrace());
   }
</script>




问题2:


Question 2:

审批流程将使审批人重定向到项目显示表格,因此无论"点击"是什么。在原始列表或单独的列表中,它会增加,这不是解决方案,应该使用上面的代码以编程方式批准任务
以防止Approver再次重定向到显示表单。 

The Approval Process will make Approver redirect to the Item Display form, so no matter the "Hits" is in the original list or a separated list, it will increase, this is not the solution, should use the above code to approve the task programmatically to prevent Approver redirect to Display form again. 

谢谢

最好的问候


这篇关于使用Java脚本更改SharePoint Online列表项批准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆